-1

I'm trying to turn the LED (pin 2) ON when I push the boot button on an esp32 ! Here is my code ! Any idea why this don't work ?

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

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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

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) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
4

1 回答 1

0

您的设计在多个方面存在致命缺陷,主要是,当芯片启动时,i/o 引脚不受您的代码控制,一旦检测到该信号,您的程序就会停止运行。重新启动会恢复其默认上电状态。另外,按钮输入肯定由中断提供服务,您的轮询循环永远不会看到它变高,永远。

如果您想要一个在芯片启动时保持亮起的 LED,您将需要另一个组件,例如 JFET。将 i/o 引脚连接到 JFET 的栅极,其源极和漏极引脚与 LED 的电源或接地引脚串联,然后在程序运行时将该 i/o 引脚设置为高电平,以偏置 JFET,因此保持源极和漏极之间的开路。一旦门变低,电路就会关闭,你的 LED 就会打开。当引导程序执行您的程序时,它再次将该引脚设置为高电平,LED 熄灭。作为奖励,您无需浪费任何计算轮询 i/o。赢:赢!

于 2017-11-28T08:59:37.937 回答