Edit for return key test.
OK, got rid of that error message.
Thanks for everyone's help.
End of the code was cleaned-up alot as I work toward putting the math into a function to be called as lar3ry suggested.
And getting rid of all the garbage at the bottom before putting the interrupt functions there got rid of that "not declared" message.
Thanks markT for catching the lower case function call that didn't match the function.
Played some more.
Here's the code. and this time I got auto format working so it's easier to read:
/* Finds time from TDC1, TDC4 and pulseStart. It's a finite state machine.
* Holds in a loop until the proper state occurs.
* First it looks for a LOW on the pulse detector to trigger an interrupt
* that logs the timing of the event.
* The INJECTORWAIT state isn't looking for a LOW on the pin.
* The interrupt does that.
* The INJECTORWAITT state is looking for data stored because the pin went LOW.
* Same with *the other 2 states The injector pulse comes before TDC1.
* How many degrees before top dead center BTDC is what we *are looking for */
#define INJECTORWAIT 0
#define TDCWAIT_1 1
#define TDCWAIT_2 2
//declared volatile because it's a global variable used in a function
volatile unsigned long tdcStartTime; volatile unsigned long injectorStartTime;
volatile unsigned long tdcStartTime_2;
volatile unsigned long tdcStartTime_1;
unsigned long oneRevTime;
unsigned long btdcY;
unsigned long rpmX;
const int pulsePin = 2;
const int tdcPin = 3;
byte state = INJECTORWAIT;
void setup() {
Serial.begin(9600);
pinMode(pulsePin, INPUT);
pinMode(tdcPin, INPUT);
while (digitalRead(tdcPin) == HIGH) //wait here until we are at TDC (any TDC)
}
injectorStartTime = 0;
attachInterrupt (0,pulseIRPT,FALLING); //enable pin 2 pulse interrupt
attachInterrupt (1,tdcIRPT,FALLING); //enable pin 3 tdc interrupt
}
void loop () {
Serial.print("DATA, TIME,,,"); // Opens PLX-DAQ data line
while (state == INJECTORWAIT) {
if (injectorStartTime > 0 ) {
state = TDCWAIT_1;
tdcStartTime = 0;
}
}
while (state == TDCWAIT_1) {
if (tdcStartTime > 0 ) {
tdcStartTime_1 = tdcStartTime; //we stored the value from the tdcIRPT interrupt
intdcStartTime_1
tdcStartTime = 0; //set to 0 so the next state knows if it's been retriggered.
state = TDCWAIT_2;
}
}
while (state == TDCWAIT_2) {
if (tdcStartTime > 0) {
tdcStartTime_2 = tdcStartTime;
injectorStartTime = 0;
}
unsigned long z; //a call to the calculate function
z = calculate (unsigned long tdcStartTime_1,unsigned long injectorStartTime,
unsigned long tdcStartTime_2);
delay 10000;
state = INJECTORWAIT;
}
} //end of loop so interrupt and other functions go here
void tdcIRPT() { // the interrupt routines
tdcStartTime = micros();
}
void pulseIRPT() {
injectorStartTime = micros();
}
int calculate (unsigned long a, unsigned long b, unsigned long c);
{ //calculate function
int btdcY; //degrees before top dead center shouldn't be more than 30
long rpmX; // can be up to 6000
unsigned long Btdc;
Btdc = a - b; // BTDC in microseconds
oneRevTime = c - a; // time for one revolution in microseconds
rpmX = 60000000/oneRevTime; //x-axis data to send to excel
btdcY = ((Btdc/oneRevTime)/360); //y-axis data to send to excel
Serial.print(btdcY);
Serial.print(",");
Serial.println(rpmX);
}
woo hoo knocked some of them down and learned alot.
edited to swap in latest code,.. before anyone gave me crap about how bad I messed up the calculate function.
Oh you think it's bad now? ha.