[SORTED] Advice on logic of sketch - traffic lights

I GOT MY GEAR!!!! :stuck_out_tongue_closed_eyes:

Anyway, been working through projects and I'm busy with a traffic light project which has two traffic lights (say North and South), and one pedestrian lights. In all cases, its just red and green.

Currently I have done the traffic lights (although I will now work on shorting the code) working. Logic:

North Red on
South Green on
delay 10 seconds
South Red blinks 3 times
South Green off
North Red off
delay(50)
South Red on
North Green on
delay 10 seconds
flop over to original condition

Now the problem I'm struggling with, the logic - I don't want code, just the logic.

I want to press a button.
Firstly check that traffic light condition (North Green or South Green) has been such for 5 seconds
If yes
Turn light to Solid (North & South is Red) - Which ever is green needs to go to Red in the usual way of 3 blinks
Turn pedestrian light to green
5 seconds
Return to original condition (Pedestrian light red).

Does that make sense? The traffic lights needs to engage a condition where both traffic lights are red without changing the current condition.

Thank you!

EDIT: PS, the code that I will now make shorter using, what is it called now, the count where you define the value of "x=0,x<3,x++"

int carRedn=13;                  
int carGreenn=12;                
int carReds=11;                  
int carGreens=10;

void setup(){
  pinMode(carRedn,OUTPUT);
  pinMode(carGreenn,OUTPUT);
  pinMode(carReds,OUTPUT);
  pinMode(carGreens,OUTPUT);
}

void loop(){
  digitalWrite(carRedn,HIGH);
  digitalWrite(carGreens,HIGH);
  delay(50);
  digitalWrite(carReds,LOW);
  digitalWrite(carGreenn,LOW);
  delay(10000);
  digitalWrite(carReds,HIGH);
  delay(350);
  digitalWrite(carReds,LOW);
  delay(350);
  digitalWrite(carReds,HIGH);
  delay(350);
  digitalWrite(carReds,LOW);
  delay(350);
  digitalWrite(carReds,HIGH);
  delay(350);
  digitalWrite(carReds,LOW);
  delay(350);
  digitalWrite(carRedn,LOW);
  digitalWrite(carGreens,LOW);
  delay(50);
  digitalWrite(carReds,HIGH);
  digitalWrite(carGreenn,HIGH);
  delay(10000);
  digitalWrite(carRedn,HIGH);
  delay(350);
  digitalWrite(carRedn,LOW);
  delay(350);
  digitalWrite(carRedn,HIGH);
  delay(350);
  digitalWrite(carRedn,LOW);
  delay(350);
  digitalWrite(carRedn,HIGH);
  delay(350);
  digitalWrite(carRedn,LOW);
  delay(350);
}

I would do something like this.

typedef struct action {
   boolean red;
   boolean green;
   long  time;
   boolean both_on_flag;
};

#define ON HIGH
#define OFF LOW

action actions_normal [] {
   {ON, OFF, 10000, true},
   {ON, ON, 1000, true},
   {OFF, OF, 5000, true}
// etc etc
}

action actions_after_button [] {
   {ON, OFF, 10000, true}, //  both_on_flag irrelevant here
   {ON, ON, 1000, true},
   {OFF, OF, 5000, true}
// etc etc
}

const int N_ACTIONS = sizeof (actions) / sizeof (action);
struct action * current_action = action_normal;

loop () {

   current_action = actions_normal;

   for (int i = 0; i < N_ACTIONS; i++) {
         digitalWrite (redPin, actions[i].red);
         digitalWrite (greenPin, actions[i].green);
         delay (actions[i].time);
         if (actions[i].both_on_flag)
              both_on_start = millis();
   }
}

Now to have a different set of actions for after pressing a button you could probably define another action table and use that if the two lights have been on for > 5 seconds.

if (button_pressed && (millis() - both_on_start) > 5000) {
   current_action = actions_after_button;
}

This won't work as is or even compile but I think the general idea is sound if you feel like following the logic.


Rob

