Retaining a variable in the loop & General code

Fairly beginner here, and trying to finish this project for my boys. If anyone wants to dive into this deeper, I'd welcome your support, but for now, I can't figure out why Arduino cod doesn't retain a value that I try to set in the loop.

When I set SensorFinish1 = 1; it doesn't seem to keep it next time through the loop. If a condition is met, (sensor beam is broken), I was thinking of setting SensorFinish to 1 so next time through the loop, the code will know that a Hot Wheels car has finished and it won't rewrite the time. (Closer to end of code)

This is the whole thing, so I apologize if this is too much code (still new to forum and to coding)

// Name Raceway Test

unsigned long TimeLane1; // Finish line lane 1
unsigned long TimeLane2; // Finish line lane 2 

float StartTime = 0;
float FinishTime1 = 0;
float FinishTime2 = 0;
float StartTimeC;
int StartState = 0;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

// variables will change:
int SensorState1 = 1;       // variable for reading the sensor status
int SensorState2 = 1;       // variable for reading the sensor status
int LaneState1 = 0;         // variable for reading the sensor status
int LaneState2 = 0;         // variable for reading the sensor status
const int StartSwitch = 2;  // Switch on D3 PIN
const int LaneLED1 = 9;     // Lane 1 winning LED on D9 PIN
const int LaneLED2 = 10;    // Lane 2 winning LED in D10 PIN
const int SensorPin1 = 11;  // Finish line lane 1 IR on D11 PIN
const int SensorPin2 = 12;  // Finish line lane 2 IR on D12 PIN
const int BoardLED = 13;
int SensorFinish1 = 0;         // Records if a sensor was tripped
int SensorFinish2 = 0;         // Records if a sensor was tripped

int sensorThresh = 500; // Sets the trigger sensing threshold of the IR receivers. ~1000 = high
int n = 0;






void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Lane 1    Lane 2");
  
  // initialize the sensor pin as an input:
  pinMode(SensorPin1, INPUT);     
  digitalWrite(SensorPin1, HIGH); // turn on the pullup
  pinMode(SensorPin2, INPUT);     
  digitalWrite(SensorPin2, HIGH); // turn on the pullup
  
  pinMode(BoardLED, OUTPUT);      // initialize the LED pin as an output:
  pinMode(StartSwitch, INPUT);      // initialize the Reset Switch pin as an input:
  pinMode(LaneLED1, OUTPUT); // Initialize the Lane 1 LED as an output:
  pinMode(LaneLED2, OUTPUT); // Initialize the Lane 2 LED as an output:
  Serial.print( "StartTime:  ");
  Serial.println(StartTime, 4);

  StartTime = 0;
  TimeLane1 = 0;
  TimeLane2 = 0;
//  StartTime = micros(); // Use micro seconds (1,000,000 microseconds in a second)
  
 }






void loop() {

  StartState = digitalRead(StartSwitch);
//        Serial.print( "StartState:  ");
//        Serial.println(StartState);
//        Serial.print( "SensorState1:  ");
//        Serial.println(SensorState1);
        Serial.print( "SensorFinish1:  ");
        Serial.println(SensorFinish1);
                   
  // check if StartSwitch is pressed/closed, if it is, the gate is up, sensorState is HIGH, or 1:   
  if (StartState == HIGH) {   
    lcd.setCursor(0, 1);
    lcd.print("Ready"); 
    lcd.setCursor(10, 1);
    lcd.print("Ready");
    StartTime = micros() ;
    digitalWrite(LaneLED1, HIGH);     // turn Lane 1 LED on
    digitalWrite(LaneLED2, HIGH);     // turn Lane 2 LED on
    digitalWrite(BoardLED, HIGH);     // turn Board LED on
    SensorFinish1 = 0;
    SensorFinish2 = false;
//    Serial.print( "StartTime:  ");
//    Serial.println(StartTime,4);
    digitalWrite(LaneLED1, LOW);      // turn LED off:
    digitalWrite(LaneLED2, LOW);      // turn LED off:
    }
    
    else {               // Start has opened, its race mode, start watching for finish

      SensorState1 = digitalRead(SensorPin1);      // read the state of the IR value
      SensorState2 = digitalRead(SensorPin2);      // read the state of the IR value
      TimeLane1 = micros() ;                 // start reading time to know when sensor is tripped
      TimeLane2 = micros() ;                 // start reading time to know when sensor is tripped
      if (SensorFinish1 = 0) {               // if car hasn't finished, show time
        digitalWrite(LaneLED1, LOW);     // turn Lane 1 LED off
        digitalWrite(LaneLED2, LOW);     // turn Lane 2 LED off
        digitalWrite(BoardLED, LOW);     // turn Board LED off
        FinishTime1 = TimeLane1 - StartTime;
        FinishTime1 = FinishTime1 / 1000000;
        lcd.setCursor(0, 1);           // Set display position: first block, second row
        lcd.print(FinishTime1, 4);       // Display time
      }
      // check if the sensor beam 1 is broken, if it is, the sensorState is LOW:
      if (SensorState1 == LOW) {
        SensorFinish1 = 1;                  // Set variable to 1 that car has finished, to show final time
           digitalWrite(LaneLED1, HIGH);       // turn Lane 1 LED on [insert check for finisher]
        FinishTime1 = TimeLane1 - StartTime;
        FinishTime1 = FinishTime1 / 1000000;
        lcd.setCursor(0, 1);           // Set display position: first block, second row
        lcd.print(FinishTime1, 4);       // Display time
        digitalWrite(LaneLED1, HIGH);  // turn LED on:
//        Serial.print( "FinishTime1:  ");
//        Serial.println(FinishTime1,4);
//        Serial.print( "StartTime:  ");
//        Serial.println(StartTime,4);
//        Serial.print( "Micros:  ");
//        Serial.println(micros(),4);
        }
        else {
        // turn LED off:
        digitalWrite(LaneLED1, LOW);      // turn LED off:
      }
  
      // check if the sensor beam 2 is broken, if it is, the sensorState is LOW:
      if (SensorState2== LOW) {     
        digitalWrite(LaneLED2, HIGH);     // turn LED on:
      } 
      else {
        digitalWrite(LaneLED2, LOW);      // turn LED off:
      }

    }
}

Rather than check when pins are low (or high), try checking when they become low (or high)

Take a look at the state change detection example in the IDE's worked examples.

(You've got too much code.
Global variables are all zero/null/false, unless assigned otherwise, and int variables shouldn't really be assigned true/false.)

'=' is not the same as '=='. You did it correct in the other if statements, so maybe a typo?

Appreciate the pointer! Yes, I know my code is probably ugly and a mess. Just trying to get this to work :wink:

Thanks so much! I'll fix that and try checking it. :slightly_smiling_face:

I moved your topic to an appropriate forum category @enginerd42.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Thanks Pert. I did try looking through the guides, and thought I had selected a category. Apologies, but thanks for your help.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.