Difference between revisions of "Lesson 3--Microduino "Button Controlled LED""
From Microduino Wiki
(→Experimental schematic) |
|||
Line 29: | Line 29: | ||
[[File:button connection.jpg|600px|center|thumb]] | [[File:button connection.jpg|600px|center|thumb]] | ||
− | ==Experimental | + | ==Experimental Schematic== |
[[File:button schematic.jpg|600px|center|thumb]] | [[File:button schematic.jpg|600px|center|thumb]] | ||
Using external pulldown method, when unpressed, it is "LOW". When pressed, it is high. | Using external pulldown method, when unpressed, it is "LOW". When pressed, it is high. | ||
− | |||
==Program== | ==Program== |
Revision as of 21:54, 12 July 2015
Language: | English • 中文 |
---|
ContentsObjectiveThe first two lessons showed you how to use software to control the LED directly. If we add a button to control the LED light, then we can combine the use of both hardware and software. Previously, we used Microduino I/O port as the output to control the LED. So if want to use a button, how would we monitor the input signal of the button? In this lesson, we will use a button as an example to show how to use Microduino as the input.
Equipment
Button
Experimental SchematicUsing external pulldown method, when unpressed, it is "LOW". When pressed, it is high. Program
const int buttonPin = 2; // Define button input pin
const int ledPin = 11; //Define LED pin
int buttonState = 0; //Initialize the button value
void setup() {
pinMode(ledPin, OUTPUT); //Set the LED pin as output
pinMode(buttonPin, INPUT); //Set button pin as input
}
void loop(){
buttonState = digitalRead(buttonPin);//Read the value from the buttonPin
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); //If the button input signal is high, the LED will light up
}
else {
digitalWrite(ledPin, LOW); //LED goes out
}
}
const int buttonPin = 2; // Define button input pin
const int ledPin = 11;
int buttonState = 0;
boolean led; //Define LED as boolean(true or false)
void setup() {
pinMode(ledPin, OUTPUT);
// pinMode(buttonPin, INPUT); //Set the button pin as input
pinMode(buttonPin, INPUT_PULLUP);//Set button pin as internal pull-up input
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState ==HIGH)
{
delay(200); //Short time delay for stabilization
// delay(1000); //Long time press
// if (buttonState == LOW) //Check still is low
led=!led; //LED state flip
}
digitalWrite(ledPin, led);
} digitalRead()usageRead a pin's value and return HIGH or LOW. Result
Video |