如果按下一个按钮而不是按住我的按钮,我试图在一定时间内创建一次性脉冲请求这是我的代码(不要担心阅读2我需要重命名以后是一些其他变量)
const int buttonPin = 10; // the number of the pushbutton pin
const int ledPin1 = 11; // the number of the LED pin
const int ledPin2 = 12;
// Variables will change:
int ledState1 = LOW; // the current state of the output pin
int ledState2 = LOW;
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime1 = 0; // the last time the output pin was toggled
long debounceDelay1 = 0; // the debounce time; increase if the output flickers
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long debounceDelay2 = 300;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// set initial LED state
digitalWrite(ledPin1, ledState1);
digitalWrite(ledPin2, ledState2);
}
void loop() {
int reading2 = digitalRead(buttonPin);
if (reading2 == HIGH){
ledState1 = !ledState1;
digitalWrite(ledPin1, ledState1);
}
else if(ledState2 == LOW){
ledState1 = LOW;
digitalWrite(ledPin1, ledState1);
}
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading2 != lastButtonState) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay2) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading2 != buttonState) {
buttonState = reading2;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState2 = !ledState2;
}
}
}
// set the LED:
digitalWrite(ledPin2, ledState2);
lastButtonState = reading2;
答案 0 :(得分:0)
我建议在电路中添加一个单稳态多谐振荡器。 这里解释了单稳态多谐振荡器的工作原理。 https://electrosome.com/monostable-multivibrator-555-timer/