Three functions with one button - help?

Hi everyone! I have so far done a couple of basic projects with Arduino, but now I am trying to do something more dificult. Basically I have a switch and when it's on a red LED is on. When it's off a green LED is on.However what I want to do is turn both LEDs off by pressing the button for longer. I don't think that this is possible with a switch, so I's appreciate any suggestions on how to do it.

If by switch you mean, a switch statement, then yes, it is possible. If by switch you mean, a physical switch (that you flip), then I have no idea.

Hope this helped?..

PS: I'll need a bit more about what you are trying to do before I can help setting up the switch statement to do what you want.

Thanks for the quick reply :) By switch I mean a physical switch - a pushbutton. At the moment I only have the following code regarding the switch:

void switch_check() { SwitchState=digitalRead(SWITCH); while (SwitchState==1) { SwitchState=digitalRead(SWITCH); } pinMode (RedLED, HIGH); pinMode (GreenLED, LOW);

SwitchState=digitalRead(SWITCH); while (SwitchState==0) //ON { SwitchState=digitalRead(SWITCH); } pinMode (RedLED, LOW); pinMode (GreenLED, HIGH); }

Obviously with that code I can only check if the switch is on or off, respectively if the green led is on and the red one off or vice versa.

I saw the switch case in the Arduino reference and thought that this might be what I need, but as I've never done it I am not really aware of how to do that.

Here’s a concept that might work.

I figure what you need to do is: on button turning on enter a loop, and stay in that loop until the button is off, and use a variable to count the number of iterations of that loop. When outside the loop you check if the number of iterations is greater than N (long press) or lower than N (short press).

Possibly you could introduce some microsecond delays within the loop to stop the processor from counting needlessly.

Hope this helps.

One each pass through loop, note whether the button is pressed but wasn’t before or is released but wasn’t before. That is, note whether the button state has changed.

If it has, use millis() to capture the time. Use separate variables (global) to record when the button state changed to pressed and when the button state changed to released.

When the button state changes to released, you can then determine how long the button was pressed, and react accordingly.

You are not the first person that has wanted to do this:

Thank you for the help. I managed to get it working by modifying this code

did you ever try the pulseIn(). Just seems A LOT simpler. Like in their example code simply set duration = pulseIn (pin #, HIGH). From there run a simple if/else statement. If less than xxxx then do this If held longer than xxxx then do this.

A lot less code but I am new and may be missing something.