Turn on an LED by holding down a push button.
Equipment:
•1 x Arduino Uno
•1 x Breadboard
•7 x Wire
•2 x Pushbutton
•1 x LED
•1 x 560 Ohm Resistor
•2 x 10k Ohm Resistor
Reference:
http://www.oomlout.com/a/products/ardx/circ-07
Program Details:
Pushbuttons are initialized the same way LEDs are.
Time to Complete:
25 minutes
Results:
The first pushbutton lit up the LED when held down. However, the second pushbutton did not do anything.
Photos of Project:
Tips:
Pushbuttons can be misplaced on the breadboard. If nothing is happening, try rotating your pushbuttons 90 degrees.
Further Work:
Code can be added to make the second pushbutton have a function.
Program Modifications:
The program is exactly the one from http://www.oomlout.com/a/products/ardx/circ-07
Program:
const int buttonPin = 2; // sets the pin the button is connected to as a constant
const int ledPin = 13; // sets the pin the LED is connected to as a constant
int buttonState = 0; // int for reading state of button
const int ledPin = 13; // sets the pin the LED is connected to as a constant
int buttonState = 0; // int for reading state of button
void setup()
{
pinMode(ledPin, OUTPUT); //initializes pin 13 as an output
pinMode(buttonPin, INPUT); //initializes pin 2 as an input
}
pinMode(ledPin, OUTPUT); //initializes pin 13 as an output
pinMode(buttonPin, INPUT); //initializes pin 2 as an input
}
void loop()
{
//reads the state of the button and assigns it to buttonState
buttonState = digitalRead(buttonPin);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) { //checks if the button is pushed then the LED will go on
digitalWrite(ledPin, HIGH);
}
else { //if not the LED will go off
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
No comments:
Post a Comment