The number get's multiplied by a small number and that result is microseconds between each rise and fall of the crank signal.
Trying to do any serial monitoring gives a garbage signal (I don't yet want to try multi-threaded coding).
The signal pauses very briefly after sending each value. Like zoomed way in on the oscilloscope brief.
Code: Select all
int TPS = 1;
int sequence[48] = {0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0};
int cam[48] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int phase = 0;
int tooth = 0;
int low = 0;
int high = 0;
int RPMlow = 0;
int RPMhigh = 0;
int math = 1;
int halfdone = 1;
int done = 1;
int width = 1;
unsigned long previousmicros = 0;
int Hz = 1;
void setup() {
Serial.begin(300);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
}
void loop() {
if (Serial.available())
{
Hz = Serial.parseInt();
}
if (halfdone == 0) {
if (micros() - previousmicros >= RPMlow) {
halfdone = 1;
done = 0;
digitalWrite(0, HIGH);
previousmicros = micros();
}
}
if (done == 0) {
if (micros() - previousmicros >= RPMhigh) {
done = 1;
math = 1;
digitalWrite(0, LOW);
previousmicros = micros();
}
}
if (math == 1) {
tooth++;
if (tooth == 48) {
tooth = 0;
}
TPS = Hz;
width = sequence[tooth];
phase = cam[tooth];
digitalWrite(1, phase);
if (width == 0) {
low = 2;
high = 1;
}
if (width == 1) {
low = 1;
high = 2;
}
halfdone = 0;
math = 0;
RPMlow = low * TPS;
RPMhigh = high * TPS;
}
}