4. Arduino Learning notebook -- Make the first circuit: Blink single LED (six leds)

4. Arduino Learning notebook -- Make the first circuit: Blink single LED
             Juvtmall ( a company supply PCB Prototyping, PCBA service and sell kinds of components, modules and so on) ( Catduino has the same function of Arduino)

In the previous experiment, we learned how to make an LED to blink, and the following experiment would control six LEDs.





[code]
1. //set IO pin as controlling the LED
2.int Led1 = 1;
3.int Led2 = 2;
4.int Led3 = 3;
5.int Led4 = 4;
6.int Led5 = 5;
7.int Led6 = 6;
8. //Led display style 1 subprogram
9.void style_1(void)
10.{
11.  unsigned char j;
12.  for(j=1;j<=6;j++)
13.  {
14.    digitalWrite(j, HIGH);
15.    delay(200);
16.  }
17.  for(j=6;j>=1;j--)
18.  {
19.    digitalWrite(j, LOW);
20.    delay(200);
21.  }
22.}
23.void setup()
24.{
25.  unsigned char i;
26.  for(i=1;i<=6;i++)//Set 1 ~ 6 digital pins as output mode in turn
27.    pinMode(i, OUTPUT);  //Set the number i pin as output mode
28.{
29.void loop()
30.{
31.  style_1();//style 1  
32.}
[code]

We found that the first program was light the 6 leds one by one, then turned them off one by one.

The second program
[code]
 //set IO pin as controlling the LED
int Led1 = 1;
int Led2 = 2;
int Led3 = 3;
int Led4 = 4;
int Led5 = 5;
int Led6 = 6;
 //Led display style 1 subprogram
void style_1(void)
{
  unsigned char j;
for(j=1;j<=6;j++)
   digitalWrite(j, HIGH);
    delay(200);
  for(j=6;j>=1;j--)
  {
    digitalWrite(j, LOW);
    delay(200);
  }
}
void setup()
{
  unsigned char i;
  for(i=1;i<=6;i++)   //Set 1 ~ 6 digital pins as output mode in turn
    pinMode(i, OUTPUT);  //Set the number i pin as output mode
{
void loop()
{
  style_1();//style 1  
}
[code]

The second program is six lights lightened at the same time, and then the 6 leds turned off one by one.

The following code represents the “for recycle sentence, light the j and then delay it for 200ms before repeating it. The result is that the six lights are lightened every 200ms one by one. 

1.for(j=1;j<=6;j++)
2.    {
3.      digitalWrite(j,HIGH);
4.      delay(200);
5.    }
6. [/ code [size = 4 The below code is a non-standard writing. To express the command forrequirements must have {} cycle, if there is no mark {}, it will be automatic to add {} when compiling.  If the code is huge, the problem is very hard to find . [/ size [DISCUZ_CODE_0 [size = 4 six lights lit up one by one, and then delay 200ms to enter next sentence. Because six lights lit up one by one quickly, it seems to be lightened together.
7.[bvoid [/ b (no type) is a type of data type in arduino, which is usually used to represent an event. If the process of control is relatively simple, the void don’t need to defined and directly use [/ size [pre lang = "arduino" line = "1" "void setup" ()
8.  {
9.    // ...
10. }
11.  
12.  void loop()
13.  {
14.    // ...
15. }

Represent the beginning and the cycle of events.

If the process of control is complex, it is common to set the multiple subevents, decompose the complex process, and each subevent is defined as a void data.
Upload the following code to see what does the led work.
ARDUINO code

// set IO pin as controlling the LED
 int Led1 = 1;
 int Led2 = 2;
 int Led3 = 3;
 int Led4 = 4;
 int Led5 = 5;
 int Led6 = 6;

//led display style 1 subprogram
 void style_1(void)
  {
   unsigned char j;

  for(j=1;j<=6;j++)//lighten the led connected to the 1~6 pins one by one every 200ms
{
 digitalWrite(j,HIGH);//lighten the led connected to the j pin
delay(200);//delay200ms
}
  for(j=6;j>=1;j--)//turn off the led connected to the 6~1pins one by one every200ms
{
digitalWrite(j,LOW);//turn off the led connected to the j pin
 delay(200);//delay 200ms
}
}
 void flash(void)
  {
   unsigned char j,k;
 //light flash subprogram
 for(k=0;k<=1;k++)//flicker twice
{
for(j=1;j<=6;j++)//lighten the led connected to the 1~6 pins
 digitalWrite(j,HIGH);//lighten the led connected to the j pin
31.delay(200);//delay 200ms
32.for(j=1;j<=6;j++)//turn off the led connected to the 1~6 pins
33.digitalWrite(j,LOW);//turn off the led connected to the j pin
34.delay(200);//delay 200ms
}
}
//led display style2 subprogram
  void style_2(void)
  {
   unsigned char j,k;

  k=1;//set k initial value as 1
 for(j=3;j>=1;j--)
   {

 digitalWrite(j,HIGH);//lighten the light
 digitalWrite(j+k,HIGH);//lighten the light 
 delay(400);//delay 400ms
 k +=2;//k plus 2
}
k=5;//set the k as 5
 for(j=1;j<=3;j++)
{
 digitalWrite(j,LOW);//turn off the light
digitalWrite(j+k,LOW);//turn off the light
 delay(400);//delay 400ms
 k -=2;//k reduce 2
}
}
//led display style 3subprogram
 void style_3(void)
 {

   unsigned char j,k;//led display style3 subprogram
  k=5;//set k as 5
 for(j=1;j<=3;j++)
    {

  digitalWrite(j,HIGH);//lighten the light
   digitalWrite(j+k,HIGH);//lighten the light
  delay(400);//delay 400ms
  digitalWrite(j,LOW);//turn off the light
  digitalWrite(j+k,LOW);//turn off the light
  k -=2;//k reduce 2
}
  k=3;//set k as 3
  digitalWrite(j,HIGH);//lighten the light
  digitalWrite(j+k,HIGH);//lighten the light
   delay(400);//delay 400ms
   digitalWrite(j,LOW);//turn off the light
   digitalWrite(j+k,LOW);//turn off the light
k +=2;//k plus 2
   }
  }
  void setup()
  {
    unsigned char i;

  for(i=1;i<=6;i++)//set 1~6 pin as output in turn
   pinMode(i,OUTPUT);//set the ith pin as output
  }
  void loop()
  {

  style_1();//style 1
  flash();//flash
   style_2();//style 2
   flash();//flash
   style_3();//style 3
  flash();//flash
}

The code above is divided into four subevents, style 1, style 2, style 3, and flicker.
Flicker: 1-6 light, delay 200ms, 1-6 light out, delay 200ms. 

Pattern 1:  The1-6 light is lit up one by one, and then the 6- 1 lit out one by one.
Pattern 2:  3, 4 light first, then 2, 5 and then again, finally, 1, 6, and 2. Then the lights went out on no. 1, no. 2, 5, and then went out.

Pattern 3:  light the 3and 4, then 2 and 5 light, last 1 and 6; Next light out 1 and 6, then 2 and 5, last 3 and 5.

Through the combination of these four events, you can make a variety of effects.


评论

此博客中的热门博文

21. Arduino learning notebook-- Experiment of controlling steering engine

20.Arduino learning notebook--Use Arduino Duemilanove to download the bootloader to other chips

19.Arduino learning notebook--Atmega8 makes the minimum arduino system