Identify bugs and resolve it to fulfill following objective. Interface 3 LEDs and 4 SWICHES with Arduino. Develop a program that by pressing 1st switch it will ON LED1, by pressing 2nd switch it ON LED2 and same for 3rd switch and LED3. By pressing 4th switch it will reset (OFF) all LEDs.
const int LED1 = 7;
const int LED2 = 6;
const int LED3 = 5;
const int LED4 = 8;
const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int BUTTON3 = 0;
const int BUTTON4 = 1;
int Buttonmodel = 0;
int Buttonmode2 = 0;
int Buttonmode3 = 0;
int Buttonmode4 = 0;
void setup [] {
// put your setup code here, to run once:
pinMode(LeD1,INPUT);
pinMode(LEd2,input);
pinMode(LED1,INPUT);
pinMode(LED2,input)
pinMode(BUT0N1,INPUT_);
pinMode(BuT0N2,INPUT_);
pinMode(BUTON3,INPUT_);
pinMode(BUTON4,INPUT_);
}
void loop[] (
// put your main code here, to run repeatedly:
//First LED interface
Buttonmode1 == digitalread(BUTT0Nl);
if(Buttonmode1 = high)
{
digitalwrite(LED1,HIGH);
else
digitalWrite(LED1,HIGH);
}
//Second LED interface
Butonmode2 = digita1Read(BUTTON2);
if(Buttonmode2 = LoW)
{
digitalWrite(LED2,L0W);
}
else
digitalwrite(LEd2,HIGH);
}
//Third LED interface
Buttonmode3 = digitalread(BUTTON3);
if(Buttonmode3 = HIGH);
{
digitalWrite(LED3,LOW);
}
else
{
digitalwrite(LED3,HIGH)
}
//Fourth LED interface
Buttonm0de4 = digiatlRead(BUTTON4);
if(Buttonmode4 = LOW)
{
digitalWrite(LeD4,LOW)
}
else
{
digitalWrite(LED4,HIGH);
}
}
}
Ans :-
Correct Code:
const int LED1 = 7;
const int LED2 = 6;
const int LED3 = 5;
const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int BUTTON3 = 0;
const int BUTTON4 = 1;
int Buttonmodel = 0;
int Buttonmode2 = 0;
int Buttonmode3 = 0;
int Buttonmode4 = 0;
void setup () {
// put your setup code here, to run once:
pinMode(LED1,OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BUTTON4,INPUT);
}
void loop(){
// put your main code here, to run repeatedly:
//First LED interface
Buttonmode1 == digitalRead(BUTTONl);
if(Buttonmode1 = HIGH)
{
digitalWrite(LED1,HIGH);
}
//Second LED interface
Butonmode2 = digita1Read (BUTTON2);
if(Buttonmode2 = HIGH)
{
digitalWrite(LED2,HIGH);
}
//Third LED interface
Buttonmode3 = digitalRead(BUTTON3);
if(Buttonmode3 = HIGH);
{
digitalWrite(LED3,HIGH);
}
//Fourth LED interface
Buttonmode4 = digitalRead(BUTTON4);
if(Buttonmode4 = HIGH)
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
}
}
Comments
Post a Comment