switch turn on a led, timing turn it off

Hi,

I am completely new to Arduino, been copying and pasting part of other sketches found in internet and so far so good, but can find any reference on this, though should be something very easy to achieve:

I have 2 led and 2 momentary switches: - press switch1 turn on led1 after 15sec led1 turns off, if during these 15sec switch1 was pressed led1 turns off. - press switch2 turn on led2 after 15sec led2 turns off, if during these 15sec switch2 was pressed led1 turns off.

  • while led1 is on, pressing switch2 turns off led1 and turns on led2.
  • while led2 is on, pressing switch1 turns off led2 and turns on led1.

been using so many different setup but none seems to be working, board wiring are all ok as with other instructions less tuns on and off on switches command.

with these programming I can turn led on with its switch and revert the led with opposite switch but won't turn off after timing neither re-pressing its own switch:

const int buttonSX = A1; const int ledSX = 12; unsigned long off_timeSX; boolean ledStateSX=false; int buttonStateSX = 0; const int buttonDX = A3; const int ledDX = 9; unsigned long off_timeDX; boolean ledStateDX=false; int buttonStateDX = 0;

void setup() { pinMode( buttonSX, INPUT ); pinMode( buttonDX, INPUT );

pinMode( ledSX, OUTPUT ); pinMode( ledDX, OUTPUT );

digitalWrite( ledSX, LOW ); digitalWrite( ledSX, LOW ); }

void loop() {

if ((ledStateSX) && (millis()>=off_timeSX)) /* is it on and is it later or equal to off_time / { digitalWrite(ledSX,LOW); ledStateSX = false; } else if (!ledStateSX) / is it off? */ { buttonStateSX = digitalRead(buttonSX); if(buttonStateSX == HIGH) { digitalWrite(leddX, LOW); digitalWrite(ledSX, HIGH); ledStateSX = true; off_timeSX = millis() + 5000; } }

if ((ledStateDX) && (millis()>=off_timeDX)) /* is it on and is it later or equal to off_time / { digitalWrite(ledDX,LOW); ledStateDX = false; } else if (!ledStateDX) / is it off? */ { buttonStateDX = digitalRead(buttonDX); if(buttonStateDX == HIGH) { digitalWrite(ledSX, LOW); digitalWrite(ledDX, HIGH); ledStateDX = true; off_timeDX = millis() + 5000; } } }

How are your switches wired?

Complex if statements are not the way to go. Simple, nested if necessary, is statements are.

You really need to look at the state change detection example. You want to do something when the switch BECOMES pressed, not when the switch IS pressed. From the Arduino point of view, the switch BECOMES pressed once, while the switch IS pressed many thousands of iterations of loop(), even for a (by human standards) very short press.

Deal with the compound issues separately. Has the appropriate switch become pressed? If so, is the appropriate LED on? That is different than "has the switch become pressed and the LED is on?" The first involves an if statement in the body of an if statement. The second involves a compound if statement. Compound if statement are harder to understand and harder to get right and harder to see that all possible conditions are dealt with.

Turning the LED off after it has been on for x amount of time is independent of is the switch pressed or not.

Global variables are needed when the variable needs to be referenced by multiple functions, such as setup() and loop(). The pin number variables need to be global. The pin state variables do not, and should not. When a variable needs to retain its value from one iteration of a function to the next, as the previous state variables will need to, when you add them, the variable should be declared static in the function, rather than global.

Perhaps the way the demo several things at a time illustrates the use of millis() will give you some ideas.

...R

Hi Guys,

thanks for your time, the post several things is still beyond my capabilities.

Had a look to ButtonStateChangeDetection and BlinkWithoutDelay tutorials. I can easily integrate the button state change and turn on and off the led when pressed and also turn off the opposite led when switch is pressed, but seems I cannot integrate the 2 and have the led to turn off after a preset..

this way led go on and off by switch command: buttonStateSX = digitalRead(buttonSX); if ( buttonStateSX != lastButtonStateSX ) { if ( buttonStateSX == HIGH ) { if ( statoLedSX == LOW ) statoLedSX = HIGH;

else statoLedSX = LOW; } digitalWrite( ledSX, statoLedSX ); delay(100); } lastButtonStateSX = buttonStateSX;

but if I insert the mills function, led still only gets on/off by switch command no timing..:

buttonStateSX = digitalRead(buttonSX); if ( buttonStateSX != lastButtonStateSX ) { if ( buttonStateSX == HIGH ) { if ( statoLedSX == LOW ) statoLedSX = HIGH; else statoLedSX = LOW; { if ( statoLedSX == HIGH ); unsigned long currentMillis = millis(); if ( currentMillis - previousMillis > interval ) { previousMillis = currentMillis; statoLedSX = LOW; } } } digitalWrite( ledSX, statoLedSX ); delay(100); } lastButtonStateSX = buttonStateSX;

You need to work on your indenting. The compiler doesn't care, but the way you have your code indented makes me think that you believe the indent level is important.

If that is the case, your understanding is wrong.

Nothing goes on the line after the {.

Use Tools + Auto Format to see how the compiler understands what happens when.

Perhaps then your issues will become apparent.