在启动代码之前测试LED

时间:2014-07-09 13:58:11

标签: arduino arduino-uno

无法找到我想要的东西。

我已经写了以下代码:

    /*
  Traffic Lights
  Turns on an LED on in a predetermined sequence - Red, Red Amber, Green, Amber, Red.

  This example code is in the public domain.
 */

// determine the colours and the pins that they are connected to
int green = 12; //green
int amber = 8; //amber
int red = 4; //red

// the setup routine runs once when you press reset:
void setup() 
{                
  // initialize the digital pins as an output.
  pinMode(green, OUTPUT); 
  pinMode(amber, OUTPUT);
  pinMode(red, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(red, HIGH); //red on
  delay(1250);
  digitalWrite(amber, HIGH); //red amber on
  delay(1250);
  digitalWrite(red, LOW);
  digitalWrite(amber, LOW); //red amber off
  delay(250);
  digitalWrite(green, HIGH); //green on
  delay(1250);
  digitalWrite(green, LOW); //green off
  delay(250);
  digitalWrite(amber, HIGH); //amber on
  delay(1250);
  digitalWrite(amber, LOW); //amber off
  delay(250);
  digitalWrite(red, HIGH); //red on
  delay(1250);


}

对于Arduino而言,我非常感谢他们。我的一位开发者朋友正试图让我“测试”。在我的代码工作之前的一切(TDD方法),我的想法是在开始时同时打开所有灯;例如,使用一点'digitalWrite(green, HIGH);非常简单。但是,我不希望它循环播放,我只是希望它们只有一次出现约2秒才能显示它们正在工作,然后进入循环。

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:1)

只需在您的设置功能中执行此操作即可!请记住,当Arduino上电(或复位)时,安装程​​序只运行一次,然后调用循环功能,直到设备断电。

我会尝试以下代码段:

void setup() 
{                
  // initialize the digital pins as an output.
  pinMode(green, OUTPUT); 
  pinMode(amber, OUTPUT);
  pinMode(red, OUTPUT);
  //Turn the pins on to check your wiring
  digitalWrite(green, HIGH);
  digitalWrite(amber, HIGH);
  digitalWrite(red, HIGH);
  delay(1000);
  digitalWrite(green, LOW);
  digitalWrite(amber, LOW);
  digitalWrite(red, LOW);
}

编辑:将ifdef中的调试代码包含在:

中也是值得的
#ifdef DEBUG
...
#endif 

编辑:澄清(回应评论):

#ifdef DEBUG
  digitalWrite(green, HIGH);
  digitalWrite(amber, HIGH);
  digitalWrite(red, HIGH);
  delay(1000);
  digitalWrite(green, LOW);
  digitalWrite(amber, LOW);
  digitalWrite(red, LOW);
#endif 

这样,您可以保留调试代码,并在不再调试时“关闭”。