Greetings, I am relatively new in programming world and would like to make physics experiment tool to make student easier while doing the experiment.
Basically, the tool need to measure time interval between two gate which I use photodiode as the sensor and infrared as the transmitter. The work principle is quite simple, the detail is a follows :
when the ball pass photodiode 1, the timer is started and will off when the ball pass photodiode 2.
here is the fritzing scheme :
Here is my code (If you dont mind, please give me the solution) :
const byte photoTransistor1 = 2;
const byte photoTransistor2 = 3;
unsigned long startTime;
unsigned long endTime;
volatile unsigned long elapsedTime;
volatile boolean done;
void ballPasses ()
{
// if low, ball is in front of light
if (digitalRead (photoTransistor1) == LOW)
{
startTime = micros ();
}
else
{
if (digitalRead (photoTransistor2) == LOW)
{
endTime = micros();
elapsedTime = (endTime - startTime)/1000;
}
}
done = true;
}
void setup ()
{
Serial.begin (115200);
Serial.println ("Timer sketch started.");
attachInterrupt (0, ballPasses, CHANGE);
}
void loop ()
{
if (!done)
return;
Serial.print ("Time taken = ");
Serial.print (elapsedTime);
Serial.println (" S");
done = false;
}
Make sure the voltages on the photodiodes are over the switch thresholds of the digital pins (<1.5volt or >3volt) with/without light.
Measure voltages with a DMM. Ambient light could be a problem.
Leo..
But now you are calling the same function for both. You want to store the start time when the ball reaches the start and store the end time when the ball reaches the end. That should be two separate functions.
But now you still have the ballPassesStart both when the ball first blocks the light (FALLING) and when the light is no longer blocked (RISING). If you change CHANGE to FALLING you will get the interrupt when the ball first blocks the light, which is the time you want to record.
Dear Johnwasser, thank you for breaking the explanation
const byte photoTransistor1 = 2;
const byte photoTransistor2 = 3;
unsigned long startTime;
unsigned long endTime;
volatile unsigned long elapsedTime;
void setup ()
{
pinMode(photoTransistor1, INPUT_PULLUP);
pinMode(photoTransistor2, INPUT_PULLUP);
Serial.begin (115200);
Serial.println ("Timer sketch started.");
attachInterrupt (0, ballPassesStart, FALLING);
attachInterrupt (1, ballPassesEnd, FALLING);
}
void ballPassesStart ()
{
// if low, ball is in front of light
if (digitalRead (photoTransistor1) == LOW) {
startTime = micros ();
}
}
void ballPassesEnd ()
{
if (digitalRead (photoTransistor2) == LOW) {
endTime = micros();
elapsedTime = (endTime - startTime);
}
}
void loop () {
Serial.print ("Time taken = ");
Serial.print (elapsedTime);
Serial.println (" S");
}
Like this one? I wonder, is it necessary to make void ballPassesEnd () ?
If yes, how to implement in the code?
I guest my code still wrong, especially in the logic started from void ballPassesStart ()