I think it helps writing out a problem. This is my idea of what I think the logic will be, please advise:
Start with a condition:
Pin 13 HIGH //North Red
Pin 12 LOW //North Green
Pin 11 LOW //South Red
Pin 10 HIGH //South Green
North and South will have their own program. North will check the condition of South, and vice versa.
If South sees Pin 13 is HIGH for longer than 10 seconds, it starts to blink Pin 11.
Then it sets PIN 10 to LOW, and Pin 13 to LOW.
Delay (50)
Set Pin 12 to HIGH and Pin 11 to HIGH.
Maybe this will only be one program....
Edit the above, a program checks condition of Pin 13 and runs as described.
Ad to the IF statement.
If Pin 13 is High for more than 10 sec, change condition.
AHhh, need to define conditions to call.

(Sorry, I'm letting you see inside my brain here)

Define conditions
There will be three conditions of Pin 13 to Pin 8 which are:

1
Pin 13 HIGH (red - Traffic Sorth)
Pin 12 LOW (green)
Pin 11 LOW (red - Traffic South)
Pin 10 HIGH (green)
Pin 9 HIGH (red - pedestrian)
Pin 8 LOW (green - pedestrian)

2
Pin 13 LOW
Pin 12 HIGH
Pin 11 HIGH
Pin 10 LOW
Pin 9 HIGH
Pin 8 LOW

3
Pin 13 HIGH
Pin 12 LOW
Pin 11 HIGH
Pin 10 LOW
Pin 9 LOW
Pin 8 HIGH

If condition 1 for longer than 10 seconds, change to Condition 2. If Condition 2 for longer than 10 seconds, change to condition 1. If condition 3 for longer than 5 seconds, change to condition 1.

And one last condition which regards the pushing of the button.
If condition 1 change to condition 3
If condition 2 change to condition 3

Sorry for the interruption. Does this look better.

Hey Graynomad. Thanks for the code, I will try and follow it cause I've only started playing yesterday morning.

Inprogress:
I GOT MY GEAR!!!!

You’re a ventriloquist.

I changed my logic a bit and wrote sections of codes which I call "programs".

All works fine except for the change from the two default programs to another program with the use of a pull-down push button. I have written a little code section just to check if the pull-down push button works as it should, and all checks out fine. What am I doing wrong? I suspect its in my condition comparison where I compare the state of the INPUT pin with a program condition, in other words I am comparing a program with a INPUT. I tried the Boolean function but no luck there either yet.

Any ideas?

Some of the coding is still "longhand" but I will shorten that later once I get the sketch to run like it should.

int pinLed=13;                   //Verify that button works
int carRedn=12;                  //Traffic North Red
int carGreenn=11;                //Traffic South Green
int carReds=10;                  //Traffic North Red
int carGreens=9;                 //Traffic South Green
int pedRed=8;                    //Pedestrian Green
int pedGreen=7;                  //Pedestrian Green
int button=6;                    //Pedestrian button




void setup(){
  pinMode(pinLed,OUTPUT);
  pinMode(carRedn,OUTPUT);
  pinMode(carGreenn,OUTPUT);
  pinMode(carReds,OUTPUT);
  pinMode(carGreens,OUTPUT);
  pinMode(pedRed,OUTPUT);
  pinMode(pedGreen,OUTPUT);
  pinMode(button,INPUT);
  northGo();
  }
void loop(){
  int state=digitalRead(button);     //Reads the condition of input
    if(state == HIGH&&(northGo)){    //Compares button condition with running program
      digitalWrite(pinLed,HIGH);     //Button check
      delay(250);
      digitalWrite(pinLed,LOW);
        northpedgo();                //Since northGo is running, calls cancel program northpedgo
    }
    if(state == HIGH&&(southGo)){    //Compares button condition with running program
      digitalWrite(pinLed,HIGH);     //Button check
      delay(250);
      digitalWrite(pinLed,LOW);
        southpedgo();                //Since southGo is running, calls cancel program southpedgo
    }
    if(state == HIGH&&(allRed)){    //Compares button condition with running program
      digitalWrite(pinLed,HIGH);    //Button check
      delay(250);
      digitalWrite(pinLed,LOW);
        pedGo();                    //Since allRed is running, calls cancel program pedGo
    }
  if(northGo){
    northsouth();
    }
  if(southGo){
    southnorth();
    }
}
int allRed(){                   //Everyone stops
  digitalWrite(carRedn,HIGH);
  digitalWrite(carGreenn,LOW);
  digitalWrite(carReds,HIGH);
  digitalWrite(carGreens,LOW);
  digitalWrite(pedRed,HIGH);
  digitalWrite(pedGreen,LOW);
  delay(2000);
}
int northGo(){                  //Northen traffic go, South & Pedestrian stop
  digitalWrite(carRedn,LOW);
  digitalWrite(carGreenn,HIGH);
  digitalWrite(carReds,HIGH);
  digitalWrite(carGreens,LOW);
  digitalWrite(pedRed,HIGH);
  digitalWrite(pedGreen,LOW);
  delay(10000);
  northsouth();
  allRed();
}
int southGo(){                 //Southern traffic go, North & Pedestrian stop
  digitalWrite(carRedn,HIGH);
  digitalWrite(carGreenn,LOW);
  digitalWrite(carReds,LOW);
  digitalWrite(carGreens,HIGH);
  digitalWrite(pedRed,HIGH);
  digitalWrite(pedGreen,LOW);
  delay(10000);
  southnorth();
  allRed();
}
int pedGo(){                   //Traffic stops, Pedestrians go
  digitalWrite(carRedn,HIGH);
  digitalWrite(carGreenn,LOW);
  digitalWrite(carReds,HIGH);
  digitalWrite(carGreens,LOW);
  digitalWrite(pedRed,LOW);
  digitalWrite(pedGreen,HIGH);
  pedStop();
  allRed();
  northGo();
}
int pedStop(){                 //Stops pedestrians, returns to NorthGo
  digitalWrite(pedRed,HIGH);
  delay(250);
  digitalWrite(pedRed,LOW);
  delay(250);
  digitalWrite(pedRed,HIGH);
  delay(250);
  digitalWrite(pedRed,LOW);
  delay(250);
  digitalWrite(pedRed,HIGH);
  delay(250);
  digitalWrite(pedRed,LOW);
  delay(250);
  digitalWrite(pedRed,HIGH);
  delay(50);
  allRed();
  northGo();
}
int northsouth(){              //Changes traffic from North to South
  digitalWrite(carRedn,HIGH);
  delay(250);
  digitalWrite(carRedn,LOW);
  delay(250);
  digitalWrite(carRedn,HIGH);
  delay(250);
  digitalWrite(carRedn,LOW);
  delay(250);
  digitalWrite(carRedn,HIGH);
  delay(250);
  digitalWrite(carRedn,LOW);
  delay(250);
  digitalWrite(carRedn,HIGH);
  delay(50);
  allRed();
  southGo();
}
int southnorth(){              //Changes traffic from South to North
  digitalWrite(carReds,HIGH);
  delay(250);
  digitalWrite(carReds,LOW);
  delay(250);
  digitalWrite(carReds,HIGH);
  delay(250);
  digitalWrite(carReds,LOW);
  delay(250);
  digitalWrite(carReds,HIGH);
  delay(250);
  digitalWrite(carReds,LOW);
  delay(250);
  digitalWrite(carReds,HIGH);
  delay(50);
  allRed();
  northGo();
}
int northpedgo(){                  //Stops all traffic from NorthGo, pedestrian go
  digitalWrite(carRedn,HIGH);
  delay(250);
  digitalWrite(carRedn,LOW);
  delay(250);
  digitalWrite(carRedn,HIGH);
  delay(250);
  digitalWrite(carRedn,LOW);
  delay(250);
  digitalWrite(carRedn,HIGH);
  delay(250);
  digitalWrite(carRedn,LOW);
  delay(250);
  digitalWrite(carRedn,HIGH);
  delay(50);
  allRed();
  pedGo();
}
int southpedgo(){                  //Stops all traffic from SouthGo, pedestrian go
  digitalWrite(carReds,HIGH);
  delay(250);
  digitalWrite(carReds,LOW);
  delay(250);
  digitalWrite(carReds,HIGH);
  delay(250);
  digitalWrite(carReds,LOW);
  delay(250);
  digitalWrite(carReds,HIGH);
  delay(250);
  digitalWrite(carReds,LOW);
  delay(250);
  digitalWrite(carReds,HIGH);
  delay(50);
  allRed();
  pedGo();
}