Pages: [1]   Go Down
Author Topic: More problems with attachInterrupt  (Read 388 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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();
  }
 
 
 
}
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: