简体   繁体   English

试图用 arduino 打开 LED 达 x 秒

[英]trying to turn on an led with arduino for x seconds

My code is suppose to take in 2 numbers: num1 and num2 .我的代码假设包含 2 个数字: num1num2 Then it should turn on the built in led for num1 seconds and then blink the led for num2 seconds.然后它应该打开内置num1秒,然后闪烁num2秒。 Should be a very simple program but for some reason the led will not turn on.应该是一个非常简单的程序,但由于某种原因,led 无法打开。 Here is my code:这是我的代码:

long num1 = 0;
long num2 = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available() == 0) {
  }
  bool d = true;
  while (d) {
    num1 = Serial.parseInt();
    delay(2);
    if (num1 > 0) {
      d = false;
    }
  }

  bool c = true;
  while (c) {
    num2 = Serial.parseInt();
    delay(2);
    if (num2 > 0) {
      c = false;
    }
  }

  // num1=5;
  // num2=4;

  digitalWrite(LED_BUILTIN, HIGH);
  delay(num1 * 1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1500);

  for (int i = 0; i < num2; i++) {
    // delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
  }
}

are you sure that the LED_BUILTIN corresponds to your board?你确定 LED_BUILTIN 对应于你的电路板吗?

Try setting it manually to eg 13 for a regular nano尝试手动将其设置为例如 13 用于常规 nano

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM