#include<Servo.h>
#include<EEPROM.h>
#define segA A0 // connecting segment A to PIN2
#define segB 3 // connecting segment B to PIN3
#define segC 4 // connecting segment C to PIN4
#define segD 5 // connecting segment D to PIN5
#define segE 6 // connecting segment E to PIN6
#define segF 7 // connecting segment F to PIN7
#define segG 8 // connecting segment G to PIN8
#define red 9 // red pin
#define green 10 // green pin
#define speaker 12
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
Servo myservo;
const int button = 2;
int di,du,count,a,t,y,z,u, value1, value2;
int l=0;
int b=0;
void setup(){
Serial.begin(9600);
pinMode(A1, INPUT);
myservo.attach(11);
for(a=3;a<=10;a++)
{
pinMode(a, OUTPUT);
}
pinMode(button, INPUT);
pinMode(A0, OUTPUT);
pinMode(speaker,OUTPUT);
attachInterrupt(digitalPinToInterrupt(button), bstate, HIGH);
}
void loop(){
pOffroadOn();
Serial.println(di);
value1 = EEPROM.read(1);
Serial.print("value is : " );
Serial.println(value1);
Serial.println(di);
if(value1 == 1){
roadOffpOn();
delay(9000);
}
else if(value1 == 0){
pOffroadOn();
}
EEPROM.update(1,0);
value2 = EEPROM.read(2);
Serial.print("value2 is: ");
Serial.println(value2);
if(value2 == 1 && b == 0){
pOffroadOn();
Serial.println("button pressed in first cycle");
}
else if(value2 == 1 && b == 1){
roadOffpOn();
Serial.println("button pressed in second cycle");
}
else{
pOffroadOn();
Serial.println("button not pressed");
}
EEPROM.update(2,0);
}
void trainAppear(){ // train appears
ultra();
if(di<=30){
Train();
}
}
void timerOff(){ // Timer off
for(t=3;t<=8;t++){
digitalWrite(t, LOW);
}
digitalWrite(A0, LOW);
}
void pOffroadOn(){ // pedestrian off when road On
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
myservo.write(90);
noTone(speaker);
trainAppear();
Timer();
}
void roadOffpOn(){ // road off when pedestrian on
digitalWrite(red, LOW);
//digitalWrite(green, HIGH);
myservo.write(0);
//speakerP();
timerOff();
noTone(speaker);
//tone(speaker,1000);
//trainAppear();
greenG();
}
void bstate() {
EEPROM.write(1,1);
EEPROM.write(2,1);
Serial.println("value return: ");
}
void Timer(){ // Timer O
switch (count)
{
case 0 ://when count value is zero show”0” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
case 1:// when count value is 1 show”1” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
case 2:// when count value is 2 show”2” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
case 3:// when count value is 3 show”3” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
case 4:// when count value is 4 show”4” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
case 5:// when count value is 5 show”5” on disp
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
case 6:// when count value is 6 show”6” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;
case 7:// when count value is 7 show”7” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;
case 8:// when count value is 8 show”8” on disp
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
}
if (count<10)
{
count++;
delay(1000);///increment count integer for every second
}
else if (count==10)
{
count=0;// if count integer value is equal to 10, reset it to zero.
b = 1;
}
}
void greenG(){
for(y=0;y<=5;y++){
digitalWrite(green, HIGH);
delay(900);
digitalWrite(green, LOW);
delay(100);
trainAppear();
}
for(u=0;u<=30;u++){
digitalWrite(green, HIGH);
delay(90);
digitalWrite(green, LOW);
delay(10);
trainAppear();
}
}
Comments