Difference between revisions of "Lesson 3--Microduino "Button Controlled LED""

From Microduino Wiki
Jump to: navigation, search
(Created page with "{| style="width: 800px;" |- | ==目的== 前两个实验介绍了如何用软件直接控制led灯的亮灭。如果我们增加一个按键,通过按键来控制led的亮...")
 
Line 2: Line 2:
 
|-
 
|-
 
|
 
|
==目的==
+
==Objective==
前两个实验介绍了如何用软件直接控制led灯的亮灭。如果我们增加一个按键,通过按键来控制led的亮灭,就可以实现硬件与软件的结合。前面实际上是Microduino的I/O口作为输出去控制led,增加按键如何去监测按键输入的信号呢?今天以按键为例讲讲Microduino如何作为输入使用。
+
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发光二极管  一个 
+
**LED Light-emitting diodes    one
**220欧姆电阻  一个
+
**220ohm resistor           one
**按键          一个  
+
**Button                      one  
**USB数据连接线  一根
+
**USB Data cable              one
  
==按键==
+
==Button==
*按键原理
+
*Button principle
 
[[File:第三课-按键原理.jpg|600px|center|thumb]]
 
[[File:第三课-按键原理.jpg|600px|center|thumb]]
若用此封装按键制作PCB时,若不好布线,可利用它有两对引脚始终导通作为导线布线。
+
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]]
采用外部下拉,没按下时为低“0”,按下为高。
+
Using external pulldown resistor, when didn't press, it is low "0", press for high.
 +
 
 +
 
 +
==Program==
 +
*LED display button value
  
==程序==
 
*led显示按键值
 
 
<source lang="cpp">
 
<source lang="cpp">
const int buttonPin = 2;    // 定义按键输入引脚
+
const int buttonPin = 2;    // Define button input pin
const int ledPin =  13;    //定义led引脚
+
const int ledPin =  13;    //Define LED pin
int buttonState = 0;        //初始化按键值
+
int buttonState = 0;        //Initialize the button value
 
void setup() {
 
void setup() {
   pinMode(ledPin, OUTPUT);    //设置led引脚为输出状态 
+
   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); //如果按键信号是高,则led点亮(led接法阳极接控制脚,阴极接GND)
+
     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电压翻转
+
 
 +
*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;        //定义led为布尔型(true或false)
+
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;                  //LED state flip
 
   }  
 
   }  
 
   digitalWrite(ledPin, led);   
 
   digitalWrite(ledPin, led);   
 
}
 
}
此实验需要注意按键的防抖,可以调整延时时间,或者在按键信号变化两端加104电容,电压信号不会突变,稳定点。
 
 
</source>
 
</source>
按键防抖详细参考:http://www.geek-workshop.com/thread-74-1-1.html
+
This experiment should be noted the button anti-shake, you can adjust the delay time,
===digitalRead()用法===
+
or at both ends of the input signal plus a 104 capacitor, then voltage signal does not mutant.
读一个引脚的值,返回值为HIGH或低。
+
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==
*程序1:led显示按键按键引脚值,按键值高则点亮,低则熄灭。
+
*Program 1:LED display input button value. High value will light LED, or Put out the LED.
*程序2:每按一次按键,led电压翻转一次,注意防抖。
+
*Program 2:Each time you press the button, led voltage flipping once, pay attention to button anti-shake.
==视频==
+
==Video==
 
|}
 
|}

Revision as of 00:52, 10 February 2014

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-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

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

Experimental schematic

Using external pulldown resistor, when didn't press, it is low "0", press for high.


Program

  • LED display button value
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
  }
}
  • LED voltage flip
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()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