Simple button switcher with LED indication

Hi, I have very simple problem, but I cant find the right simple solution. Please can anyone help me? : I have a button sensor that indicates if it is HIGH or LOW. What I need is to light up the LED when I press the button for the first time, keep the LED shining when I hold the button and also keep it on when move finger away and the sensor input indicates LOW. Than when I press the button once again the led might turn out.

So it should work like ON/OFF button. Press once and keep the LED shining all the time and than press once again and LED stop shining - indicates OFF.

Very simple, but for me not...

Please can you help me how tosolve this issue?

Thanks a lot!

JURA--

I think something like this would probably work. You just need to keep track of the LED's state in a variable:

define LEDPIN 13

define BUTTONPIN 2

void setup() { pinMode(LEDPIN, OUTPUT); pinMode(BUTTONPIN, INPUT); }

int ledState = LOW;

void loop() { while (digitalRead(BUTTONPIN) == HIGH); // wait until button is pressed ledState = ledState == HIGH ? LOW : HIGH; // change LED state digitalWrite(LEDPIN, ledState); // set LED to new state while (digitalRead(BUTTONPIN) == LOW); // wait until button is released }

(This assumes that the button brings the pin low; if not, simply reverse the while loops.)

Good luck!

Mikal

Thanks Mikal!

unfortunatelly it doesnot work... what is this part of code: ledState = ledState == HIGH ? LOW : HIGH; // change LED state its really strange for me and I dont understand it...

I had idea somathing like this:

define all variables....

constrain (X,0,1) BUTTONPIN=digitalRead(SensPin1);

void loop() { if ( BUTTONPIN==HIGH && X==0) { digitalWrite(LED, HIGH); delay(1000); X += 1; // X is variable that might detect, if the button was pressed before or not BUT here I want to add just 1 / not more... I dont know how. What happens with multiple additions +1 when I use constrain(X,0,1) in the begining of code??? } if ( BUTTONPIN== LOW && X==1) { digitalWrite (LED, HIGH); } if ( BUTTONPIN==HIGH && X==1) { digitalWrite (LED, LOW); delay(1000); X -= 1; } if (BUTTONPIN==LOW && X==0) { digitalWrite (LED,LOW); } }

Thanks for any idea for improving or rebuilding...

:)

Mikal's expression toggles the value of ledState using the C ternary operator. It looks strange at first but is quite useful writing if/then/else statements.

The expression above is the same as the following:

if(ledState == HIGH) ledState = LOW; // set the state LOW if it was HIGH else ledState = HIGH; // state was LOW set set it HIGH

here is how the ternary operator works:

result = ledState == HIGH [glow]?[/glow] LOW [glow]:[/glow] HIGH
          \_____________/    |      | 
                 |           |       \- result will be this if ledState was LOW   
                 |           \ - result will be this if ledState was  HIGH
                 |
                 \ - this expression is evaluated, the result will be the value
                      after the [glow]?[/glow] if true, or after the [glow]:[/glow] if false

Thanks - interesting!

What about: [UNTESTED]

boolean bCurrentlyOn = false;

void setup(void){/*your code*/}

void loop(void)
{
      if(digitalRead(BUTTONPIN)==LOW && bCurrentlyOn)
      {
            digitalWrite(LEDPIN,LOW);
            bCurrentlyOn=false;
            delay(10);//do not know if needed, but I think it is or else the next if block will execute
      }
      else if(digitalRead(BUTTONPIN)==LOW && !bCurrentlyOn)
      {
            digitalWrite(LEDPIN,HIGH);
            bCurrentlyOn=true;
            delay(10);//do not know if needed, but I think it is or else the next if block will execute
      }
}

The delay is needed to debounce the switch. When you press a button the contact opens and closes rapidly for a short period (10 to 20 milliseconds) so you should wait for the contact to stop bouncing before leaving the if statement

Hi

I am using an old footswitch to switch a laser on and off and mikalhart's solution is very nice.

The scripts main part sends midi controller values and I dont want that to stutter by adding delay but the footswitch "spills" a lot. So I need the switch state to be less sensitive, maybe using millis?

Thankful for advice

fubbi

If you can re-wire the switch, then you can de-bounce it it hardware using the standard circuit with a set-reset latch. Is re-wiring the switch a possibility in this case?

Hi

it's an old east german thing that doesn't look like it wants to be opened

outside of that, it's still hanging on the breadboard just lighting up an LED for now

fubbi