Starting of Arduino class project:
Light up both LEDs simultaneously while the switch is pushed on. And turn them off when the switch is turned off or not pushed on.
Components:
- Breadboard
- Arduino UNO R3
- 3 Resistor of 322 ohm
- Push Switch
- Green LED
- Red LED
Code
int redLed=12;
int greenLed=13;
int swt=7; int val=0;
void setup() { pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(swt, INPUT);
}
void loop() { val=digitalRead(swt);
if(val==HIGH){ digitalWrite(redLed,HIGH);
digitalWrite(greenLed,HIGH);
}else { digitalWrite(redLed,LOW);
digitalWrite(greenLed,LOW);
}
delay(1000);
}
Image 1: Thinkercad website a[er Log in
Image 2: Preparing to create new Circuit
Image 3: New Circuit Interface
Image 4: Components
Image 5: Circuit built
Image 6: Error in code
Image 7: Error fixed and the green, but a bug appears to light up the green LED
Image 8: While pushed it’s turning both LEDs on
Image 9: Bug fixed and the LEDs are ligh bng a[er pushing the switch
Code
int redLed=12;
int greenLed=13;
int swt=7; int val=0;
void setup() { pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(swt, INPUT);
}
void loop() { val=digitalRead(swt);
if(val==HIGH){ digitalWrite(redLed,HIGH);
digitalWrite(greenLed,HIGH);
}else { digitalWrite(redLed,LOW);
digitalWrite(greenLed,LOW);
}
delay(1000);
}
Modified Project-1
We are trying to modify the class project to light up the LEDs separately. Our goal is to light up the Green LED turned on when the switch is turned off (val = 0). And Red LED lights up when the switch is pushed (val=1).
Code
int redLed=12;
int greenLed=13;
int swt=7;
int val=0;
void setup() {
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(swt, INPUT);
}
void loop() {
val=digitalRead(7);
if(val==HIGH){
digitalWrite(redLed,HIGH);
digitalWrite(greenLed,LOW);
}
else { digitalWrite(redLed,LOW);
digitalWrite(greenLed,HIGH);
}
delay(1000); // put your main code here, to run repeatedly:
}
Image 10: Green is Lighting while switch is not pushed on.
Image 11: Red is Lighting while switch is pushed on.
Modified Project-2
We are going to remove the switch to blink the LEDs separately
Code
int redLed = 12;
int greenLed = 13;
void setup() {
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(7,INPUT);
}
void loop(){
digitalWrite(redLed,HIGH);
digitalWrite(greenLed,LOW);
delay(700);
digitalWrite(redLed,LOW);
digitalWrite(greenLed,HIGH);
delay (700);
}
Image 11: Red LED is lighꢀng up
Image 12: Green LED is lighꢀng up