1

I have a school project, where I'm going to make a traffic light system on a pls. I want to try it out on an Arduino first, just to get the hang of it.

To make it a little bit more advanced than I have found on the internet, I want to make the lights go in "emergency mode" if the green lights in both directions are on at the same time. It will then go into a yellow blink.

My idea is something like this:

// Yellow blinking if fault in the system or operator activates it
int killState = digitalRead(Kill);
int resState = digitalRead(Res);
if (killState == HIGH /*|| greenlights in both directions */) {
 while (resState == LOW;) { // Yellow blink as long as system is not reset
  BLINK();
 }
}

I guess I need a variable or two that tells what state the lights are in, but I'm stuck. Any good solutions out there?

Another thing is the reset button. I haven't put much effort in it, but I want it to go into the starting point.

The link to the complete script can be found here.

1

For starters, take a close look at this line:

while (resState == LOW;)

That is not the correct syntax. But the real issue is how is this while loop terminated? The value is never set again?

Cheers!

  • I guessed so. As I said, I haven't put much effort in it yet. I need to take one step at a time. Thanks for the quick reply, though! The idea is that the state will not change until the reset button is activated. And the ' ; ' was a type-o. I am new to 'while' and 'for' loops, but i think I'm starting to get the hang on 'if' and 'else' criterias. – Mikael Brun Feb 12 '16 at 10:54
1

You could do something simple to test if both red and green are on, eg.

if (digitalRead (RED_PIN) == HIGH && digitalRead (GREEN_PIN) == HIGH)
  {
  digitalWrite (RED_PIN, LOW);  // turn off red
  digitalWrite (GREEN_PIN, LOW);  // turn off green
  while (true)
    {
    digitalWrite (YELLOW_PIN, HIGH);
    delay (500);
    digitalWrite (YELLOW_PIN, LOW);
    delay (500);
    }  // end of while
  }  // end of it

If you have other questions please post your code and not a snippet - however I suggest you try to get further on your own. This is obviously an exercise, and the more you try to work it out yourself the more you will learn.

  • Thanks! That was a good idea. I will add this to the script. I will add the full code next time. I added it as a link, though. – Mikael Brun Feb 13 '16 at 10:34
  • What is it that is true in the while (true) part? – Mikael Brun Feb 13 '16 at 11:59
  • true is true. That particular loop loops indefinitely. In other words, if there is some failure in the traffic lights, it will flash yellow ... forever. (Kind of a scary thought, eh?) – Nick Gammon Feb 14 '16 at 5:59
  • I could have written while (5 == 5) - it just means to do something forever. – Nick Gammon Feb 14 '16 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.