Hi Nikhil_m,
I'm not sure how far you've gotten with the code ...?
How does the user select a scenario? By pressing one of the Sx buttons?
Once a scenario is selected, I'd imagine that the code waits for a key to be pressed.
If at least one of the LEDs isn't off, then the LED matching the switch should be switched off.
If however all LEDs are already off then the new scenario should be applied.
I'd define the PINs for that something like so:
#define LEDPIN1 6
#define LEDPIN2 7
#define LEDPIN3 8
#define LEDPIN4 9
#define LEDPIN5 10
The numbers 6-10 are just example numbers - modify to what you're using.
Since you seem to be using regular LEDs, I'd use an array to hold the status (on or off) of each LED.
Something like this:
bool LEDs[5]; // hold status of each individual LED
The same way I'd make a function to see if all LEDs are OFF:
bool AllLEDsOff() {
// LEDs[0] || LEDs[1] = LEDs[0] or LEDs[1] -> so if one of these is TRUE the result will be "true".
// But we want the opposite, so !true = false
return = !(LEDs[0]||LEDs[1]||LEDs[2]||LEDs[3]||LEDs[4]);
}
And we'd want to have a function to turn a LED on or off (pass LED pin number and true=ON and false=OFF).
void setLED(int LEDpin, bool LEDOn) {
if (LEDOn) {
digitalWrite(LEDpin, HIGH);
} else {
digitalWrite(LEDpin, LOW);
}
}
In setup we also have to set all LED pins to being outputs;
pinMode(LEDPIN1, OUTPUT);
pinMode(LEDPIN2, OUTPUT);
pinMode(LEDPIN3, OUTPUT);
pinMode(LEDPIN4, OUTPUT);
pinMode(LEDPIN5, OUTPUT);
For setting a scenario I'd make a function, something like this:
void setScenario(int S) {
// Note:
// Arrays start counting with zero, so the 1ste LED would be index 0
// Below I use || (or) to determine which LEDs should be on or off
// for example: LEDs[0] = true when S=1 or S=2 or S=4
// so for Scenario's 1, 2 and 4 (that's why I aligned them a little weird to make it more readable)
LEDs[0] = (S=1)||(S=2)|| (S=4); // note: array indexes start counting with zero ...
LEDs[1] = (S=1)|| (S=3) ;
LEDs[2] = (S=2)||(S=3)||(S=4);
LEDs[3] = (S=1)|| (S=4);
LEDs[4] = (S=2)|| (S=4);
setLED(LEDPIN1, LEDs[0]);
setLED(LEDPIN2, LEDs[1]);
setLED(LEDPIN3, LEDs[2]);
setLED(LEDPIN4, LEDs[3]);
setLED(LEDPIN5, LEDs[4]);
}
Next I'll start with setting the array to false, all LEDs off, by calling setScenario(0) -> this will result in all being false since all equations fail and all LEDs to OFF.
We should call that in the setup() function as well.
Now in the loop() I'd wait for buttons being pressed.
When a button is being pressed, I'd first make sure that we are not looking at scene selection (all LEDs off).
So if AllLEDsOff==true then all LEDs are off and we need to call setScenario to populate the array for the correct scenario.
If AllLEDsOff==false, and at least one LED is still on, then we need to switch a LED off based on the switch that was pressed.
Does this help to get started? (none of the code has been tested)