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:
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.
ElectronicsNoobie:
If I remember correctly you cant make calls to Serial from within an interrupt. I would remove them from your interrupt functions.
That's correct, all further interrupts are disabled once you are inside a ISR function so anything that requires the active use on interrupts (like serial) will just hang inside the ISR. The general rule for writing good effective ISR functions is to keep it simple, just do the bare minimum needed to be done, increment a variable, set a flag, save a value, etc, and do all the heavy lifting in the main loop by testing such flags, or variables to actually do stuff.
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
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.
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.
Well maybe the interrupts are coming faster then you think. Try using micros():
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?
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?
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?
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.