Go Down

Topic: Toggling led with button switch problem (Read 1 time) previous topic - next topic

_Sander_1

In my arduino project I have to toggle my LED (or something other) with pressing the button switch. So if I press the button once, the LED turns on, and if I press the button again, the LED turns off. My code is working in another programming language, "Java", but it isn't working in the arduino programming software. Here is my code:


boolean ledon = false;

void setup(){
  Serial.begin(9600);
  pinMode(33, INPUT); //Defining the button input
  pinMode(12, OUTPUT); //Defining the LED output
}
void loop(){
  delay(200); //The delay that I need to press and release the button
 
  //Toggles the "LED-boolean"
  if(digitalRead(33) == 1){
    if(ledon == false){
      ledon = true;
    }
    else if(ledon == true){
      ledon = false;
    }
  }

  //Turns on or off the LED
  if(ledon == true){
    digitalWrite(12, HIGH); //
  }
  if(ledon == false){
    digitalWrite(12, LOW);
  }
 
}

I'm sorry if my English isn't good. I'm dutch  :D

JimboZA

Since you release the button during the delay, it will never be read as pressed.

Have a look at this example, where you keep track of the button's state.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

nilton61

Code: [Select]
const byte led = 12;
const byte btn = 33;
int btnState;

void setup(){
    pinMode(led, OUTPUT);
}//setup()

void loop(){
  if((btnState=(btnState<<1)|(digitalRead(btn)&3))==1) digitalWrite(led, !digitalRead(led));
}//loop()

_Sander_1

Thanks for the reply, but my problem is not with the delay, but a part of the code isn't running or not running correctly. Here is my new code without the delay. The part between the hashtags isn't running. I tried to print something in the serial window, but it didn't do it. Here is my code:


int buttonState = 0;
int lastButtonState = 0;

int LEDstate = 0;

void setup(){
 Serial.begin(9600);
 
 pinMode(12, OUTPUT);
 pinMode(33, INPUT);
 
}
void loop(){
 
 if(digitalRead(33) == 1){
   while(digitalRead(33) == 1){
     
   }
   if(LEDstate == 0){
     
     Serial.println("NO");
     LEDstate = 1;
   }
#
   else if(LEDstate == 1){
     LEDstate = 0;
     Serial.println("Turn LED off"); //This part is not running, there is nothing printed in the Serial.
   }
#    
   
   
 }
 
 Serial.println(LEDstate);
 
 if(LEDstate == 1){
   digitalWrite(12, HIGH);
 }
 else{
   digitalWrite(12, LOW);
 }
 
 
}

Vaclav


Here is modified code and the part in question works.
It is "forced" to start with iRead true, so you may play with it to start with false.
Sorry, got to run.



int buttonState = 0;
int lastButtonState = 0;

int LEDstate = 0;

void setup() {
  Serial.begin(115200);

  pinMode(12, OUTPUT);
  pinMode(33, INPUT);

}
void loop() {
int iRead = 1; 
  if (iRead) {
    while (iRead) {
 Serial.println("iRead = 1 ");     
 Serial.println("while  iRead = 1 ");
      delay(3000);
      iRead = 0;     // change after time delay 
    }
  Serial.println("iRead = 0 ");
 
    if (LEDstate == 0) {
      Serial.println("Set LED on");
      LEDstate = 1;
    }

    else //  if (LEDstate == 1) no need to check - it gets here if LEDstate = 1 anyway
    {
      LEDstate = 0;
      Serial.println("Turn LED off"); //This part is not running, there is nothing printed in the Serial.
    }
  }
  Serial.print(" LEDState  ");
  Serial.println(LEDstate);

  if (LEDstate == 1) {
    digitalWrite(12, HIGH);
  }
  else {
    digitalWrite(12, LOW);
  }


}

_Sander_1

Thanks for the reply, I tested it, but it didn't work. I think I found it by trying to use the analog inputs instead of using the digital in/outputs. When I use digitalRead() and digitalWrite() in 1 sketch, my arduino will still think the digital input is HIGH. So there is something wrong with my arduino I think. But if it isn't, it's that I can't use digitalRead() and digitalWrite() in 1 sketch.

Thanks,

_Sander_1

GoForSmoke

Does this work for you?
Code: [Select]

byte buttonPin = 6; // I am using an UNO, my button is a jumper.
                    // change the pin to suit yourself
byte ledPin = 13; // I am using the onboard led.

void setup( void )
{
  pinMode( buttonPin, INPUT_PULLUP ); // using internal resistor
  pinMode( ledPin, OUTPUT );
}

void loop( void )
{
  digitalWrite( ledPin, !digitalRead( buttonPin )); 
}

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

PaulS

If you want to press a switch once to have event 1 occur, and press it again to have event 2 occur, you need to count presses, and base actions on count, not switch state.

You need to recognize when a switch BECOMES pressed, not IS pressed. You are not doing that. The State Change Detection example shows how.

You may (or may not) need to debounce the switch.
The art of getting good answers lies in asking good questions.

nilton61

Or another way to put it, a toggling state machine must have 4 states

Go Up