防止 ButtonState 从高电平切换到低电平
Prevent ButtonState Toggle From HIGH to LOW
功能:
在按下红色圆顶按钮(不是 2 状态按钮)之前,串行监视器将打印列表“0"s and when the red dome button is pressed, the button state will toggle from LOW to HIGH, hence at the serial monitor will print a list of "1”。
但是,当按钮处于高电平状态时,串行监视器打印“1”,用户将无法将按钮状态从高电平切换为低电平。因此,按钮只能在一段延迟(25 秒)后自动从 HIGH 切换到 LOW。
因此,正确的行为:
Initial state print => 00000000000000(当用户按下红色圆顶按钮时 => buttonstate 将 LOW 变为 HIGH)1111111111111111111(当用户按下按钮时,没有任何反应)1111111111111111111(延迟 25 秒后,buttonState 将由 HIGH 变为 LOW )0000000000000
问题:
此时,用户可以在低到高和高到低之间切换。意思是,流量 => 00..000(用户按下按钮,将低切换为高)111...111(用户按下按钮,将高切换为低)0000...
而且我不太确定如何启用按钮仅从低切换到高但禁用按钮从高切换到低。
意思是当用户按下按钮时,它可以将按钮状态从“0”更改为“1”,但当按钮处于“1”时无法更改按钮状态。
因此,我想请求一些帮助,以允许以下正确行为。
谢谢
代码:
const int buttonPin = 2; //the number of the pushbutton pin
const int Relay = 4; //the number of the LED relay pin
uint8_t stateLED = LOW;
uint8_t btnCnt = 1;
int buttonState = 0; //variable for reading the pushbutton status
int buttonLastState = 0;
int outputState = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Check if there is a change from LOW to HIGH
if (buttonLastState == LOW && buttonState == HIGH)
{
outputState = !outputState; // Change outputState
}
buttonLastState = buttonState; //Set the button's last state
// Print the output
if (outputState)
{
switch (btnCnt++) {
case 100:
stateLED = LOW;
digitalWrite(Relay, HIGH); // after 10s turn on
break;
case 250:
digitalWrite(Relay, LOW); // after 20s turn off
//Toggle ButtonState to LOW from HIGH without user pressing the button
digitalWrite(buttonPin, LOW);
break;
case 252: // small loop at the end, to do not repeat the LED cycle
btnCnt--;
break;
}
Serial.println("1");
}else{
Serial.println("0");
if (btnCnt > 0) {
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}
您需要设置 outputState 并让它设置直到 25 秒后重置。如果仍然按下按钮,它将在 251 上循环。
const int buttonPin = 2; //the number of the pushbutton pin
const int Relay = 4; //the number of the LED relay pin
uint8_t stateLED = LOW;
uint8_t btnCnt = 1;
bool outputState = false;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
void loop() {
outputState |= digitalRead(buttonPin); // if pushButton is high, set outputState (low does nothing)
// Print the output
if (outputState)
{
switch (btnCnt++) {
case 100:
stateLED = LOW;
digitalWrite(Relay, HIGH); // after 10s turn on
break;
case 250:
digitalWrite(Relay, LOW); // after 20s turn off
//Toggle ButtonState to LOW from HIGH without user pressing the button
outputState = false; // reset state to low
break;
case 251: // loop (it might happen, if previous step sets outputState=false but button is still pressed -> no action)
--btnCnt;
outputState = false;
break;
}
Serial.println("1");
}else{
Serial.println("0");
if (btnCnt > 0) {
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}
功能:
在按下红色圆顶按钮(不是 2 状态按钮)之前,串行监视器将打印列表“0"s and when the red dome button is pressed, the button state will toggle from LOW to HIGH, hence at the serial monitor will print a list of "1”。
但是,当按钮处于高电平状态时,串行监视器打印“1”,用户将无法将按钮状态从高电平切换为低电平。因此,按钮只能在一段延迟(25 秒)后自动从 HIGH 切换到 LOW。
因此,正确的行为:
Initial state print => 00000000000000(当用户按下红色圆顶按钮时 => buttonstate 将 LOW 变为 HIGH)1111111111111111111(当用户按下按钮时,没有任何反应)1111111111111111111(延迟 25 秒后,buttonState 将由 HIGH 变为 LOW )0000000000000
问题:
此时,用户可以在低到高和高到低之间切换。意思是,流量 => 00..000(用户按下按钮,将低切换为高)111...111(用户按下按钮,将高切换为低)0000...
而且我不太确定如何启用按钮仅从低切换到高但禁用按钮从高切换到低。
意思是当用户按下按钮时,它可以将按钮状态从“0”更改为“1”,但当按钮处于“1”时无法更改按钮状态。
因此,我想请求一些帮助,以允许以下正确行为。
谢谢
代码:
const int buttonPin = 2; //the number of the pushbutton pin
const int Relay = 4; //the number of the LED relay pin
uint8_t stateLED = LOW;
uint8_t btnCnt = 1;
int buttonState = 0; //variable for reading the pushbutton status
int buttonLastState = 0;
int outputState = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Check if there is a change from LOW to HIGH
if (buttonLastState == LOW && buttonState == HIGH)
{
outputState = !outputState; // Change outputState
}
buttonLastState = buttonState; //Set the button's last state
// Print the output
if (outputState)
{
switch (btnCnt++) {
case 100:
stateLED = LOW;
digitalWrite(Relay, HIGH); // after 10s turn on
break;
case 250:
digitalWrite(Relay, LOW); // after 20s turn off
//Toggle ButtonState to LOW from HIGH without user pressing the button
digitalWrite(buttonPin, LOW);
break;
case 252: // small loop at the end, to do not repeat the LED cycle
btnCnt--;
break;
}
Serial.println("1");
}else{
Serial.println("0");
if (btnCnt > 0) {
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}
您需要设置 outputState 并让它设置直到 25 秒后重置。如果仍然按下按钮,它将在 251 上循环。
const int buttonPin = 2; //the number of the pushbutton pin
const int Relay = 4; //the number of the LED relay pin
uint8_t stateLED = LOW;
uint8_t btnCnt = 1;
bool outputState = false;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
void loop() {
outputState |= digitalRead(buttonPin); // if pushButton is high, set outputState (low does nothing)
// Print the output
if (outputState)
{
switch (btnCnt++) {
case 100:
stateLED = LOW;
digitalWrite(Relay, HIGH); // after 10s turn on
break;
case 250:
digitalWrite(Relay, LOW); // after 20s turn off
//Toggle ButtonState to LOW from HIGH without user pressing the button
outputState = false; // reset state to low
break;
case 251: // loop (it might happen, if previous step sets outputState=false but button is still pressed -> no action)
--btnCnt;
outputState = false;
break;
}
Serial.println("1");
}else{
Serial.println("0");
if (btnCnt > 0) {
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}