光线无法正常切换

时间:2017-05-10 15:27:39

标签: arduino

我正在使用Arduino UNO为我的学校进行交通灯项目。我使用4路继电器来打开和关闭240V灯。问题是灯泡在一定时间后不会切换。我认为问题出在我的代码中,因为我的继电器上的LED也没有切换。

int red = 12;
int yellow = 11;
int green = 10;

int timer = 0;

int light = 0;

void setup(){
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);

  digitalWrite(red, HIGH);
  digitalWrite(yellow, HIGH);
  digitalWrite(green, HIGH);

  light = 1;
  timer = 9;
}

void loop(){
  if(timer == 0){
    nextlight();
  }
  if(timer >= 0){
    timer = timer - 1;
  }

  if(light == 1){
    digitalWrite(yellow, HIGH);
    digitalWrite(green, HIGH);
    delay(50);
    digitalWrite(red, LOW);
  }
  if(light == 2){
    digitalWrite(red, HIGH);
    digitalWrite(green, HIGH);
    delay(50);
    digitalWrite(yellow, LOW);
  }
  if(light == 3){
    digitalWrite(red, HIGH);
    digitalWrite(yellow, HIGH);
    delay(50);
    digitalWrite(green, LOW);
  }

   delay(1000);
}

void nextlight(){
  if(light == 1){
    light = 2;
    timer = 2;
  }
  if(light == 2){
    light = 3;
    timer = 9;
  }
  if(light == 3){
    light = 1;
    timer = 9;
  }
}

编辑:我尝试使用引脚13处的灯进行调试,似乎程序根本没有循环。我真的不知道为什么。

2 个答案:

答案 0 :(得分:2)

查看您的nextLight功能。 light将始终为1,这就是它不切换的原因。

void nextlight(){
  if(light == 1){
    light = 2;
    timer = 2;
  }
  if(light == 2){
    light = 3;
    timer = 9;
  }
  if(light == 3){
    light = 1;
    timer = 9;
  }
}

所以,如果light == 1,light = 2.那么,如果light == 2,light = 3.那么,如果light == 3 ... light = 1,那么函数返回。所以无论如何,light总是1.

所以,你要么需要一些else if,要么只是在每个区块内返回,这是我认为我会做的:

void nextlight(){
  if(light == 1){
    light = 2;
    timer = 2;
    return;
  }
  if(light == 2){
    light = 3;
    timer = 9;
    return;
  }
  if(light == 3){
    light = 1;
    timer = 9;
    return;
  }
}

你在这里遇到同样的问题会让你的时间陷入困境:

  if(timer == 0){
    nextlight();
  }
  if(timer >= 0){
    timer = timer - 1;
  }

所以,相反:

  if(timer == 0){
    nextlight();
  }
  else {
    timer = timer - 1;
  }

答案 1 :(得分:0)

不是解决方案,但可能有帮助:“模拟”您的代码给出了这个序列,每一行显示了使用digitalWrite()写入输出时的状态:

计时器,浅色,黄色,绿色,红色

8 1 HIGH HIGH HIGH
8 1 HIGH HIGH HIGH
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW
8 1 HIGH HIGH LOW

将此与您期望发生的事情进行比较。

这是“模拟器”(Python)

HIGH = "HIGH"
LOW = "LOW"
yellow = 0
green = 1
red = 2
lights = [HIGH, HIGH, HIGH]


timer = 9
light = 1


def digitalWrite(thelight, newstate):
    lights[thelight] = newstate
    print("{:2} {} {}".format(timer, light, " ".join(lights)))


def nextlight():
    #global light
    if light == 1:
        light = 2
        timer = 2

    if light == 2:
        light = 3
        timer = 9

    if light == 3:
        light = 1
        timer = 9


for n in range( 20 ):
    if timer == 0:
        nextlight()

    if timer >= 0:
        timer = timer - 1

    if light == 1:
        digitalWrite(yellow, HIGH)
        digitalWrite(green, HIGH)
        digitalWrite(red, LOW)

    if light == 2:
        digitalWrite(red, HIGH)
        digitalWrite(green, HIGH)
        digitalWrite(yellow, LOW)

    if light == 3:
        digitalWrite(red, HIGH)
        digitalWrite(yellow, HIGH)
        digitalWrite(green, LOW)

    timer += 1

也许您可以以类似的方式将代码移植到JavaScript(即使是使用delay()),因为它已经内置到您的浏览器中(因此您可以在线运行,例如JSFiddle.com等)和语法有点类似于C ++。

更新了以前在nextlight()中丢失的最后一个条件。有关一些好的提示,请参阅imjosh's answer