SINGLE PUSH BUTTON SWITCH

single push button 1

The code can be donwloaded at :

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/Single_push_button.zip

int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;
int inPin = 2; // pushbutton connected to digital pin 2
int val = 0; // variable to store the read value

int index = -1; // to indicate which led should be turned on

// the setup function runs once when you press reset or power the board
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(inPin, INPUT);
}// the loop function runs over and over again forever
void loop() {
val = digitalRead(inPin);
if(val == 1){ // the button is pushed
// turn off the LED which is on now
if(index != -1){ // if index is not the initial value
switch (index){
case 0:
digitalWrite(ledPin1, LOW); // turn the LED off (LOW is the voltage level)
delay(50);
break;
case 1:
digitalWrite(ledPin2, LOW);
delay(50);
break;
case 2:
digitalWrite(ledPin3, LOW);
delay(50);
break;
case 3:
digitalWrite(ledPin4, LOW);
delay(50);
break;

}
}

// turn on the next LED
index ++;
index = index % 5;
switch (index){
case 0:
digitalWrite(ledPin1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250);
break;
case 1:
digitalWrite(ledPin2, HIGH);
delay(250);
break;
case 2:
digitalWrite(ledPin3, HIGH);
delay(250);
break;
case 3:
digitalWrite(ledPin4, HIGH);
delay(250);
break;
case 4:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
delay(250);
break;

}
}
}

==================OTHER SKET============

// Define the pins being used
int pin_LED1 = 10;
int pin_LED2 = 9;
int pin_LED3 = 8;
int pin_LED4 = 7;

int pin_switch = 2;

// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
boolean newSwitchState4 = LOW;

byte state = 0;

void setup()
{
Serial.begin(9600);
Serial.print(“Sketch: “); Serial.println(__FILE__);
Serial.print(“Uploaded: “); Serial.println(__DATE__);
Serial.print(“Time Uploaded: “); Serial.println(__TIME__);
Serial.println(” “);

pinMode(pin_LED1, OUTPUT); digitalWrite(pin_LED1,LOW);
pinMode(pin_LED2, OUTPUT); digitalWrite(pin_LED2,LOW);
pinMode(pin_LED3, OUTPUT); digitalWrite(pin_LED3,LOW);
pinMode(pin_LED4, OUTPUT); digitalWrite(pin_LED4,LOW);

pinMode(pin_switch, INPUT);
}

void loop()
{
newSwitchState1 = digitalRead(pin_switch);
delay(10);
newSwitchState2 = digitalRead(pin_switch);
delay(10);
newSwitchState3 = digitalRead(pin_switch);
delay(10);
newSwitchState4 = digitalRead(pin_switch);
delay(10);

// if all 4 values are the same we can continue
if ( (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3)&& (newSwitchState1==newSwitchState4) )
{

if ( newSwitchState1 != oldSwitchState )
{

// has the button switch been closed?
if ( newSwitchState1 == HIGH )
{
// increase the value of state
state++;
if (state > 4) { state = 0; }

// turn all LEDs off. Doing it this way means we do not need to care about the individual LEDs
// simply turn them all off and then turn on the correct one.
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);
digitalWrite(pin_LED3, LOW);
digitalWrite(pin_LED4, LOW);

// Turn on the next LED
// Because the value of state does not change while we are testing it we don’t need to use else if
if (state==1) { digitalWrite(pin_LED1, HIGH); }
if (state==2) { digitalWrite(pin_LED2, HIGH); }
if (state==3) { digitalWrite(pin_LED3, HIGH); }
if (state==4) { digitalWrite(pin_LED4, HIGH); }

}
oldSwitchState = newSwitchState1;
}
}
}

==================OTHER SKET====================

// Define the pins being used
int pin_LED1 = 10;
int pin_LED2 = 9;
int pin_LED3 = 8;
int pin_LED4 = 7;

int pin_switch = 2;

// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
boolean newSwitchState4 = LOW;

byte state = 0;

void setup()
{
Serial.begin(9600);
Serial.print(“Sketch: “); Serial.println(__FILE__);
Serial.print(“Uploaded: “); Serial.println(__DATE__);
Serial.print(“Time Uploaded: “); Serial.println(__TIME__);
Serial.println(” “);

pinMode(pin_LED1, OUTPUT); digitalWrite(pin_LED1,LOW);
pinMode(pin_LED2, OUTPUT); digitalWrite(pin_LED2,LOW);
pinMode(pin_LED3, OUTPUT); digitalWrite(pin_LED3,LOW);
pinMode(pin_LED4, OUTPUT); digitalWrite(pin_LED4,LOW);

pinMode(pin_switch, INPUT);
}

void loop()
{
newSwitchState1 = digitalRead(pin_switch);
delay(10);
newSwitchState2 = digitalRead(pin_switch);
delay(10);
newSwitchState3 = digitalRead(pin_switch);
delay(10);
newSwitchState4 = digitalRead(pin_switch);
delay(10);

// if all 4 values are the same we can continue
if ( (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3)&& (newSwitchState1==newSwitchState4) )
{

if ( newSwitchState1 != oldSwitchState )
{

// has the button switch been closed?
if ( newSwitchState1 == HIGH )
{
// increase the value of state
state++;
if (state > 4) { state = 0; }

// turn all LEDs off. Doing it this way means we do not need to care about the individual LEDs
// simply turn them all off and then turn on the correct one.
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);
digitalWrite(pin_LED3, LOW);
digitalWrite(pin_LED4, LOW);

// Turn on the next LED
// Because the value of state does not change while we are testing it we don’t need to use else if
if (state==1) {
digitalWrite(pin_LED1, HIGH);
digitalWrite(pin_LED2, HIGH);
digitalWrite(pin_LED3, HIGH);
digitalWrite(pin_LED4, HIGH);

}
if (state==2) {
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, HIGH);
digitalWrite(pin_LED3, HIGH);
digitalWrite(pin_LED4, HIGH);
}
if (state==3) {
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);
digitalWrite(pin_LED3, HIGH);
digitalWrite(pin_LED4, HIGH);
}
if (state==4) {
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);
digitalWrite(pin_LED3, LOW);
digitalWrite(pin_LED4, HIGH);
}
}
oldSwitchState = newSwitchState1;
}
}
}