Arduino 在我第一次打开按钮后,LED一直打开

Arduino 在我第一次打开按钮后,LED一直打开,arduino,Arduino,我试图通过将变量e设置为1或0来将按钮转换为开关,具体取决于针脚12返回高电平还是低电平,但在按下按钮一次后,无论我再次按下按钮多少次,led都会亮起且不会熄灭 #define boton 12 #define gled 7 #define rled 4 #define yled 8 int e=0; int botonst=LOW; void setup() { // put your setup code here, to run once: pinMode(boton,INPUT

我试图通过将变量e设置为1或0来将按钮转换为开关,具体取决于针脚12返回高电平还是低电平,但在按下按钮一次后,无论我再次按下按钮多少次,led都会亮起且不会熄灭

#define boton 12
#define gled 7
#define rled 4
#define yled 8

int e=0;
int botonst=LOW;
void setup() {
  // put your setup code here, to run once:
  pinMode(boton,INPUT);
  pinMode(gled,OUTPUT);
  pinMode(rled,OUTPUT);
  pinMode(yled,OUTPUT);



}

void loop() {
  // put your main code here, to run repeatedly:
  botonst=digitalRead(boton);
  if ((botonst==HIGH) && (e=1)){
    
    digitalWrite(yled,HIGH);
    e=0;
    
  }
  
  else{
    if(e=0){
      digitalWrite(yled,LOW);
      e=1;
        }
    }
      

  
  delay(50);
      
    

  
}

```C

你的逻辑不清楚!但我猜你有按钮和led,每次点击按钮时都会尝试切换led,如果这是你想要做的,那么使用下面的代码

#define boton 12   // button conect on pin 12
#define yled 8     // led conect on pin 8

bool ledMode = false;   // this  mode use to save the current led state ( high or low ) 
int botonst=LOW;     
 
void setup() {
  pinMode(boton,INPUT); // make button as input pin
  pinMode(yled,OUTPUT); // make led as output pin
}

void loop() {
  botonst=digitalRead(boton); // read the button state
  if (botonst==HIGH){            // if button is clicked  (assume you connect the button as active high)
    ledMode ^= true;             // toggle the mode (if it true make it false and if it false make it ture)
    if(ledMode == false)         //if led mod is off
        digitalWrite(yled,HIGH);   // turn led on
     else                        // if led mode is on
        digitalWrite(yled,LOW);     // turn led off
  }
  delay(200);   //delay for Depounceing (if you use hardware Depounce technique remove it)
}

你应该试着写出你的逻辑到底在说什么。它将变得清晰。
e=1
不是一个比较。你们有下拉电阻器吗?非常感谢