write a program which implements this decision chain Read fo
write a program which implements this decision chain: Read four buttons. If button 2 is not pressed any lit LEDs should stay lit. If button 1 is pressed, the Green LED will turn on. If button 3 is pressed, the Blue LED will turn on. If button 4 is pressed, turn on the Laser. If button 1 is not pressed, the Red LED will turn on. If button 4 is not pressed, turn off the laser. If button 3 is not pressed,the Red LED will turn on. If button 2 is pressed, any lit LEDs will blink at 2 Hz. i need it in arduino please
Solution
If button 2 is not pressed any lit LEDs should stay lit.
If button 1 is pressed, the Green LED will turn on.
If button 3 is pressed, the Blue LED will turn on.
If button 4 is pressed, turn on the Laser.
If button 1 is not pressed, the Red LED will turn on.
If button 4 is not pressed, turn off the laser.
button 3 is not pressed,the Red LED will turn on.
If button 2 is pressed, any lit LEDs will blink at 2 Hz
Program:
int delay_v = 250; //0.25 seconds [2Hz has time period of 0.5second ]
int ledB_pin = 13; // pin number initialization
int ledG_pin = 12;
int ledR_pin = 0;
int laser_pin = 5;
int button_B1 = 1;
int button_B2 = 2;
int button_B3 = 3;
int button_B4 = 4;
int status_B1 =0 ; // variable for status of buttons
int status_B2 =0 ;
int status_B3 =0 ;
int status_B4 =0 ;
int status_LEDB=0; // TO CHECK WHICH LED is ON.
int status_LEDG=0;
int status_LEDR=0;
void setup()
{
pinMode(ledB_pin, OUTPUT);
pinMode(ledG_pin, OUTPUT);
pinMode(ledR_pin, OUTPUT);
pinMode(laser_pin, OUTPUT);
pinMode(button_B1, INPUT);
pinMode(button_B2, INPUT);
pinMode(button_B3, INPUT);
pinMode(button_B4, INPUT);
}
//loop() runs over and over again
void loop() {
status_B1=digitalRead(button_B1); //LOW when button is pressed & HIGH when released
status_B2=digitalRead(button_B2);
status_B3=digitalRead(button_B3);
status_B4=digitalRead(button_B4);
if (status_B1== LOW) { // B1 pressed
digitalWrite(ledG_pin, HIGH);
status_LEDG=HIGH;
} else {
digitalWrite(ledR_pin, HIGH);
status_LEDR=HIGH;
}
if(status_B3==LOW) {
digitalWrite(ledB_pin, HIGH);
status_LEDB=HIGH;
} else {
digitalWrite(ledR_pin, HIGH);
status_LEDR=HIGH;
}
if (status_B4== LOW) {
digitalWrite(laser_pin, HIGH);
} else {
digitalWrite(laser_pin, LOW);
}
if (status_B2== LOW) {
if(status_LEDG== HIGH){
digitalWrite(ledG_pin, HIGH);
delay(delay_v);
digitalWrite(ledG_pin, LOW);
delay(delay_v);
}
else {
if(status_LEDR== HIGH){
digitalWrite(ledR_pin, HIGH);
delay(delay_v);
digitalWrite(ledR_pin, LOW);
delay(delay_v);
}
else {
if(status_LEDB== HIGH){
digitalWrite(ledB_pin, HIGH);
delay(delay_v);
digitalWrite(ledR_pin, LOW);
delay(delay_v);
}
}
}
}
}
}

