Hello everyone! First time posting, so if I mess up please forgive me :}
I'm working on a small project and could use a little help. I have an IR sensor hooked up to the Arduino and basically it reads detection of an object (True), or no object (False). I'm trying to hook up the IR sensor to a stopwatch (which i have programmed to the Arduino's LCD shield) so that when the IR sensor gets it's first "True", it will stop reading any more values.
I have something along these lines so far:
void loop(){
int y = 0;
/////////////////////////////////
if ( digitalRead(sensorIR)==true){
Serial.println("Detection");
y = 1;
}else{
Serial.println("No detection");
}
//////////////////////////////////
buttonState = y ; // Check for button press, read the button state and store
// check for a high to low transition if true then found a new button press while clock is not running - start the clock
if (buttonState == 0 && lastButtonState == 1 && blinking == false){
// var blinking = is the timer is timing?
startTime = millis(); // store the start time
blinking = true; // turn on blinking while timing
delay(10); // short delay to debounce switch
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
}
// check for a high to low transition, if true then found a new button press while clock is running - stop the clock and report
else if (buttonState == 0 && lastButtonState == 1 && blinking == true){
blinking = false; // turn off blinking, all done timing
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
/// stopwatch calculations..
}
Currently the program will trigger the stopwatch on a detection and then stop it on another detection. I have the IR sensor acting as a pseudo toggle switch right now because I want to begin running the stopwatch with the IR sensor but then be able to stop it with a different mechanism.
My current problem is in the first If statement. Basically I want it to flip "y" to 1 and then stop taking readings. Ive tried moving the "int y = 0" to the setup loop, removing the "= 0", among some other stuff but either it wont work or it doesn't change anything.
I'm pretty new to coding so any help (or a better way to do this all together) is appreciated!
Thanks!