mrandersen:
Thanks, now I have another problem.
When either interrupt triggers both timeStarted and timeEnded are the same...const float kDistance = 0.063;
volatile unsigned long timeStarted = 0;
volatile unsigned long timeEnded = 0;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
attachInterrupt(0, startTime, FALLING);
attachInterrupt(1, endTime, FALLING);
Serial.begin(9600);
}
void loop() {
if (timeStarted && timeEnded)
{
// float deltaTime = (timeEnded-timeStarted)/1000.0;
// float velocity = kDistance/deltaTime;
// Serial.print("Velocity = ");
// Serial.println(velocity, DEC);
// Serial.print("Time = ");
// Serial.println(deltaTime, DEC);
Serial.println(timeStarted, DEC);
Serial.println(timeEnded, DEC);
timeStarted = 0;
timeEnded = 0;
}
}
void startTime()
{
if (!timeStarted)
{
timeStarted = millis();
}
}
void endTime()
{
if (!timeEnded)
{
timeEnded = millis();
}
}
I think you have to make those two variables 'volatile' as I added to your code above. Give it a try and report back.
http://arduino.cc/en/Reference/Volatile
Lefty