For this project, I went with a simple on and off switch. I used 4 buttons to make two on and off switches. I used one pair to turn the blue LED on and off when pressed. I used the other pair to turn the white LED on and off and make the red LED blink when I turned off the white LED. The blue and white LEDs are connected with 1k resistors and the red LED is connected with the 330 resistor. After reading the lesson on digital inputs, I decided to follow the diagram in the lesson and not use the extra resisitors.
Materials Used:
1 Arduino
3 LED lights: Red, White and Blue
4 Buttons
1 USB connector
15 jumper wires
2 1k resistors
1 330 resistor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//LEDS | |
const int bluePin = 13; | |
const int whitePin = 9; | |
const int redPin = 8; | |
//Buttons | |
const int on1Button= 11; | |
const int off1Button = 10; | |
const int on2Button = 7; | |
const int off2Button = 6; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// initialize digital pin LED_BUILTIN as an output. | |
pinMode(bluePin, OUTPUT); | |
pinMode(on1Button, INPUT_PULLUP); | |
pinMode(off1Button, INPUT_PULLUP); | |
pinMode(on2Button, INPUT_PULLUP); | |
pinMode(off2Button, INPUT_PULLUP); | |
Serial.begin (9600); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
// wait for a second | |
if (digitalRead(on1Button) == LOW) | |
{ | |
digitalWrite(bluePin, HIGH); | |
} | |
if (digitalRead(off1Button) == LOW) | |
{ | |
digitalWrite(bluePin, LOW); | |
} | |
if (digitalRead(on2Button) == LOW) | |
{ | |
digitalWrite(whitePin, HIGH); | |
} | |
if (digitalRead(off2Button) == LOW) | |
{ | |
digitalWrite(whitePin, LOW); | |
} | |
if (digitalRead(on2Button) == HIGH) | |
{ | |
digitalWrite(redPin, LOW); | |
} | |
if (digitalRead(off2Button) == LOW) | |
{ | |
digitalWrite(redPin,HIGH); | |
} | |
} |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.