Difference between revisions of "Lesson 2--Microduino "Multiple LEDs""

From Microduino Wiki
Jump to: navigation, search
(Created page with "{| style="width: 800px;" |- | ==Objective== Have learned how to control a LED, now we can go on learn how to control Multiple led flashing turn. ==Equipment== *'''Microduin...")
 
(Result)
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{Language|第二课--多个led的闪烁}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
 
|
 
|
 
==Objective==
 
==Objective==
Have learned how to control a LED, now we can go on learn how to control Multiple led flashing turn.
+
After Lesson 1, you now know how to control a single LED. In this lesson, we will learn how to light up multiple LEDs in awesome patterns.
  
 
==Equipment==
 
==Equipment==
 
*'''[[Microduino-Core]]'''
 
*'''[[Microduino-Core]]'''
 
*'''[[Microduino-FT232R]]'''
 
*'''[[Microduino-FT232R]]'''
*其他硬件设备
+
*Other hardware equipment
**面包板跳线  一盒 
+
**1x Box of breadboard jumper wires
**面包板  一块 
+
**1x Breadboard
**LED发光二极管  八个 
+
**8x LEDs (Light-Emitting Diodes) 
**220欧姆电阻  八个 
+
**1x 220ohm resistor
**USB数据连接线  一根
+
**1x USB Data cable
  
==原理图==
+
[[File:lesson2All.jpg|600px|center|thumb]]
*led的阴极接Microduino的GND,阳极接Microduino数据控制口D3~D10,这样就是高电平点亮led。
 
*led的阴极接Microduino的数据控制口D3~D10,阳极接Microduino的VCC,这样就是低电平点亮led。
 
其实数码管共阴、共阳就是这个原理,以后会详细介绍驱动数码管。
 
  
[[File:第二课-原理图.jpg|600px|center|thumb]]
+
==Experiment Schematic==
 +
As you may recall from Lesson 1, there are two ways of connecting the LEDs.
 +
*Method One: Connect the negative ends (cathodes) of the LEDs to the GND port of Microduino and connect the positive ends (anodes) to digital I/O ports D3~D10. High level connection.
 +
*Method Two: Connect the cathodes to the digital I/O ports D3~D10 and the anodes to VCC. Low level connection.  
  
==程序==
+
[[File:schematic2.jpg|600px|center|thumb]]
*采用数据口直接输出
+
 
 +
==Program==
 +
*Using I/O port output directly
 
<source lang="cpp">
 
<source lang="cpp">
 
void setup() {                 
 
void setup() {                 
//定义microduino数字D3~D10脚为输出    
+
//Define microduino digital I/O port D3~D10 as output    
 
   for(int i=3;i<11;i++)
 
   for(int i=3;i<11;i++)
 
   pinMode(i, OUTPUT);     
 
   pinMode(i, OUTPUT);     
Line 33: Line 36:
 
   for(int i=3;i<11;i++)
 
   for(int i=3;i<11;i++)
 
   {
 
   {
   digitalWrite(i, HIGH); // 数据口i(D3~D10)输出高电平
+
   digitalWrite(i, HIGH); // Digital I/O port i(D3~D10) outputs high
   delay(50);            //延时50ms  
+
   delay(50);            //delay 50ms  
   digitalWrite(i, LOW); //// 数据口i(D3~D10)输出低电平
+
   digitalWrite(i, LOW); //// Digital I/O port i(D3~D10) outputs low
   delay(50);          //延时50ms    
+
   delay(50);          //delay 50ms    
 
   }
 
   }
 
}
 
}
 
</source>
 
</source>
*采用16进制数组,将数据移位输出送至每个I/O口,花样流水
+
 
 +
*Using a hexadecimal-based array, the output data is shifted to each I/O port, allowing the user to create countless patterns with ease.
 
<source lang="cpp">
 
<source lang="cpp">
 
/*===============================================
 
/*===============================================
ox(高-低:10,9,8,7)(高-低:6,5,4,3)
+
1 means LED is on. 0 means LED is off.
例如
+
Example:
 
0x81:10000001
 
0x81:10000001
 
10,9,8,7,6,5,4,3
 
10,9,8,7,6,5,4,3
↓ ↓ ↓↓ ↓↓
+
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
1 0 0 0 0 0 0 1
+
1 0 0 0 0 0 0 1
在共阴即所有led阴极都接在一起情况下,1代表亮,0代表灭,
+
The LEDs connected to ports 10 and 3 in this case would be on.
就可以通过数组形式直接控制亮度。
+
 
 
=================================================*/
 
