无效循环不能正常循环

时间:2018-03-29 09:03:51

标签: arduino-uno

所以我在arduino uno上创建一个红绿灯系统。我目前的问题是我的主循环不起作用。它应该通过常规模式,使每个灯打开一秒然后下一个,依此类推。目前所发生的一切都是它执行常规模式的一次轮换然后仍然停留在red.ive上保持其余的代码在这里只是因为其他东西引起了问题。

//light variables
int red = 10;
int yellow = 9;
int green = 8;

int inches = 0;
int cm = 0;
float distance;

String inString = "";
int button = 2;
int ECHOPIN = 6;
int TRIGPIN = 7;
int BUZZER = 12;
int sensorPin = A0;            
unsigned int sensorValue = 0;  
char inVal;
int darkness = 150;

void setup()
{
  pinMode(red, OUTPUT);//red led
  pinMode(yellow, OUTPUT);//yellow led
  pinMode(green, OUTPUT);//green led
  pinMode(ECHOPIN, INPUT);//rangefinder input
  pinMode(TRIGPIN, OUTPUT);//rangefiner output
  pinMode(BUZZER,OUTPUT);//buzzer
  Serial.begin(9600);
  Serial.begin(115200); 


}

void loop(){
  distance = distancefind();
  sensorValue = LDRfind();
  if(sensorValue < darkness)
  {
    nightMode();
  }
  else if(distance < 10)
  {
    pedestrianMode();
  }
  else
  {
   regularMode();
  }
}




void regularMode(){ 
    // Green + yellow off. Red on for 2 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);
    delay(2000);

    // Green + red off. Yellow on for 1 second
    digitalWrite(red, LOW);
    digitalWrite(yellow, HIGH);
    delay(1000);

    // Red + yellow off. Green on for 2 seconds 
    digitalWrite(yellow, LOW);
    digitalWrite(green, HIGH);
    delay(2000);

    // Red + green off. Yellow on for 1 second
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    delay(1000);

}

void pedestrianMode(){
  // buzzer buzzes. yellow bulb flashes for 10 seconds.

  for(int i = 0; i <= 9; i++)
    {
        digitalWrite(yellow, HIGH);
        digitalWrite(BUZZER, HIGH);
        delay(200);
        digitalWrite(yellow, LOW);
        digitalWrite(BUZZER, LOW);
        delay(200);
 } 
}

void nightMode(){
 digitalWrite(yellow, LOW);
 digitalWrite(green, LOW);
 digitalWrite(red, HIGH);

  if(distance <10){
    digitalWrite(yellow, LOW);
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
  }
  else{
    nightMode();

  }
}

float distancefind(){
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN,HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN,LOW); 
 distance = pulseIn(ECHOPIN, HIGH); // return 0 if no high pulse in 1sec
 distance = distance/58;
 Serial.print(distance);
 return distance;
}

int LDRfind(){
  sensorValue = analogRead(sensorPin);
  Serial.println("Sensor value: ");
  Serial.println(sensorValue);
  return sensorValue;
}

void buttonPress(){
  if (digitalRead(button) == HIGH){
        delay(15); // software debounce
        if (digitalRead(button) == HIGH) {
            // if the switch is HIGH, ie. pushed down - change the lights!
            digitalWrite(green, HIGH);
            digitalWrite(yellow, HIGH);
            digitalWrite(red, HIGH);
            delay(15000); // wait for 15 seconds
}
  }
}

1 个答案:

答案 0 :(得分:0)

好的,所以在发布之前我已经玩了好一个小时。发布此消息之后的minuite我知道恒定的红色来自我的夜间模式。我添加了一些距离检查,它工作正常。很抱歉浪费了谁在寻找时间。