akshay_90:
Tried this thing with nested if but not possible from my side please help.
You are thinking about your program the wrong way.
A program is based on data structures and algorithms.
It’s up to you providing these two things, the program will then do the rest.
A good data structure for a whole bunch of mechanical switches and buttons is an “array”.
A good algorithm to work over an arrray is a “for-loop”.
A good algorithm for the loop() function with Arduino sketches is the IPO programming model:
You just need to write three functions of your own, the loop then always looks like this:
void loop() {
input();
processing();
output();
}
And following the three steps over and over again
- obtain input from the outer world outside the Arduino
- do internal processing within the Arduino RAM
- send output to the world outside the Arduino
you can write any program you want.
To give you a starting point, I’ll provide you my default sketch that can read the input of as many buttons and switches as you like to have. Debouncing of mechanical switches may be tricky, so the input function is tricky, too. But you just need copy-and-paste, so the “input()” function for your program is ready without any work for you by just providing the correct pin numbers for all the buttons and switches:
#define INPUTMODE INPUT_PULLUP // INPUT or INPUT_PULLUP
#define BOUNCETIME 5 // bouncing time in milliseconds
byte buttonPins[]={2, 3, 4, 5, 6, 7};// pin numbers of all buttons
#define NUMBUTTONS sizeof(buttonPins) // number of buttons (automatically calculated)
byte buttonState[NUMBUTTONS]; // array holds the actual HIGH/LOW states
byte buttonChange[NUMBUTTONS]; // array holds the state changes when button is pressed or released
enum{UNCHANGED,BUTTONUP,BUTTONDOWN};
void input(){
// read the input state and state changes of all buttons
static unsigned long lastButtonTime; // time stamp for remembering the time when the button states were last read
memset(buttonChange,0,sizeof(buttonChange)); // reset all old state changes
if (millis()-lastButtonTime<BOUNCETIME) return; // within bounce time: leave the function
lastButtonTime=millis(); // remember the current time
for (int i=0;i<NUMBUTTONS;i++)
{
byte curState=digitalRead(buttonPins[i]); // current button state
if (INPUTMODE==INPUT_PULLUP) curState=!curState; // logic is inverted with INPUT_PULLUP
if (curState!=buttonState[i]) // state change detected
{
if (curState==HIGH) buttonChange[i]=BUTTONDOWN;
else buttonChange[i]=BUTTONUP;
}
buttonState[i]=curState; // save the current button state
}
}
void output(){
// send a message to Serial if a button state (pressed/released) has changed
// button pressed: Send button pin number with minus sign
// button released: Send button pin number
byte action;
for (int i=0;i<NUMBUTTONS;i++)
{
switch (buttonChange[i])
{
case BUTTONUP: Serial.println(buttonPins[i]);break;
case BUTTONDOWN: Serial.println(-buttonPins[i]);break;
}
}
}
void setup() {
Serial.begin(9600); // initialize Serial at 9600 baud
// then initialize all buttons
for (int i=0;i<NUMBUTTONS;i++) pinMode(buttonPins[i],INPUTMODE);
}
void loop() {
input();
// processing(); // if you need any processing, write a function for it!
output();
}
The output() function currently just shows what’s going on and sends messages to Serial when you press or release a button or a switch changes its state.
The INPUTMODE must be defined accordingly to your circuit:
If you should connect the buttons/switches just from GND to the input pin, use INPUT_PULLUP.
If you use additional “pull-down” resistors in your circuit, use INPUT.
So perhaps this might be a starting point for you, to work out the “processing()” and “output()” logic that you need for your sketch.