=================================================*/
 
long  data[]=
 
long  data[]=
 
{
 
{
   0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,//单独一个从左至右亮
+
   0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,//From left to right light
   0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,//单独一个从从右至左亮
+
   0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,//From right to left light
   0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81,//两边往中间亮及中间往两边亮
+
   0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81,//Both sides to the middle and middle to both sides bright light
   0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,//从左到右依次点亮
+
   0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,//Lit from left to right
   0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,//从右到左依次熄灭
+
   0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,//From right to left extinguished
 
};
 
};
  
Line 65: Line 69:
 
   for(int x=3;x<11;x++)  
 
   for(int x=3;x<11;x++)  
 
   {
 
   {
     pinMode(x,OUTPUT);//设置输出引脚
+
     pinMode(x,OUTPUT);//Set the output pin
 
   }   
 
   }   
 
}
 
}
Line 72: Line 76:
 
void loop()  
 
void loop()  
 
{
 
{
   for(int x=0;x<40;x++)//分别读取不同花样灯
+
   for(int x=0;x<40;x++)//Reading different pattern's lights
 
   {
 
   {
 
     leddisplay(data[x]);
 
     leddisplay(data[x]);
     delay(200); //每个状态显示200ms
+
     delay(200); //Every state displays 200ms
 
   }
 
   }
   leddisplay(0x00);//循环完毕全灭
+
   leddisplay(0x00);//Cycle is completed, then all extinguished
 
   delay(200);
 
   delay(200);
 
}
 
}
  
void leddisplay(int num)    // 将花样字模对应到端口显示
+
void leddisplay(int num)    // Mapping the pattern matrix to the port display
 
{
 
{
  /*====================================================================
+
/*====================================================================
  先将16进制数向右移x位(num>>x),x代表了microduinoI/O口对应16进制的某位,
+
Firstly right shift the hexadecimal number x bits (num >> x),
  0是最低位,7是最高位。再将移的数据与0x01按位与,就可以得到16进制数某位的
+
x represents the microduino I/O port corresponding hexadecimal bit,
  数据(0或1),再将其值赋值给microduino的I/O就行。
+
0 is the lowest bit, bit 7 is the highest bit.
 +
Then, use the bitwise AND operation with the shifted data and 0x01 to get the corresponding binary digit at port x (0 or 1). 
 
  ====================================================================*/
 
  ====================================================================*/
 
   digitalWrite(3, ((num>>0)&0x01));
 
   digitalWrite(3, ((num>>0)&0x01));
Line 98: Line 103:
 
}
 
}
 
</source>
 
</source>
此实验类似51单片机流水灯写法,将16进制数组赋值到I/O,比如0x18表示2进制的00011000,对应值为“1”的灯亮,“0”的灭。能简单随意的写出各种花样。
+
Comparing the two programs, the first looks simple, has only one pattern, and is very limited.The second program is optimized. Using an array, the hexadecimal number's each bit is assigned to the specified I/O port. Waterfall LED designs can be created using the second program.
两个实验比较,第一个实验看起来简单,效果单一,并且输出口必须连续,局限性很大。第二个实验就优化了,利用数组,将16进制的每位得值都赋值给指定的I/O口,可以实现花样流水。
+
 
 +
==Result==
 +
*Program 1
 +
LED lights are turned on and off from left to right one at a time.
 +
*Program 2
 +
A total of five patterns:
 +
#LEDs turn on and off from left to right
 +
#LEDs turn on and off from right to left
 +
#LEDs turn on and off from both ends to the middle and middle to both ends
 +
#LEDs are all lit from left to right
 +
#LEDs are extinguished from right to left.
  
==结果==
+
[[File:lesson2Result1.jpg|600px|center|thumb]]
*程序1
+
[[File:lesson2Result2.jpg|600px|center|thumb]]
led从左到右依次循环点亮每一个led。
+
[[File:lesson2Result3.jpg|600px|center|thumb]]
*程序2
 
可以看到五种花样效果:单独一个从左至右亮;单独一个从从右至左亮;两边往中间亮及中间往两边亮;从左到右依次点亮;从右到左依次熄灭。
 
