For this multi-switch assignment, I connected the Arduino board to a breadboard using the 5V and GND pins to supply power. To make the layout more organized, I bridged the power rails from one side of the breadboard to the other using jumper wires, allowing me to distribute power across both halves of the board. I placed the three buttons side by side on the breadboard. Each button is connected on one side to the positive power rail, and on the other side to a digital input pin on the Arduino to read its state. A 10kOhm resistor connects each button to ground, acting as a pull-down resistor to make the button behave as expected.
For the LEDs, I used three different colors: red, yellow, and green. Each LED is connected to a separate digital output pin on the Arduino, with the negative leg connected to ground through a 100Ohm resistor to limit current. The behavior of each LED is controlled through conditional logic in the code. The first button turns the green LED on only while the button is pressed. The second button acts as a toggle, switching the yellow LED on or off with each press. The third button controls all three LEDs by triggering a looping animation sequence..
Video Demonstration
Photos
Sketch
Schematic
Code
int buttonLastState = 0;
int buttonState = 0;
int led1on = 0;
int led2on = 0;
int led3on = 0;
int btn1 = 2;
int led1 = 8;
int led2 = 9;
int btn2 = 3;
int btn3 = 4;
int led3 = 10;
int ledSequencePlaying = 0;
voidsetup()
{
pinMode(btn1, INPUT);
pinMode(led1, OUTPUT);
pinMode(btn2, INPUT);
pinMode(led2, OUTPUT);
pinMode(btn3, INPUT);
pinMode(led3, OUTPUT);
Serial.begin(9600);
}
voidloop()
{
//First Button
if (digitalRead(btn1) == HIGH) {
digitalWrite(led1, HIGH);
} else {
if(!ledSequencePlaying){
digitalWrite(led1, LOW);
}
}
//Second Button
if(digitalRead(btn2) == HIGH && buttonLastState == LOW){
if(led2on == 0){
led2on = 1;
digitalWrite(led2, HIGH);
}else{
if(led2on == 1){
led2on=0;
digitalWrite(led2,LOW);
}
}
}
buttonLastState = digitalRead(btn2);
//Serial.print("Button last state is= ");
//Serial.println(buttonLastState);
// Third Button
if(digitalRead(btn3) == HIGH){
ledSequencePlaying = 1;
}
if(ledSequencePlaying == 1){
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
digitalWrite(led1, HIGH);
delay(150);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(200);
digitalWrite(led3, LOW);
delay(500);
}
Serial.print("the red light is: ");
Serial.println(led1on);
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.