Hi everyone,
I wrote code for traffic lights of T-junction witn Main traffic light, side traffic light and pedestrian light.
I succeeded to write the code except one thing.
The only problem i'm dealing with is to combine push button that will increase the amount of time (once it pushed for a moment) the next time the light turns green.
if the button will be pushed - next time pedestrian light will turn green for 8 sec instead of 6 secs.
// put your setup code here, to run once:
const int buttonPin = A0; // the number of the pushbutton pin
int buttonPressed=0; // variable for reading the pushbutton status
const int PE=3;
void setup() {
pinMode(9, OUTPUT); // Set digital pin 9 (D9) to an output
TCCR1A = _BV(COM1A1) | _BV(WGM11); // Enable the PWM output OC1A on digital pins 9
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12); // Set fast PWM and prescaler of 256 on timer 1
ICR1 = 62499; // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
OCR1A = 31250; // Set the duty-cycle to 10%: 62499 / 10 = 6249
OCR1B=3125;
pinMode(buttonPin, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(PE, OUTPUT);
}
void loop() {
void Check();
digitalWrite(PE, HIGH);
// First State (Main-G, Side-R, Walk-G)
if(!buttonPressed){
digitalWrite(PE, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(PE, LOW);
digitalWrite(8, HIGH);
delay(8000);
digitalWrite(PE, HIGH);
digitalWrite(8, LOW);
}
else {
digitalWrite(PE, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(PE, LOW);
digitalWrite(8, HIGH);
delay(6000);
digitalWrite(PE, HIGH);
digitalWrite(8, LOW);
}
void Check(); // Button check
// digitalWrite(PE, LOW);
// Second State (Main-Y, Side-R, Walk-R)
digitalWrite(10, HIGH);
delay(3000);
digitalWrite(10, LOW);
void Check();
void Check(); // Button check
// Third State (Main-R, Side-G, Walk-R)
digitalWrite(11, HIGH);
delay(6000);
void Check();
digitalWrite(11, LOW);
void Check(); // Button check
// Forth State (Main-Y, Side-R, Walk-R)
digitalWrite(12, HIGH);
delay(3000);
digitalWrite(12, LOW);
void Check(); // Button check
}
void Check(){
for(int i=0 ; i<1000; i++)
{
delay(1);
if(!buttonPressed)
{
buttonPressed=analogRead(buttonPin);
}
}
}
Please let me know if anybody have an idea
Thanks