So some background: Building an arduino tachometer for my motorcycle, taking the tachometer signal that is already on the bike, routing it through and optocoupler and into the Arduino Uno. Getting a "expected primary expression before do" and a "output was not declared in this scope." Can I get somebody to take a look at my code and tell me what I'm doing wrong? Any critiques or suggestions appreciated.
volatile int RPMpulses = 0;
volatile unsigned long firstpulse = 0;
volatile unsigned long lastpulse = 0;
volatile int period = 0;
String string1 = "Engine Speed =";
unsigned long lastRefresh = millis();
const int refreshInterval = 250;
const int tachpin = 1;
void setup(){
Serial.begin(96000);
attachInterrupt(tachpin,countRPM,RISING);
}
void countRPM()
{
RPMpulses++;
if(RPMpulses > 1) {
lastpulse = millis();
}
else {
firstpulse = millis();
}
}
void loop() {
if((millis() - lastRefresh) > refreshInterval) {
if(RPMpulses>1) { //make sure the engine is running
(do period=(lastpulse-firstpulse)/RPMpulses)
output period)
}
else {
(output zero)
}
RPMpulses = 0;
lastRefresh = millis();
}
Serial.print (string1+period)
}
Thanks, this was an example that I got from someone on reddit. I want to calculate the time difference between the first and last pulse to extrapolate to RPM. There are two pulses for each engine revolution.
I would suggest a copy paste error. As was pointed out:
This lines:
Code:
(do period=(lastpulse-firstpulse)/RPMpulses)
output period)
and
Code:
(output zero)
Are not "Arduino language" so he don't understand they.
Makes no sense whatsoever.
There are one or more syntax errors on each and every line
Hint: the semicolon is used to end a line and your parentheses don't match... Each' (' requires a matching ')' .
You might explore your IDE a little more... You could get lucky and find both Ctrl T and the verify button
Ctrl T would have pointed out all of your errors that verify didn't...
Thanks Doc. I plugged it in at my desk without the optocoupler connected, expecting to get a serial monitor full of EngineSpeed=0. However, I'm getting a garbled text that looks like this: