Go Down

Topic: More problems with attachInterrupt (Read 404 times) previous topic - next topic

mrandersen

When I pass a magnet by a hall effect sensor, it will trigger both timeStarted and timeEnded, although I do not see how this is possible. It triggers them at the exact same time.
Both timeStarted and timeEnded have to have a value for the loop to start. What am I doing wrong?

Code: [Select]
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();
  }
 

 
}

jraskell

How is the hall effect sensor attached to the Arduino and what is the intended behavior you are attempting to implement?

Just a guess here, but what you may be intending is to trigger one interrupt on the rising edge and one on the falling, not both on the falling edge.  If the hall effect sensor is tied to both input 2 and 3, then you will indeed trigger both interrupts essentially at the same time.

Go Up