Pages: [1]   Go Down
Author Topic: Level Crossing Program  (Read 556 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,
Fairly new to the world of Arduino and I am trying to create a program that flashes some LED's in a level crossing sequence until they receive the same input that was the trigger. I have written a code using bits I have learnt but it doesn't work between the while loop and the termination sequence for some reason I can't understand. One red is supposed to come on for 500 milliseconds then switch over to the other for 500 milliseconds and this carries on until the sequence is terminated. Could you please troubleshoot it for me and explain the problem.
Thanks
Owen


Code:
const int R1 = 12; // 1st Red light
const int A = 11; // Amber Light
const int R2 = 10; // 2nd Red light
const int trigger = 4; // Trigger

int triggerState = 0;
int running = 0;
int long interval = 500;
long previousMillis = 0;
int R1State = HIGH;

void setup() {
  pinMode(R1, OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(trigger, INPUT);
}

void loop() {
  triggerState = digitalRead(trigger);
  if(triggerState == HIGH && running == 0){
      int running = 1; //To prevent the While loop from triggering early
      digitalWrite(A, HIGH); //Gives a constant Amber
      delay(5000);
      digitalWrite(A, LOW);
      digitalWrite(R1, HIGH);
      digitalWrite(R2, HIGH); //Both Red lights come on
      delay(500);
      digitalWrite(R1, LOW); // One switches off
      delay(500);
      triggerState = digitalRead(trigger);
        while(triggerState == LOW && running == 1)  {
          digitalWrite(R1, HIGH);
          digitalWrite(R2, LOW);
         
          unsigned long currentMillis = millis(); // Timer used instead of delay to allow input to be read at the same time
         
          if(currentMillis - previousMillis > interval) {
            previousMillis = currentMillis;
           
            if (R1State == HIGH){ // Switches lights over
              digitalWrite(R1, LOW);
              digitalWrite(R2, HIGH);
              int R1State = LOW;
            }
            else {
              digitalWrite(R1, HIGH); // Switches lights over
              digitalWrite(R2, LOW);
              int R1State = HIGH;
            }
          }
          else {
            triggerState = digitalRead(trigger); // Terminates program and switches off lights if the trigger input is detected
            if (triggerState == HIGH){
              digitalWrite (R1, LOW);
              digitalWrite (R2, LOW);
              int R1State = LOW;
              delay(5000);
              int running = 0;
              break;
            }
          }
        }
  }
}
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 72
Posts: 1642
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't need to redeclare variables each time you use them
Code:
if (R1State == HIGH){ // Switches lights over
              digitalWrite(R1, LOW);
              digitalWrite(R2, HIGH);
              int R1State = LOW; //Re declared the variable, which means it is a different one to the if clause
            }
This will suffice
Code:
if (R1State == HIGH){ // Switches lights over
              digitalWrite(R1, LOW);
              digitalWrite(R2, HIGH);
              R1State = LOW; //no longer has an 'int' declaration
            }
Logged

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I have changed it and won't do that again but the program still doesn't work as it is supposed to. If I look hard at one of the LED's I can see it blinks very slightly (R2) but R1 stays constantly on if that is any help?
Owen
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post code.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Leeds, UK
Offline Offline
Edison Member
*
Karma: 72
Posts: 1642
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        while(triggerState == LOW && running == 1)  {
          digitalWrite(R1, HIGH); //This is not needed
          digitalWrite(R2, LOW);  //This is not needed
         
          unsigned long currentMillis = millis(); // Timer used instead of delay to allow input to be read at the same time
         
          if(currentMillis - previousMillis > interval) {
            previousMillis = currentMillis;
           
            if (R1State == HIGH){ // Switches lights over
              digitalWrite(R1, LOW);
              digitalWrite(R2, HIGH);
              int R1State = LOW;
            }
            else {
              digitalWrite(R1, HIGH); // Switches lights over
              digitalWrite(R2, LOW);
              int R1State = HIGH;
            }

You don't need those two at the top there as the leds are being changed in the if statements.
Logged

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot Tom, I removed those two lines of code and it now works perfectly  smiley-grin
Owen
Logged

Pages: [1]   Go Up
Jump to: