Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / More problems with attachInterrupt on: October 08, 2012, 09:21:55 am
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();
  }
 
 
 
}
2  Using Arduino / Programming Questions / Re: Trouble with attachinterrupts on: October 07, 2012, 09:48:07 pm
Quote
Short of a wiring problem, that is not really possible. Can you explain how you have the sensors arranged in relationship to the magnet and how fast the magnet is moving between them?

The signal being generated by the sensors is correct as it works flawlessly in the loop code. The two sensors are about 10cm apart, and the magnet passes by them in about 0.2 seconds. Even after I unplug a sensor, the one pin will still trigger both startTime and endTime.
 I appreciate your help.
3  Using Arduino / Programming Questions / Re: Trouble with attachinterrupts on: October 07, 2012, 03:32:52 pm
Quote
Well maybe the interrupts are coming faster then you think. Try using micros():

timeStarted = micros();

timeEnded = micros();

Lefty

After a further look I realized that one pin is triggering both the start time and end time.
Do you know why one input is triggering start time and end time?
4  Using Arduino / Programming Questions / Re: Trouble with attachinterrupts on: October 07, 2012, 02:01:16 pm
Quote
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

I tried this but both variables are still printing as the same number.
5  Using Arduino / Programming Questions / Re: Trouble with attachinterrupts on: October 07, 2012, 01:22:24 pm
Thanks, now I have another problem.
When either interrupt triggers both timeStarted and timeEnded are the same...

Code:
const float kDistance = 0.063;
unsigned long timeStarted = 0;
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();
  }
 
 
 
}
6  Using Arduino / Programming Questions / Trouble with attachinterrupts on: October 07, 2012, 12:56:26 pm
I am using this to sense when a magnet passes the hall effect sensors. They are both trying to do the same thing. The below code works:
Code:
const float kDistance = 0.063;
unsigned long timeStarted;

void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  Serial.begin(9600);
  timeStarted = 0;
 
 
}

void loop() {
  // put your main code here, to run repeatedly:
  if (!digitalRead(2) && !timeStarted)
  {
   timeStarted = millis();
   Serial.println("Start time");
   
  }
  if (!digitalRead(3) && timeStarted)
  {
  float deltaTime = (millis()-timeStarted)/1000.0;
  float velocity = kDistance/deltaTime;
  Serial.print("Velocity = ");
  Serial.println(velocity, DEC);
  Serial.print("Time = ");
  Serial.println(deltaTime, DEC);
  timeStarted = 0;
  Serial.println("End time");

  }

}

However this code does not - It is using attachinterrupts which would work much better, especially if I added more to my code and made it more complex.
Code:
const float kDistance = 0.063;
unsigned long timeStarted;

void setup() {
  timeStarted = 0;
  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() {

}

void startTime()
{
  Serial.println("Start time");
  if (!timeStarted)
  {
    timeStarted = millis();
  }

}
void endTime()
{
  Serial.println("End time");
  if (timeStarted)
  {
  float deltaTime = (millis()-timeStarted)/1000.0;
  float velocity = kDistance/deltaTime;
  Serial.print("Velocity = ");
  Serial.println(velocity, DEC);
  Serial.print("Time = ");
  Serial.println(deltaTime, DEC);
  timeStarted = 0;
  }
  else
  {
   Serial.println("Testing Error!");
  }
 
 
}

Thanks in advance!
Pages: [1]