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

From Microduino Wiki
Jump to: navigation, search
Line 4: Line 4:
 
|
 
|
 
==Objective==
 
==Objective==
The first two experiments showed you how to use software to control the LED directly. If we add a button,
+
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 realize the combination of hardware and software.
+
to control the LED light, then we can combine the use of both 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,
+
Previously, we used Microduino I/O port as the output to control the LED. So if want to use a button,
how to monitor the input signal of the button?
+
how would we 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.
+
In this lesson, we will use a button as an example to show how to use Microduino as the input.
  
  
Line 15: Line 15:
 
*'''[[Microduino-FT232R]]'''
 
*'''[[Microduino-FT232R]]'''
 
*Other hardware equipment
 
*Other hardware equipment
**Breadboard Jumper           one box 
+
**1x Box of breadboard jumper wires            
**Breadboard                one piece 
+
**1x Breadboard                 
**LED Light-emitting diodes    one
+
**1x LED (Light-Emitting Diode)
**220ohm resistor           one
+
**1x 220ohm resistor          
**Button                       one 
+
**1x Button                        
**USB Data cable               one
+
**1x USB Data cable            
  
 
==Button==
 
==Button==
 
*Button principle
 
*Button principle
 
[[File:button.jpg|600px|center|thumb]]
 
[[File:button.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
 
*Button connection
Line 32: Line 31:
 
==Experimental schematic==
 
==Experimental schematic==
 
[[File:button schematic.jpg|600px|center|thumb]]
 
[[File:button schematic.jpg|600px|center|thumb]]
Using external pulldown resistor, when didn't press, it is low "0", press for high.
+
Using external pulldown method, when unpressed, it is "LOW". When pressed, it is high.
  
  
Line 44: Line 43:
 
void setup() {
 
void setup() {
 
   pinMode(ledPin, OUTPUT);    //Set the LED pin as output     
 
   pinMode(ledPin, OUTPUT);    //Set the LED pin as output     
   pinMode(buttonPin, INPUT); //Set button pin as intpu    
+
   pinMode(buttonPin, INPUT); //Set button pin as input    
 
}
 
}
 
void loop(){
 
void loop(){
 
   buttonState = digitalRead(buttonPin);//Read the value from the buttonPin
 
   buttonState = digitalRead(buttonPin);//Read the value from the buttonPin
 
   if (buttonState == HIGH) {     
 
   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)
+
     digitalWrite(ledPin, HIGH); //If the button input signal is high, the LED will light up
 
   }  
 
   }  
 
   else {
 
   else {
Line 67: Line 66:
 
   pinMode(ledPin, OUTPUT);       
 
   pinMode(ledPin, OUTPUT);       
 
   // pinMode(buttonPin, INPUT); //Set the button pin as input     
 
   // pinMode(buttonPin, INPUT); //Set the button pin as input     
   pinMode(buttonPin, INPUT_PULLUP);//Set button pin as interanl pull-up input     
+
   pinMode(buttonPin, INPUT_PULLUP);//Set button pin as internal pull-up input     
 
}
 
}
 
void loop(){
 
void loop(){
 
   buttonState = digitalRead(buttonPin);
 
   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.
+
   if (buttonState ==HIGH)
 
   {   
 
   {   
     delay(200);                     //Short time delay, stabilization
+
     delay(200);                   //Short time delay for stabilization
 
     // delay(1000);              //Long time press
 
     // delay(1000);              //Long time press
     // if (buttonState == LOW) //Check still is low
+
     // if (buttonState == LOW)   //Check still is low
     led=!led;                   //LED state flip
+
     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.
+
===digitalRead()usage===
The detailed button anti-shake information, please refer to:http://www.geek-workshop.com/thread-74-1-1.html
+
Read a pin's value and return HIGH or LOW.
===digitalRead()usage===
 
Read a Pin's value and return HIGH or LOW.
 
  
 
==Result==
 
==Result==
*Program 1:LED display input button value. High value will light LED, or Put out the LED.
+
*Program 1:LED display input button value. High value will light the LED.
*Program 2:Each time you press the button, led voltage flipping once, pay attention to button anti-shake.
+
*Program 2:Each time you press the button, LED voltage will flip.
 
==Video==
 
==Video==
 
|}
 
|}

Revision as of 11:46, 12 July 2015

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

  • 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

Experimental schematic

Button schematic.jpg

Using external pulldown method, when unpressed, it is "LOW". When pressed, it is high.


Program

  • LED display button value
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_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()usage

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

Result

  • Program 1:LED display input button value. High value will light the LED.
  • Program 2:Each time you press the button, LED voltage will flip.

Video