arduino college project

Need help with a bit of code ye Arduino geniuses. I have a pedestrian crossing system.Normal 3 traffic lights and 2 pedestrian lights. Starts off with your green traffic light on and red pedestrian on.When pedestrian button is pressed,traffic goes orange and then red while pedestrian light goes from red to green. The pedestrian is now crossing and the duration of this green light is 6 seconds. if the pedestrian reaches the other side before the 6 second duration of the light he hits another pedestrian button to signify he has reached the other side of the road and the simulation goes back to the start. HEEEEELLLLLLLLPPPPPPPP!!!!

(deleted)

There is no question so how do you expect anyone to help you. Do you think people here can read thoughts to know what you need?

Take this as an example.
I start with lights off, so the first press of any button starts the lamps.

I would suggest to remove the switch to green-button, because it will be misused.
Nice trick to kill elderly people crossing slowly…

// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

const byte pinCarRed = 2;
const byte pinCarYellow = 3;
const byte pinCarGreen = 4;

const byte pinPedRed = 5;
const byte pinPedGreen = 6;

const byte pinButCross = 7;
const byte pinButClear = 8;

const unsigned int yellowTime = 2000;
const unsigned int redTime = 6000;

enum states {
  tlOff,
  tlNormal,
  tlYellow,
  tlRed,
};

byte currentState = tlOff;
unsigned long stateEntered;
unsigned long topLoop;

Bounce butCross;
Bounce butClear;

void setup() {
  pinMode(pinCarRed, OUTPUT);
  pinMode(pinCarYellow, OUTPUT);
  pinMode(pinCarGreen, OUTPUT);
  pinMode(pinPedRed, OUTPUT);
  pinMode(pinPedGreen, OUTPUT);
  butCross.attach(pinButCross, INPUT_PULLUP);
  butClear.attach(pinButClear, INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println(F("Pedestrian Crossing"));
  switchLamps();
}

void loop() {
  topLoop = millis();
  butCross.update();
  butClear.update();
  switch (currentState) {
    case tlOff:
      if (butCross.fell() || butClear.fell()) {
        switchState(tlNormal);
      }
      break;
    case tlNormal:
      if (butCross.fell() || butClear.fell()) {
        switchState(tlYellow);
      }
      break;
    case tlYellow:
      if (topLoop - stateEntered > yellowTime) {
        switchState(tlRed);
      }
      break;
    case tlRed:
      if ((topLoop - stateEntered > redTime) || butClear.fell()) {
        switchState(tlNormal);
      }
      break;
  }
}

void switchState(byte newState) {
  Serial.print(F("state "));
  Serial.print(newState);
  Serial.print(F(" from "));
  Serial.print(currentState);
  Serial.print(F(" @ "));
  Serial.println(topLoop);
  currentState = newState;
  stateEntered = topLoop;
  switchLamps();
}

void switchLamps() {
  switch (currentState) {
    case tlOff:
      digitalWrite(pinCarRed, LOW);
      digitalWrite(pinCarYellow, LOW);
      digitalWrite(pinCarGreen, LOW);
      digitalWrite(pinPedRed, LOW);
      digitalWrite(pinPedGreen, LOW);
      break;
    case tlNormal:
      digitalWrite(pinCarRed, LOW);
      digitalWrite(pinCarYellow, LOW);
      digitalWrite(pinCarGreen, HIGH);
      digitalWrite(pinPedRed, HIGH);
      digitalWrite(pinPedGreen, LOW);
      break;
    case tlYellow:
      digitalWrite(pinCarRed, LOW);
      digitalWrite(pinCarYellow, HIGH);
      digitalWrite(pinCarGreen, LOW);
      digitalWrite(pinPedRed, HIGH);
      digitalWrite(pinPedGreen, LOW);
      break;
    case tlRed:
      digitalWrite(pinCarRed, HIGH);
      digitalWrite(pinCarYellow, LOW);
      digitalWrite(pinCarGreen, LOW);
      digitalWrite(pinPedRed, LOW);
      digitalWrite(pinPedGreen, HIGH);
      break;
  }
}

How far did you get, or do you need help getting started?

If you're having trouble getting started, start with some of the [u]Examples[/u]. If you understand the Blink Example, the Blink Without Delay Example, and the Digital Read Example, you are well on your way to making a LED/light sequence and making it respond to a button-press.

Do you have any electronics or programming experience?

arduino college project

What's your major? What class is this for? ...It makes a difference in the kind of advice we'll give you. ...It will be helpful to know if you're a 1st year electronics student, or a 4th year urban planning student with no electronics or programming experience trying to solve pedestrian traffic issues.

I assume this is your first Arduino project?

Is this "life size" with real traffic lights, or just with LEDs?

Sorry for bleak description guys, its my first project so not much experience. I have it working as normal no problem,but when i try add the second pedestrian crossing button it goes wrong. I am simulating this with l.e.d.s on a breadboard. I will attach what i have done so far

traffic_light_project.ino (1.79 KB)

No wonder, as you are trying to do all the stuff with delays, good luck with that approach.

Whandall: No wonder, as you are trying to do all the stuff with delays, good luck with that approach.

delays are either bad, very bad or perfect.

in any case where you do one thing at at time, think of Mrs. Finigan. she is driving and wants to stop to get a soda. all traffic stops, everywhere, noting moves. she gets her soda, and all traffic everywhere resumes. very bad. we get around that in two ways.

if(switch== high){ delay(1); delaycounter = delaycounter + 1 } else { delaycounter = 0 } if(delaycounter >= 5000){ do that thing you do }

see ? the pause is so minimal that other things can go about their business. read about the preferred way as blinkwithoutdelay

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)