const int westButtonPin = 6;   // 西向车辆检测按键引脚
const int northButtonPin = 5;  // 北向车辆检测按键引脚
const int westGreenPin = 7;    // 西向绿灯引脚
const int northGreenPin = 2;   // 北向绿灯引脚
const int westYellowPin = 8;   // 西向黄灯引脚
const int northYellowPin = 3;  // 北向黄灯引脚
const int westRedPin = 9;      // 西向红灯引脚
const int northRedPin = 4;     // 北向红灯引脚

int currentState = 0;          // 当前交通灯状态西向绿灯
bool dubbleDirection = false;
bool noDirection = false;
unsigned long previousMillis = 0;  // 记录上一次状态切换的时间
const unsigned long greenDuration = 4000;  // 绿灯持续时间(3秒)
const unsigned long yellowDuration = 1000;  // 黄灯持续时间(0.5秒)

void setup() {
  pinMode(westButtonPin, INPUT_PULLUP);
  pinMode(northButtonPin, INPUT_PULLUP);
  pinMode(westGreenPin, OUTPUT);
  pinMode(northGreenPin, OUTPUT);
  pinMode(westYellowPin, OUTPUT);
  pinMode(northYellowPin, OUTPUT);
  pinMode(westRedPin, OUTPUT);
  pinMode(northRedPin, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  // 检测西向车辆按键状态
  bool westButtonPressed = digitalRead(westButtonPin) == LOW;

  // 检测北向车辆按键状态
  bool northButtonPressed = digitalRead(northButtonPin) == LOW;

  //西向北向均按下
  dubbleDirection = westButtonPressed && northButtonPressed;
  noDirection = !westButtonPressed && !northButtonPressed;

  // 根据当前状态和车辆按键状态切换交通灯状态
  //if(currentState == 0 && noDirection == true){
  //  currentState == 0;//两边都没有车来,默认西向绿灯
  //}
  if(!dubbleDirection && !noDirection){  //只有一个方向有车
    switch (currentState) {
      case 0:  // 西向绿灯
        if (northButtonPressed) {
          currentState = 1;  // 切换到北向绿灯
          previousMillis = currentMillis;
        }
        break;
      case 1:  // 北向绿灯
        if (westButtonPressed) {
          currentState = 0;  // 切换到西向绿灯
          previousMillis = currentMillis;
        }
        break;
    }
  }


  // 根据交通灯状态控制灯光
  switch (currentState) {
    case 0:  // 西向绿灯
      digitalWrite(westGreenPin, HIGH);
      digitalWrite(northGreenPin, LOW);
      digitalWrite(westYellowPin, LOW);
      digitalWrite(northYellowPin, LOW);
      digitalWrite(westRedPin, LOW);
      digitalWrite(northRedPin, LOW);
      break;
    case 1:  // 北向绿灯
      digitalWrite(westGreenPin, LOW);
      digitalWrite(northGreenPin, HIGH);
      digitalWrite(westYellowPin, LOW);
      digitalWrite(northYellowPin, LOW);
      digitalWrite(westRedPin, LOW);
      digitalWrite(northRedPin, LOW);
      break;
  }

  // 根据当前状态和经过的时间切换灯光
  if(noDirection || dubbleDirection){
    if (currentState == 0 && currentMillis - previousMillis >= greenDuration) {
      currentState = 1;  // 切换到北向绿灯
      previousMillis = currentMillis;
    } else if (currentState == 1 && currentMillis - previousMillis >= greenDuration) {
      currentState = 0;  // 切换到西向绿灯
    previousMillis = currentMillis;
    }
  }
  if (currentState == 0 && currentMillis - previousMillis >= greenDuration - yellowDuration) {
    digitalWrite(westGreenPin, LOW);
    digitalWrite(northGreenPin, LOW);
    digitalWrite(westYellowPin, HIGH);  // 切换到黄灯
    digitalWrite(northYellowPin, LOW);
    digitalWrite(westRedPin, LOW);
    digitalWrite(northRedPin, LOW);
  }
  else
  {
    if (currentState == 0 && currentMillis - previousMillis >= yellowDuration) {
      digitalWrite(westGreenPin, LOW);
      digitalWrite(northGreenPin, LOW);
      digitalWrite(westYellowPin, LOW);  
      digitalWrite(northYellowPin, LOW);
      digitalWrite(westRedPin, LOW);
      digitalWrite(northRedPin, HIGH);  // 切换到红灯
    }
  } 
  if (currentState == 1 && currentMillis - previousMillis >= greenDuration - yellowDuration) {
    digitalWrite(westGreenPin, LOW);
    digitalWrite(northGreenPin, LOW);
    digitalWrite(westYellowPin, LOW);  
    digitalWrite(northYellowPin, HIGH); // 切换到黄灯
    digitalWrite(westRedPin, LOW);
    digitalWrite(northRedPin, LOW);
  }
  else
  {
    if (currentState == 1 && currentMillis - previousMillis >= yellowDuration) {
      digitalWrite(westGreenPin, LOW);
      digitalWrite(northGreenPin, LOW);
      digitalWrite(westYellowPin, LOW);  
      digitalWrite(northYellowPin, LOW);
      digitalWrite(westRedPin, HIGH);  // 切换到红灯
      digitalWrite(northRedPin, LOW);
    }
  }
}
$abcdeabcde151015202530354045505560fghijfghij