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

4. Arduino Learning notebook -- Make the first circuit: Blink single LED (VOL.1)
             Juvtmall ( a company supply PCB PrototypingPCBA 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]
 //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]

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 non-standard writing. To express the command for” requirements 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.

评论

此博客中的热门博文

3.Basic knowledge of Raspberry Pi --How to set the Wi-Fi?