“Lesson 3--Microduino "Button Controlled LED"”的版本间的差异
Jasonsheng(讨论 | 贡献) (Created page with "{| style="width: 800px;" |- | ==目的== 前两个实验介绍了如何用软件直接控制led灯的亮灭。如果我们增加一个按键,通过按键来控制led的亮...") |
Jasonsheng(讨论 | 贡献) |
||
第2行: | 第2行: | ||
|- | |- | ||
| | | | ||
− | == | + | ==Objective== |
− | + | The first two experiments showed you how to use software to control the LED directly. If we add a button, | |
− | == | + | to control the LED light, then realize the combination of hardware and software. |
+ | Actually that two experiment use Microduino I/O port as the output to control the LED, if want to use the button, | ||
+ | how to monitor the input signal of the button? | ||
+ | Today we will take button as an example to show how to use the Microduino as the input. | ||
+ | |||
+ | |||
+ | ==Equipment== | ||
*'''[[Microduino-Core]]''' | *'''[[Microduino-Core]]''' | ||
*'''[[Microduino-FT232R]]''' | *'''[[Microduino-FT232R]]''' | ||
− | * | + | *Other hardware equipment |
− | ** | + | **Breadboard Jumper one box |
− | ** | + | **Breadboard one piece |
− | ** | + | **LED Light-emitting diodes one |
− | ** | + | **220ohm resistor one |
− | ** | + | **Button one |
− | ** | + | **USB Data cable one |
− | == | + | ==Button== |
− | * | + | *Button principle |
[[File:第三课-按键原理.jpg|600px|center|thumb]] | [[File:第三课-按键原理.jpg|600px|center|thumb]] | ||
− | + | If use this wrap button making PCB, if bad wiring, it has two pairs of pins available always conduction as a conductor wiring. | |
− | * | + | |
+ | *Button connection | ||
[[File:第三课-按键接法.jpg|600px|center|thumb]] | [[File:第三课-按键接法.jpg|600px|center|thumb]] | ||
− | == | + | ==Experimental schematic== |
[[File:第三课-原理图.jpg|600px|center|thumb]] | [[File:第三课-原理图.jpg|600px|center|thumb]] | ||
− | + | Using external pulldown resistor, when didn't press, it is low "0", press for high. | |
+ | |||
+ | |||
+ | ==Program== | ||
+ | *LED display button value | ||
− | |||
− | |||
<source lang="cpp"> | <source lang="cpp"> | ||
− | const int buttonPin = 2; // | + | const int buttonPin = 2; // Define button input pin |
− | const int ledPin = 13; // | + | const int ledPin = 13; //Define LED pin |
− | int buttonState = 0; // | + | int buttonState = 0; //Initialize the button value |
void setup() { | void setup() { | ||
− | pinMode(ledPin, OUTPUT); // | + | pinMode(ledPin, OUTPUT); //Set the LED pin as output |
− | pinMode(buttonPin, INPUT); // | + | pinMode(buttonPin, INPUT); //Set button pin as intpu |
} | } | ||
void loop(){ | void loop(){ | ||
− | buttonState = digitalRead(buttonPin);// | + | buttonState = digitalRead(buttonPin);//Read the value from the buttonPin |
if (buttonState == HIGH) { | if (buttonState == HIGH) { | ||
− | digitalWrite(ledPin, HIGH); // | + | digitalWrite(ledPin, HIGH); //If the button input signal is high, the LED will light (LED connect method is that anode connects control pin, cathode connects GND) |
} | } | ||
else { | else { | ||
− | digitalWrite(ledPin, LOW); // | + | digitalWrite(ledPin, LOW); //LED goes out |
} | } | ||
} | } | ||
</source> | </source> | ||
− | * | + | |
+ | *LED voltage flip | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | const int buttonPin = 2; // | + | const int buttonPin = 2; // Define button input pin |
const int ledPin = 13; | const int ledPin = 13; | ||
int buttonState = 0; | int buttonState = 0; | ||
− | boolean led; // | + | boolean led; //Define LED as boolean(true or false) |
void setup() { | void setup() { | ||
pinMode(ledPin, OUTPUT); | pinMode(ledPin, OUTPUT); | ||
− | // pinMode(buttonPin, INPUT); // | + | // pinMode(buttonPin, INPUT); //Set the button pin as input |
− | pinMode(buttonPin, INPUT_PULLUP);// | + | pinMode(buttonPin, INPUT_PULLUP);//Set button pin as interanl pull-up input |
} | } | ||
void loop(){ | void loop(){ | ||
buttonState = digitalRead(buttonPin); | buttonState = digitalRead(buttonPin); | ||
− | if (buttonState ==HIGH) | + | if (buttonState ==HIGH) //Use the external pull-down, because the initial state is low, only the press is high, the press was flipped, or not it will blink. |
− | // | ||
{ | { | ||
− | delay(200); // | + | delay(200); //Short time delay, stabilization |
− | // delay(1000); // | + | // delay(1000); //Long time press |
− | // if (buttonState == LOW) // | + | // if (buttonState == LOW) //Check still is low |
− | led=!led; // | + | led=!led; //LED state flip |
} | } | ||
digitalWrite(ledPin, led); | digitalWrite(ledPin, led); | ||
} | } | ||
− | |||
</source> | </source> | ||
− | + | This experiment should be noted the button anti-shake, you can adjust the delay time, | |
− | === | + | or at both ends of the input signal plus a 104 capacitor, then voltage signal does not mutant. |
− | + | The detailed button anti-shake information, please refer to:http://www.geek-workshop.com/thread-74-1-1.html | |
+ | ===digitalRead()usage=== | ||
+ | Read a Pin's value and return HIGH or LOW. | ||
− | == | + | ==Result== |
− | * | + | *Program 1:LED display input button value. High value will light LED, or Put out the LED. |
− | * | + | *Program 2:Each time you press the button, led voltage flipping once, pay attention to button anti-shake. |
− | == | + | ==Video== |
|} | |} |
2014年2月10日 (一) 00:52的版本
目录ObjectiveThe first two experiments showed you how to use software to control the LED directly. If we add a button, to control the LED light, then realize the combination of hardware and software. Actually that two experiment use Microduino I/O port as the output to control the LED, if want to use the button, how to monitor the input signal of the button? Today we will take button as an example to show how to use the Microduino as the input.
Equipment
Button
If use this wrap button making PCB, if bad wiring, it has two pairs of pins available always conduction as a conductor wiring.
Experimental schematicUsing external pulldown resistor, when didn't press, it is low "0", press for high.
Program
const int buttonPin = 2; // Define button input pin
const int ledPin = 13; //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 intpu
}
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 (LED connect method is that anode connects control pin, cathode connects GND)
}
else {
digitalWrite(ledPin, LOW); //LED goes out
}
}
const int buttonPin = 2; // Define button input pin
const int ledPin = 13;
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 interanl pull-up input
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState ==HIGH) //Use the external pull-down, because the initial state is low, only the press is high, the press was flipped, or not it will blink.
{
delay(200); //Short time delay, stabilization
// delay(1000); //Long time press
// if (buttonState == LOW) //Check still is low
led=!led; //LED state flip
}
digitalWrite(ledPin, led);
}
This experiment should be noted the button anti-shake, you can adjust the delay time, or at both ends of the input signal plus a 104 capacitor, then voltage signal does not mutant. The detailed button anti-shake information, please refer to:http://www.geek-workshop.com/thread-74-1-1.html digitalRead()usageRead a Pin's value and return HIGH or LOW. Result
Video |