==视频==
 
  
 +
==Video==
 +
http://v.youku.com/v_show/id_XNzA5OTk3MjI4.html
 
|}
 
|}

Latest revision as of 18:03, 14 July 2015

Language: English  • 中文

Objective

After Lesson 1, you now know how to control a single LED. In this lesson, we will learn how to light up multiple LEDs in awesome patterns.

Equipment

  • Microduino-Core
  • Microduino-FT232R
  • Other hardware equipment
    • 1x Box of breadboard jumper wires
    • 1x Breadboard
    • 8x LEDs (Light-Emitting Diodes)
    • 1x 220ohm resistor
    • 1x USB Data cable
Lesson2All.jpg

Experiment Schematic

As you may recall from Lesson 1, there are two ways of connecting the LEDs.

  • Method One: Connect the negative ends (cathodes) of the LEDs to the GND port of Microduino and connect the positive ends (anodes) to digital I/O ports D3~D10. High level connection.
  • Method Two: Connect the cathodes to the digital I/O ports D3~D10 and the anodes to VCC. Low level connection.
Schematic2.jpg

Program

  • Using I/O port output directly
void setup() {                
//Define microduino digital I/O port D3~D10 as output   
  for(int i=3;i<11;i++)
  pinMode(i, OUTPUT);     
}
void loop() {
  for(int i=3;i<11;i++)
  {
  digitalWrite(i, HIGH); // Digital I/O port i(D3~D10) outputs high
  delay(50);            //delay 50ms  
  digitalWrite(i, LOW); //// Digital I/O port i(D3~D10) outputs low
  delay(50);           //delay 50ms    
  }
}
  • Using a hexadecimal-based array, the output data is shifted to each I/O port, allowing the user to create countless patterns with ease.
/*===============================================
1 means LED is on. 0 means LED is off.
Example:
0x81:10000001
10,9,8,7,6,5,4,3
 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
 1 0 0 0 0 0 0 1
The LEDs connected to ports 10 and 3 in this case would be on.

=================================================*/
long  data[]=
{
  0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,//From left to right light
  0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,//From right to left light
  0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81,//Both sides to the middle and middle to both sides bright light
  0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,//Lit from left to right
  0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,//From right to left extinguished
};

void setup() 
{                
  for(int x=3;x<11;x++) 
  {
    pinMode(x,OUTPUT);//Set the output pin
  }  
}


void loop() 
{
  for(int x=0;x<40;x++)//Reading different pattern's lights
  {
    leddisplay(data[x]);
    delay(200); //Every state displays 200ms
  }
  leddisplay(0x00);//Cycle is completed, then all extinguished
  delay(200);
}

void leddisplay(int num)    // Mapping the pattern matrix to the port display
{
/*====================================================================
Firstly right shift the hexadecimal number x bits (num >> x), 
x represents the microduino I/O port corresponding hexadecimal bit, 
0 is the lowest bit, bit 7 is the highest bit. 
Then, use the bitwise AND operation with the shifted data and 0x01 to get the corresponding binary digit at port x (0 or 1).  
 ====================================================================*/
  digitalWrite(3, ((num>>0)&0x01));
  digitalWrite(4, ((num>>1)&0x01));
  digitalWrite(5, ((num>>2)&0x01));
  digitalWrite(6, ((num>>3)&0x01));
  digitalWrite(7, ((num>>4)&0x01));
  digitalWrite(8, ((num>>5)&0x01));
  digitalWrite(9,((num>>6)&0x01));
  digitalWrite(10,((num>>7)&0x01));
}

Comparing the two programs, the first looks simple, has only one pattern, and is very limited.The second program is optimized. Using an array, the hexadecimal number's each bit is assigned to the specified I/O port. Waterfall LED designs can be created using the second program.

Result

  • Program 1

LED lights are turned on and off from left to right one at a time.

  • Program 2

A total of five patterns:

  1. LEDs turn on and off from left to right
  2. LEDs turn on and off from right to left
  3. LEDs turn on and off from both ends to the middle and middle to both ends
  4. LEDs are all lit from left to right
  5. LEDs are extinguished from right to left.
Lesson2Result1.jpg
Lesson2Result2.jpg
Lesson2Result3.jpg

Video

http://v.youku.com/v_show/id_XNzA5OTk3MjI4.html