Hi,
So yes, this is yet another chronograph project, I am a novice at programming and using micro controllers, I've only had the Arduino UNO for an evening (but I feel I'm already slightly addicted, I should have got into this a long time ago). The code I do have is a bit of a mix of all the other chronograph topics I could find and some of the example code in the Arduino dev software.
I have two laser beams powered by the 3.3V ouput and two high speed optoschmidt sensors powered by the 5V output.
The optoschmidt sensors are wired up to pins 2 and 4, when the laser beam is pointed at the sensor they let 5V though to the pins.
So the pins are HIGH by default and LOW when a projectile breaks their beam.
This is my attempt so far:
//Laser gate chronograph, with serial output.
int sensorA= 4,sensorB= 2; //The two photodetectors are connected to pins 4 and 2
unsigned long start_time, stop_time, elapsed;
float distance, velocity;
void setup() {
// initialize sensor A as an input
pinMode(sensorA, INPUT);
// initialize sensor B as an input:
pinMode(sensorB, INPUT);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// set distance between photodetectors in metres
distance= 0.02;
}
void loop(){
// Wait until triggerA becomes LOW (projectile cuts beam)
while (digitalRead(sensorA) == LOW) {
start_time=micros(); // record start time
//triggerA was tripped, now wait for triggerB to trip
while(digitalRead(sensorB) == LOW) {
stop_time=micros(); // record stop time
elapsed= stop_time - start_time; //How long did it take ?
velocity= (distance * 1000000) / elapsed; // So, how fast was it going ?
//Send it all through serial
Serial.println ("raw data:");
Serial.println (" ");
Serial.print ("Start=");
Serial.println (start_time);
Serial.print ("Stop=");
Serial.println (stop_time);
Serial.print ("Elapsed=");
Serial.println (elapsed);
Serial.print ("Projectile velocity=");
Serial.print (velocity);
Serial.println ("m/s");
Serial.println ("___ ___ ___ ___");
delay (2000); // don't go too crazy
};
};
}
The while loops don't seem to work like I'd imagined, I only get a reading when both sensors go LOW, when what I would want is to only get is a reading when sensor A is triggered followed by sensor B.
I have tried making sure that the while loop only works when one sensor is HIGH and the other LOW ( using &&) but that just produces nothing.
Here is what I get on the serial monitor:
Does anyone have any pointers as to where I'm going wrong ?
EDIT: Ah it those are actually slightly different results than was I was getting, the micros() start time is now not resetting, I was getting a constant 8microseconds difference on each cycle. I'm guessing this has happened since I changed the variables from int to unsigned long.