Hey, so I'm trying to convert an old photogate into a modern one using an arduino for my school and have (obviously) run into a bit of a pickle. So there are two problems, the first is in the measuring section where once the photogate is triggered both endd and start give the same value as seen in the serial monitor. So the first question would be, is there a way to either delay the second if statement so that end doesn't trigger immediately or is there a more elegant solution I'm missing?
Second problem/question is that the millisecond timer once it reaches a bit above 30000 it keeps counting up but from -30000 something and this unfortunately messes with the first if statement in the measuring section. Is there anything I can do about this?
I've tried looking up solutions with no luck.
any help would be greatly appreciated as school is ending soon and I wont be able to get it back to the teacher afterwards.
/* The code takes a signal from the photogate and times the amount of time between two consecutive signals.
* To reset and do a second test, the button is pressed.
*/
int photogate;// signal form photogate will be retrieved to pin 2
int button; // input from button will be retrieved to pin 12
int start; // To record the initial crossing of the photogate
int endd; // To recored the second crossing of the photogate
int Timee; // to do then record the calculation
int msec; // to keep track of milliseconds
unsigned long time;
void setup() {
Serial.begin(9600); // start the serial monitor on baud 9600
pinMode(2,INPUT); // set the photogate pin to an input
pinMode(12,INPUT_PULLUP); // set the button pin to an input
time=0; // start the timer at 0
}
void loop() {
photogate=digitalRead(2);// reads signal from the photogate
button=digitalRead(12);// reads signal from the button
time = millis();
//Serial.print(photogate);
//This section is for the button to reset the display and photogate measuring
if(button==0){//if the button is pressed
start=0;// reset variable start
endd=0;// reset varialbe endd
Timee=0;// reset Timee
Serial.print("Time:");// on a new line in the serial monitor print "Time:"
Serial.println(Timee); // on the same line in the serial monitor print the number associated with variable Timee
// set display to "Time:0"
}
//This section is the measuring section
if((start==0)&&(endd==0)&&(photogate==0)){// if something passes through the photogate and neither start nor endd has a value
start=time; // save the current time in miliseconds to variable start
Serial.print("Start: ");
Serial.println(start);
}
if((start>0)&&(endd==0)&&(photogate==0)){ // if something passes through the photogate and variable start has a value
endd=time; // save the current time in miliseconds to variable endd
Serial.print("End: ");
Serial.println(endd);
Timee=endd-start; // then change the varialbe Timee to the end time minus the start time giving the time it took to pass through both times
Serial.print("Time:");// on a new line in the serial monitor print "Time:"
Serial.println(Timee); // on the same line in the serial monitor print the number associated with variable Timee
//display "Time:'Timee'"
}
//delay(500);
}