const int REDPIN_FIRSTDIRECTION = 13;
const int YELLOWPIN_FIRSTDIRECTION = 12;
const int GREENPIN_FIRSTDIRECTION = 11;
const int REDPIN_SECONDDIRECTION = 10;
const int YELLOWPIN_SECONDDIRECTION = 9;
const int GREENPIN_SECONDDIRECTION = 8;
const int BUTTONPIN_FIRSTDIRECTION = 5;
const int BUTTONPIN_SECONDDIRECTION = 4;
const int NORMAL = 10;
const int GREEN_NORMAL = 15000;
int count_first=0;
int count_second=0;
int count_first_previous=0;
int count_second_previous=0;
int red_time = GREEN_NORMAL;
int green_time = GREEN_NORMAL;
int yellow_time = 3000;
int green_time_previous=green_time;
int red_time_previous=red_time;
unsigned long previous_second = 0;
bool currentButtonState;
bool buttonPressed = false;
void setup() {
pinMode(BUTTONPIN_FIRSTDIRECTION, INPUT_PULLUP);
pinMode(REDPIN_FIRSTDIRECTION, OUTPUT);
pinMode(YELLOWPIN_FIRSTDIRECTION, OUTPUT);
pinMode(GREENPIN_FIRSTDIRECTION, OUTPUT);
pinMode(BUTTONPIN_SECONDDIRECTION, INPUT_PULLUP);
pinMode(REDPIN_SECONDDIRECTION, OUTPUT);
pinMode(YELLOWPIN_SECONDDIRECTION, OUTPUT);
pinMode(GREENPIN_SECONDDIRECTION, OUTPUT);
Serial.begin(9600);
}
void loop() {
green_time_previous = green_time;
red_time_previous = red_time;
if(abs(count_first-count_second)>5&&count_first-NORMAL>5&&count_first-count_second>5)
{
if(count_first-count_second<10&&green_time_previous-red_time_previous>=1800){
green_time=green_time_previous-3000; red_time=red_time_previous+3000;
}
else{
green_time=green_time_previous+3000; red_time=red_time_previous-3000;
}
}
else if(abs(count_first-count_second)>5&&count_second-NORMAL>5&&count_second-count_first>5)
{
if(count_second-count_first<10&&red_time_previous-green_time_previous>=18000){
green_time=green_time_previous+3000; red_time=red_time_previous-3000;
}
else{
red_time=red_time_previous+3000; green_time=green_time_previous-3000;
}
}
else
{
if(red_time_previous>green_time_previous){
red_time=red_time_previous-3000; green_time=green_time_previous+3000;
}
else if(green_time_previous>red_time_previous){
red_time=red_time_previous+3000; green_time=green_time_previous-3000;
}
}
if(green_time <= 3000) {green_time = 6000; red_time = 24000;}
if(red_time <= 3000) {red_time = 6000; green_time = 24000;}
Serial.print("green: ");
Serial.println(green_time);
Serial.print("red: ");
Serial.println(red_time);
Serial.print("first: ");
Serial.println(count_first);
Serial.print("second: ");
Serial.println(count_second);
count_first_previous=count_first;
count_second_previous=count_second;
count_first=0;
count_second=0;
blink(REDPIN_FIRSTDIRECTION, GREENPIN_SECONDDIRECTION, red_time);
blink(YELLOWPIN_FIRSTDIRECTION, YELLOWPIN_SECONDDIRECTION, yellow_time);
blink(GREENPIN_FIRSTDIRECTION, REDPIN_SECONDDIRECTION, green_time);
blink(YELLOWPIN_FIRSTDIRECTION, YELLOWPIN_SECONDDIRECTION, yellow_time);
}
void blink(int pin_first, int pin_second, int time)
{
digitalWrite(pin_first, HIGH);
digitalWrite(pin_second, HIGH);
long start = millis();
long lastButtonPress;
while(millis() - start < time)
{
unsigned long remaining_time = (time - (millis() - start)) / 1000;
if (pin_first == GREENPIN_FIRSTDIRECTION)
{
if (!digitalRead(BUTTONPIN_FIRSTDIRECTION)&&millis()-lastButtonPress>250)
{
lastButtonPress=millis();
count_first = count_first + 1;
Serial.print("1: ");
Serial.println(count_first);
}
writeRemainingTime(remaining_time);
}
if (pin_second == GREENPIN_SECONDDIRECTION)
{
if (!digitalRead(BUTTONPIN_SECONDDIRECTION)&&millis()-lastButtonPress>250)
{
lastButtonPress=millis();
count_second = count_second + 1;
Serial.print("2: ");
Serial.println(count_second);
}
writeRemainingTime(remaining_time);
}
}
digitalWrite(pin_first, LOW);
digitalWrite(pin_second, LOW);
}
void writeRemainingTime(unsigned long remaining_time){
unsigned long current_second = remaining_time % 60;
if (current_second != previous_second)
{
Serial.print("Время до следующего сигнала: ");
Serial.println(remaining_time);
previous_second = current_second;
}
}