“Lesson 3--Microduino "Button Controlled LED"”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(Created page with "{| style="width: 800px;" |- | ==目的== 前两个实验介绍了如何用软件直接控制led灯的亮灭。如果我们增加一个按键,通过按键来控制led的亮...")
 
 
(未显示3个用户的9个中间版本)
第1行: 第1行:
 +
{{Language|第三课--按钮控制的LED开关}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
 
|
 
|
==目的==
+
==Objective==
前两个实验介绍了如何用软件直接控制led灯的亮灭。如果我们增加一个按键,通过按键来控制led的亮灭,就可以实现硬件与软件的结合。前面实际上是Microduino的I/O口作为输出去控制led,增加按键如何去监测按键输入的信号呢?今天以按键为例讲讲Microduino如何作为输入使用。
+
The 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 we want to use a button,
 +
how would we monitor when it is pressed?
 +
In this lesson, we will use a button as an example to show how to use Microduino as an input.
 +
 
 +
==Equipment==
 
*'''[[Microduino-Core]]'''
 
*'''[[Microduino-Core]]'''
 
*'''[[Microduino-FT232R]]'''
 
*'''[[Microduino-FT232R]]'''
*其他硬件设备
+
*Other hardware equipment
**面包板跳线  一盒 
+
**1x Box of breadboard jumper wires           
**面包板  一块 
+
**1x Breadboard               
**LED发光二极管  一个 
+
**1x LED (Light-Emitting Diode)
**220欧姆电阻  一个
+
**1x 220ohm resistor          
**按键          一个 
+
**1x Button                       
**USB数据连接线  一根
+
**1x USB Data cable           
  
==按键==
+
==Button==
*按键原理
+
*Button principle
[[File:第三课-按键原理.jpg|600px|center|thumb]]
+
[[File:button.jpg|600px|center|thumb]]
若用此封装按键制作PCB时,若不好布线,可利用它有两对引脚始终导通作为导线布线。
 
*按键接法
 
[[File:第三课-按键接法.jpg|600px|center|thumb]]
 
  
==原理图==
+
*Button connection
[[File:第三课-原理图.jpg|600px|center|thumb]]
+
[[File:button connection.jpg|600px|center|thumb]]
采用外部下拉,没按下时为低“0”,按下为高。
+
 
 +
==Experiment Schematic==
 +
[[File:button schematic.jpg|600px|center|thumb]]
 +
Using the external pulldown method, when the button is unpressed, the input to D2 is "LOW". When pressed, it is "HIGH".
 +
 
 +
==Program==
 +
*LED is on while button is pressed down
  
==程序==
 
*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 =  11;    //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 input    
 
}
 
}
 
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 up
 
   }  
 
   }  
 
   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 =  11;     
  
 
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);//Set button pin as input    
 
}
 
}
 
void loop(){
 
void loop(){
 
   buttonState = digitalRead(buttonPin);
 
   buttonState = digitalRead(buttonPin);
   if (buttonState ==HIGH)  
+
   if (buttonState ==HIGH)
//外部下拉,因为这样起始状态为低,只有按下才是高,按下才翻转,否则不按时则会闪烁
 
 
   {   
 
   {   
     delay(200);                     //短暂延时,防抖
+
     delay(200);                   //Short time delay for 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
 
===digitalRead()用法===
 
读一个引脚的值,返回值为HIGH或低。
 
  
==结果==
+
===digitalRead() usage===
*程序1:led显示按键按键引脚值,按键值高则点亮,低则熄灭。
+
Read a pin's value and return HIGH or LOW.
*程序2:每按一次按键,led电压翻转一次,注意防抖。
+
 
==视频==
+
==Result==
 +
*Program 1:LED is on while button is pressed down.
 +
*Program 2:Each time you press the button, the LED will turn on/off.
 +
==Video==
 
|}
 
|}

2015年7月14日 (二) 19:01的最新版本

Language English

Objective

The 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 we want to use a button, how would we monitor when it is pressed? In this lesson, we will use a button as an example to show how to use Microduino as an input.

Equipment

  • Microduino-Core
  • Microduino-FT232R
  • Other hardware equipment
    • 1x Box of breadboard jumper wires
    • 1x Breadboard
    • 1x LED (Light-Emitting Diode)
    • 1x 220ohm resistor
    • 1x Button
    • 1x USB Data cable

Button

  • Button principle
Button.jpg
  • Button connection
Button connection.jpg

Experiment Schematic

Button schematic.jpg

Using the external pulldown method, when the button is unpressed, the input to D2 is "LOW". When pressed, it is "HIGH".

Program

  • LED is on while button is pressed down
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
  }
}
  • LED voltage flip
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);//Set button pin as 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() usage

Read a pin's value and return HIGH or LOW.

Result

  • Program 1:LED is on while button is pressed down.
  • Program 2:Each time you press the button, the LED will turn on/off.

Video