if statement problem

Hi everyone,

I build a simple traffic light for my son's wooden train toys, and he asked me if i wanted to make a railroad crossing with lights.

So i started to build the sign and its electronics.
I also made the sketch for the traffic light and the crossing.
Here is where it went wrong.

If i only use the traffic light everything is fine, as soon i put this second "if" statement, it just won't work.
don't worry about the wiring, cause it works stand alone.

If i put the second "if" (crossing) at first, then it will run but the traffic light won't.

So that makes me think i did something wrong in my code.
is there anyone who could help me ?

int red = 13;
int orange = 11;
int green = 9;
int button = 7;
int buttoncrossing = 2;
int ledcrossing1 = 5;
int ledcrossing2 = 4;
int val = 0;
int val2 = 0;

void setup() {
  
pinMode(red, OUTPUT);  
pinMode(green, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(button, INPUT);
pinMode(ledcrossing1, OUTPUT);
pinMode(ledcrossing2, OUTPUT);
pinMode(buttoncrossing, INPUT);
}

void loop() {

val = digitalRead(button);  
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(orange, LOW);



 if(val == HIGH){
  digitalWrite(green, HIGH);
  digitalWrite(red, LOW);
  delay(5000);
  digitalWrite(green, LOW); 
  digitalWrite(orange, HIGH);
  delay(2000);
 }
 


val2 = digitalRead(buttoncrossing);
digitalWrite(ledcrossing1, LOW);
digitalWrite(ledcrossing2, LOW);

  if(val2 == HIGH){
    digitalWrite(ledcrossing1, HIGH);
    delay(500);
    digitalWrite(ledcrossing1, LOW);
    delay(500);
    digitalWrite(ledcrossing2, HIGH);
    delay(500);
    digitalWrite(ledcrossing2, LOW);
    delay(500);
    digitalWrite(ledcrossing1, HIGH);
    delay(500);
    digitalWrite(ledcrossing1, LOW);
    delay(500);
    digitalWrite(ledcrossing2, HIGH);
    delay(500);
    digitalWrite(ledcrossing2, LOW);
    delay(500);
    
  }
  
 }

Show us the code that does not work.

Show us a good image of your wiring.

delay() is a blocking function.

Have you seen how to use millis() to get a non blocking delay?

Ok, i made a quick drawing how i wired it.(see attachment)

The code is down below.

no, i have never heard of mills()??

int red = 13;
int orange = 11;
int green = 9;
int button = 7;
int buttoncrossing = 2;
int ledcrossing1 = 5;
int ledcrossing2 = 4;
int val = 0;
int val2 = 0;

void setup() {
  
pinMode(red, OUTPUT);  
pinMode(green, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(button, INPUT);
pinMode(ledcrossing1, OUTPUT);
pinMode(ledcrossing2, OUTPUT);
pinMode(buttoncrossing, INPUT);
}

void loop() {

val = digitalRead(button);  
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(orange, LOW);



 if(val == HIGH){
  digitalWrite(green, HIGH);
  digitalWrite(rood, LOW);
  delay(5000);
  digitalWrite(green, LOW); 
  digitalWrite(orange, HIGH);
  delay(2000);
 }
 


val2 = digitalRead(buttoncrossing);
digitalWrite(ledcrossing1, LOW);
digitalWrite(ledcrossing2, LOW);

  if(val2 == HIGH){
    digitalWrite(ledcrossing1, HIGH);
    delay(500);
    digitalWrite(ledcrossing1, LOW);
    delay(500);
    digitalWrite(ledcrossing2, HIGH);
    delay(500);
    digitalWrite(ledcrossing2, LOW);
    delay(500);
    digitalWrite(ledcrossing1, HIGH);
    delay(500);
    digitalWrite(ledcrossing1, LOW);
    delay(500);
    digitalWrite(ledcrossing2, HIGH);
    delay(500);
    digitalWrite(ledcrossing2, LOW);
    delay(500);
    
  }
  
 }

Read the discussion here, in your own words tell us what the millis() function does.

EDIT:

digitalWrite(rood, LOW); :wink:

Do you know what a function is?

.

yeah im sorry, i had my sketch in dutch at first so i needed to translate it to english before posting it.
So it was easier for you guys.
But i forgot that one.

Do you know what a function is?

yes i do, and i have read you're link, but i don't know where i should place it in my sketch.
Should i change my delay for time?

I will put a small sketch together and get back to you.

Edit:
Are you wanting to learn or do you want a final sketch?

I want to learn it, so can do it myself.

What happens in this sketch?

#define LEDon  HIGH
#define LEDoff LOW

#define PUSHED HIGH

byte red              = 13;
byte orange           = 11;
byte green            = 9;
byte button           = 7;

byte StateTraffic;

byte lastTrafficSwitch;

unsigned long trafficMillis;

//**************************************************************
void setup()
{
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(orange, OUTPUT);
  
  pinMode(button, INPUT_PULLUP);

  lastTrafficSwitch = digitalRead(button);

  digitalWrite(red, LEDon);
  digitalWrite(green, LEDoff);
  digitalWrite(orange, LEDoff);

}//END of:               s e t u  p ( )

//**************************************************************
void loop()
{
  checkSwitches();

  traffic();

}//END of:                l o o p ( )

//                 c h e c k S w i t c h e s ( )
//**************************************************************
void checkSwitches()
{
  byte switchState = digitalRead(button);

  if (lastTrafficSwitch != switchState)
  {
    lastTrafficSwitch = switchState;

    if (switchState == PUSHED)
    {
      StateTraffic = 1;
    }

  }

}//END of:         c h e c k S w i t c h e s ( )

//                       t r a f f i c ( )
//**************************************************************
void traffic()
{
  switch (StateTraffic)
  {
    //**************************
    case 0:

      break;

    //**************************
    case 1:
      digitalWrite(green, LEDon);
      digitalWrite(red, LEDoff);
      digitalWrite(orange, LEDoff);

      StateTraffic = 2;
      trafficMillis = millis();

      break;

    //**************************
    case 2:
      if (millis() - trafficMillis >= 5000)
      {
        digitalWrite(green, LEDoff);
        digitalWrite(red, LEDoff);
        digitalWrite(orange, LEDon);

        StateTraffic = 3;
        trafficMillis = millis();
      }
      break;

    //**************************
    case 3:
      if (millis() - trafficMillis >= 2000)
      {
        digitalWrite(red, LEDon);
        digitalWrite(green, LEDoff);
        digitalWrite(orange, LEDoff);

        StateTraffic = 0;
      }

      break;

  }//END of:  switch/case

}//END of                t r a f f i c ( )

//**************************************************************

the traffic lights are working.

But i need to study you're code to understand it so maybe i can make my own for the crossing sign and add it to it.

For now i really don't understand that code but i wil try to figure it out.

millis() returns the current time in milliseconds.
Let's say millis() returned 10000.

trafficMilli = millis(); >>>---> trafficMillis captures the time at that instant.
Therefore trafficMills is now 10000.

Later let say you do the calculation below where millis() is now 15000
millis() - trafficMillis = ? ? ? ? ?
What will the subtraction be equal to?

Edit:
Add another LED to pin 3 of the Arduino. Wire this LED same way as the other LEDs.

Try this sketch, what do you see?

#define LEDon  HIGH    //wired so a HIGH on the pin turns the LED on
#define LEDoff LOW

//#define LEDon  LOW   //wired so a LOW on the pin turns the LED on
//#define LEDoff HIGH

#define PUSHED HIGH    //wired so a push gives a HIGH on the pin
byte BlockingLED      = 3;
byte red              = 13;
byte orange           = 11;
byte green            = 9;
byte button           = 7;

byte StateTraffic;

byte lastTrafficSwitch;

unsigned long trafficMillis;

unsigned long blockingMillis;

//**************************************************************
void setup()
{
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(orange, OUTPUT);

  pinMode(BlockingLED, OUTPUT);

  pinMode(button, INPUT_PULLUP);

  lastTrafficSwitch = digitalRead(button);

  digitalWrite(red, LEDon);
  digitalWrite(green, LEDoff);
  digitalWrite(orange, LEDoff);

}//END of:               s e t u  p ( )

//**************************************************************
void loop()
{
  //**************************
  //Check for blocking code, toggle the LED on pin 2 every 1/2 second
  if (millis() - blockingMillis > 500)
  {
    blockingMillis = millis();
    digitalWrite(BlockingLED, !digitalRead(BlockingLED));
  }

  //**************************
  checkSwitches();

  //**************************
  traffic();


}//END of:                l o o p ( )




//                 c h e c k S w i t c h e s ( )
//**************************************************************
void checkSwitches()
{
  byte switchState = digitalRead(button);

  if (lastTrafficSwitch != switchState)
  {
    lastTrafficSwitch = switchState;

    if (switchState == PUSHED)
    {
      StateTraffic = 1;
    }

  }

}//END of:         c h e c k S w i t c h e s ( )

//                       t r a f f i c ( )
//**************************************************************
void traffic()
{
  switch (StateTraffic)
  {
    //**************************
    case 0:

      break;

    //**************************
    case 1:
      digitalWrite(red, LEDoff);
      digitalWrite(green, LEDon);
      digitalWrite(orange, LEDoff);

      StateTraffic = 2;
      trafficMillis = millis();

      break;

    //**************************
    case 2:
      if (millis() - trafficMillis >= 5000)
      {
        digitalWrite(red, LEDoff);
        digitalWrite(green, LEDoff);
        digitalWrite(orange, LEDon);

        StateTraffic = 3;
        trafficMillis = millis();
      }
      break;

    //**************************
    case 3:
      if (millis() - trafficMillis >= 2000)
      {
        digitalWrite(red, LEDon);
        digitalWrite(green, LEDoff);
        digitalWrite(orange, LEDoff);

        StateTraffic = 0;
      }

      break;

  }//END of:  switch/case

}//END of                t r a f f i c ( )

//**************************************************************

Here is a start.

Lines that have <-----<<<<< were added.

You still need to finish the last part of the additions. :wink:

If you don't ask questions you will not learn.

//**************************************************************
// StateMachineTrafficCrossingLights.ino
//
// YY/MM/DD   Version   Comments
// 18/01/14   1.01
//
//
//**************************************************************
//
//wired so a HIGH on the pin turns the LED on
#define LEDon  HIGH
#define LEDoff LOW

//wired so a LOW on the pin turns the LED on
//#define LEDon  LOW
//#define LEDoff HIGH

#define PUSHED HIGH     //wired so a push gives a HIGH on the pin

//**************************************************************

byte BlockingLED      = 3;

byte red              = 13;
byte orange           = 11;
byte green            = 9;
byte button           = 7;

int ledcrossing1      = 5;    // <-----<<<<<
int ledcrossing2      = 4;    // <-----<<<<<
int buttoncrossing    = 2;    // <-----<<<<<

byte StateTraffic;
byte StateCrossing;           // <-----<<<<<

byte lastTrafficSwitch;
byte lastCrossingSwitch;      // <-----<<<<<

unsigned long trafficMillis;
unsigned long crossingMillis; // <-----<<<<<
unsigned long blockingMillis;
unsigned long switchMillis;

//**************************************************************
void setup()
{
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(orange, OUTPUT);
  pinMode(ledcrossing1, OUTPUT);
  pinMode(ledcrossing2, OUTPUT);

  pinMode(BlockingLED, OUTPUT);

  pinMode(button, INPUT);
  pinMode(buttoncrossing, INPUT);

  lastTrafficSwitch = digitalRead(button);
  lastCrossingSwitch = digitalRead(button); // <-----<<<<<

  digitalWrite(red, LEDon);
  digitalWrite(green, LEDoff);
  digitalWrite(orange, LEDoff);

  digitalWrite(ledcrossing1, LEDoff);      // <-----<<<<<
  digitalWrite(ledcrossing2, LEDoff);      // <-----<<<<<

}//END of:               s e t u  p ( )

//**************************************************************
void loop()
{
  //**************************
  //Check for blocking code, toggle the LED on pin 2 every 1/2 second
  if (millis() - blockingMillis > 500)
  {
    blockingMillis = millis();
    digitalWrite(BlockingLED, !digitalRead(BlockingLED));
  }

  //**************************
  //is it time to check the switches?
  if (millis() - switchMillis >= 50)
  {
    switchMillis = millis();
    checkSwitches();
  }

  //**************************
  traffic();

  //**************************
  crossing(); //                           <-----<<<<<

}//END of:                l o o p ( )




//                 c h e c k S w i t c h e s ( )
//**************************************************************
void checkSwitches()
{
  //Handle the Traffic switch reading
  //**************************
  byte switchState = digitalRead(button);

  if (lastTrafficSwitch != switchState)
  {
    lastTrafficSwitch = switchState;

    if (switchState == PUSHED)
    {
      StateTraffic = 1;
    }

  }//END of: if (lastTrafficSwitch != switchState)


  //Handle the Crossing switch reading                 <-----<<<<<
  //**************************
  switchState = digitalRead(buttoncrossing);

  if (lastCrossingSwitch != switchState)
  {
    lastCrossingSwitch = switchState;

    if (switchState == PUSHED)
    {
      StateCrossing = 1;
    }

  }//END of: if (lastCrossingSwitch != switchState)    <-----<<<<<

}//END of:         c h e c k S w i t c h e s ( )

//                       t r a f f i c ( )
//**************************************************************
void traffic()
{
  switch (StateTraffic)
  {
    //**************************
    case 0:

      break;

    //**************************
    case 1:
      digitalWrite(red, LEDoff);
      digitalWrite(green, LEDon);
      digitalWrite(orange, LEDoff);

      StateTraffic = 2;
      trafficMillis = millis();

      break;

    //**************************
    case 2:
      if (millis() - trafficMillis >= 5000)
      {
        digitalWrite(red, LEDoff);
        digitalWrite(green, LEDoff);
        digitalWrite(orange, LEDon);

        StateTraffic = 3;
        trafficMillis = millis();
      }
      break;

    //**************************
    case 3:
      if (millis() - trafficMillis >= 2000)
      {
        digitalWrite(red, LEDon);
        digitalWrite(green, LEDoff);
        digitalWrite(orange, LEDoff);

        StateTraffic = 0;
      }

      break;

  }//END of:  switch/case

}//END of                t r a f f i c ( )

//                      c r o s s i n g ( )               <-----<<<<<
//**************************************************************
void crossing()
{

  //You add some code here

}//END of:              c r o s s i n g ( )               <-----<<<<<


//**************************************************************

EDIT:
Updated sketch

Im sorry for this late response, but i think we live in a different timezone.

What will the subtraction be equal to?

this would be 5000??

I will update the code and add the extra LED to it asap.

Yes, the subtraction will equal 5000.

Try to follow the comments below:

//when we use the code below, it 'will be' satisfied i.e. the subtraction will = 5000
if(millis()15000 - trafficMillis10000 >= 5000)
{
//the first thing we do is 'reinitialize' trafficMillis so we can handle a new timing comparison in the next loop.
trafficMillis = millis()15000; //since millis() is 15000, trafficMillis is now 15000

//Maybe we now toggle the state of a variable
pinState = !pinState;

//we might then send this variable to an output pin. This will in essence toggle a LED connected to that pin
digitalWrite(10, pinstate);

}

The next time we execute the above code, trafficMillis is 15000 and millis() might be 15010.
(remember, millis() returns current time in milliseconds, which keeps increasing.)
The subtraction will give only 10 which >= is 'not' satisfied.
millis()15010 - trafficMillis15000 >= 5000

Not until millis() is 20000 will we once again meet the >= comparison.
millis()20000 - trafficMillis15000 >= 5000

Does the above make sense?

Note:
I gave you some help in a new sketch attachment post #13
This will help you in adding 'Crossing' to your project.

Are you from Holland?
Do you know what these are?

For now i can follow Youre explaination.
I Will start playing with it so i can get used to it and practice myself, thank you for now.
If i run info troubles i will let you know.

O and Yes ik from Holland and these cookies... they are called “stroopwafels”
Have you Ever tried Them?

MYH1990NL:
For now i can follow Youre explaination.
I Will start playing with it so i can get used to it and practice myself, thank you for now.
If i run info troubles i will let you know.

O and Yes ik from Holland and these cookies... they are called “stroopwafels”
Have you Ever tried Them?

I just received 2 cases of stroopwafels from Holland :slight_smile: :slight_smile: :slight_smile:
They are great ! !

EDIT:
If you run into problems I can post the final sketch for you. :slight_smile:

larryd:
They are great ! !

Cover your hot coffee with one, and leave it on there for ~20sec.
Then flip it, and leave for another 10sec.
Warme stroopwafels.
Yummmm.