Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 我试图让一个按钮打开和关闭LED,但它只是保持关闭_Arduino_Arduino Uno_Arduino C++ - Fatal编程技术网

Arduino 我试图让一个按钮打开和关闭LED,但它只是保持关闭

Arduino 我试图让一个按钮打开和关闭LED,但它只是保持关闭,arduino,arduino-uno,arduino-c++,Arduino,Arduino Uno,Arduino C++,我开始的代码,使LED灯打开,而按钮被按下。成功了。 但后来我试着用tweek键,这样按钮就会像“开-关”开关一样,只按一次就可以在状态之间切换 led与下面的旧代码一起工作,所以我认为这不是接线问题。 仅供参考,我跳过了设置功能,与我使其闪烁时相同 // constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbu

我开始的代码,使LED灯打开,而按钮被按下。成功了。 但后来我试着用tweek键,这样按钮就会像“开-关”开关一样,只按一次就可以在状态之间切换

led与下面的旧代码一起工作,所以我认为这不是接线问题。 仅供参考,我跳过了设置功能,与我使其闪烁时相同

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int switcher = 0;

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    if (switcher = 0) {
      switcher = 1;
      delay(500);
    }
    else if (switcher = 1) {
      switcher = 0;
      delay(500);
    }
  }
  if (switcher == 1) {
    digitalWrite(ledPin, HIGH);
  }
  /*
  else {
    digitalWrite(ledPin, LOW);
  }
  */

}
基于

基于

仅当您希望整个系统停止时才使用延迟。它也用于Arduino职业生涯开始时的学习目的。在实际应用程序中,您将使用延迟库或计时。如果在应用程序中使用延迟,则无法读取按钮高事件,这意味着只能在500毫秒延迟后的501毫秒内读取按钮,您将有一个1毫秒或更短的窗口,这几乎是不可能的。无论如何,你应该看看Arduino的例子

此外,必须为按钮使用上拉电阻器,或在设置中为pinMode声明输入上拉,以避免反弹,请参见下面的示例

以下是解决代码的方法: 仅当您希望整个系统停止时才使用延迟。它也用于Arduino职业生涯开始时的学习目的。在实际应用程序中,您将使用延迟库或计时。如果在应用程序中使用延迟,则无法读取按钮高事件,这意味着只能在500毫秒延迟后的501毫秒内读取按钮,您将有一个1毫秒或更短的窗口,这几乎是不可能的。无论如何,你应该看看Arduino的例子

此外,必须为按钮使用上拉电阻器,或在设置中为pinMode声明输入上拉,以避免反弹,请参见下面的示例

以下是解决代码的方法:
使用此代码,当您按下按钮时,Led指示灯将点亮,当您再次按下按钮时,Led指示灯将熄灭。当您按下按钮时,状态始终会发生变化

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int switchState = 0; // actual read value from pin4
int oldSwitchState = 0; // last read value from pin4
int lightsOn = 0; // is the switch on = 1 or off = 0


void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);   // declare LED as output
  pinMode(buttonPin, INPUT); // declare push button as input
}

void loop() {

  switchState = digitalRead(buttonPin); // read the pushButton State

  if (switchState != oldSwitchState) // catch change
  {
    oldSwitchState = switchState;
    if (switchState == HIGH)
    {
      // toggle
      lightsOn = !lightsOn; // invert the values
    }
  }

  if (lightsOn)
  {
    digitalWrite(ledPin, HIGH); // set the LED on
    Serial.println("LED Turned ON");
  } else {
    digitalWrite(ledPin, LOW); // set the LED off
    Serial.println("LED Turned Off");
  }
}

使用此代码,当您按下按钮时,Led指示灯将点亮,当您再次按下按钮时,Led指示灯将熄灭。当您按下按钮时,状态始终会发生变化

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int switchState = 0; // actual read value from pin4
int oldSwitchState = 0; // last read value from pin4
int lightsOn = 0; // is the switch on = 1 or off = 0


void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);   // declare LED as output
  pinMode(buttonPin, INPUT); // declare push button as input
}

void loop() {

  switchState = digitalRead(buttonPin); // read the pushButton State

  if (switchState != oldSwitchState) // catch change
  {
    oldSwitchState = switchState;
    if (switchState == HIGH)
    {
      // toggle
      lightsOn = !lightsOn; // invert the values
    }
  }

  if (lightsOn)
  {
    digitalWrite(ledPin, HIGH); // set the LED on
    Serial.println("LED Turned ON");
  } else {
    digitalWrite(ledPin, LOW); // set the LED off
    Serial.println("LED Turned Off");
  }
}

检查此示例,确保在if条件中使用==运算符,而不是赋值运算符=。此语言中的非布尔表达式始终为true。请检查此示例,确保在if条件中使用==运算符,而不是赋值运算符=。非布尔表达式在这种语言中总是正确的。如示例中所述//稍微延迟一点以避免反弹Delay50。这是在系统中添加一个bug,而不是如何为按钮进行电气工程。您应该使用上拉电阻器或在代码中声明它。看我的例子。我知道这是从Arduino复制来的,但它只是用于教学目的,不适用于现实生活。正如示例中所述//稍微延迟一点以避免反弹延迟50。这是在系统中添加一个bug,而不是如何为按钮进行电气工程。您应该使用上拉电阻器或在代码中声明它。看我的例子。我知道这是从Arduino复制的,但它只是用于教学目的,不适用于现实生活。谢谢你的帮助!您提到您必须在501ms的准确时间按下按钮,但我认为我只在您按下按钮后添加了一个延迟。我添加了延迟,这样它就不会循环超过一次,并给出按钮处于“高”状态的读数。谢谢您的帮助!您提到您必须在501ms的准确时间按下按钮,但我认为我只在您按下按钮后添加了一个延迟。我添加了延迟,这样它就不会循环超过一次,并给出按钮处于“高”状态的读数
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int switchState = 0; // actual read value from pin4
int oldSwitchState = 0; // last read value from pin4
int lightsOn = 0; // is the switch on = 1 or off = 0


void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);   // declare LED as output
  pinMode(buttonPin, INPUT); // declare push button as input
}

void loop() {

  switchState = digitalRead(buttonPin); // read the pushButton State

  if (switchState != oldSwitchState) // catch change
  {
    oldSwitchState = switchState;
    if (switchState == HIGH)
    {
      // toggle
      lightsOn = !lightsOn; // invert the values
    }
  }

  if (lightsOn)
  {
    digitalWrite(ledPin, HIGH); // set the LED on
    Serial.println("LED Turned ON");
  } else {
    digitalWrite(ledPin, LOW); // set the LED off
    Serial.println("LED Turned Off");
  }
}