3 LED running-int code by Arduino

This is the easy 3 LED running circuit or LED Chaser using Arduino. We will learn code “int” to define the variable.
-variable Is representatives of the various

Let’s write code Sample one LED flasher. Which we’ve already made easiest it

——code Sample one LED flasher use int on Arduino

  1. int pin1 = 13;   // to define the variable
  2.  
  3. void setup() 
  4. {
  5.  pinMode (pin1, OUTPUT);  // pin1 = 13  
  6. }
  7. void loop()
  8. {
  9. digitalWrite(pin1, HIGH);
  10. delay(1000);
  11. digitalWrite(pin1, LOW);
  12. delay (1000);
  13. }

++++++++++++

From the code above. We use “int” to define the variable: pin1 = 13
The number 13 is output of arduino use for LED

In void setup() we set pinMode(pin1, OUTPUT) Which pin1 is 13.

Summarize : To see that we can use the command “int” To determine the variables To represent the various Easier to understand.

Lets builds the 3 LED Running

Next, we try to make the 3 LED Running using Arduino as Figure 1

3-LED-running-int-code-by-arduino

and Figure 2 is circuit connection

3-LED-running-circuit-connetion

Then, next, let’s write the code 3 LED Running without int on Arduino
–—code—-

  1.   void setup() {
  2.   pinMode(13, OUTPUT);
  3.   pinMode(12, OUTPUT);
  4.   pinMode(11, OUTPUT);
  5.  
  6. }
  7.  
  8. void loop() {
  9.   digitalWrite(13, HIGH);
  10.   digitalWrite(12, LOW);
  11.   digitalWrite(11, LOW);
  12.   delay(1000);
  13.   digitalWrite(13, LOW);
  14.   digitalWrite(12, HIGH);
  15.   digitalWrite(11, LOW);
  16.   delay(1000);
  17.   digitalWrite(13, LOW);
  18.   digitalWrite(12, LOW);
  19.   digitalWrite(11, HIGH);
  20.   delay(1000);
  21. }

We will see that, although the short code. If you want to change pin output 13,12,11 into others pins (for example 10,9,8). It must to change several points so inconvenient. Or code: delay (1000) To change the time delay is 100 (0.1S) or p2000 (2S) will be edited together three positions. If our code long, it is very difficult to edit.

The 3 LED Running with int on Arduino

From the these problems, let’s use an int as well.
int red = 13; => for the red LED pin have variable is “red”.
int green = 12; => for the green LED pin have variable is “green”
int yellow = 11; => for the yellow LED pin have variable is “yellow”
int delay1 = 1000; => for times value have variable is “delay1”

—-code—-

  1. int red = 13;
  2. int green = 12;
  3. int yellow = 11;
  4. int delay1 = 1000;
  5.  
  6. void setup() {
  7.   pinMode(red, OUTPUT);
  8.   pinMode(green, OUTPUT);
  9.   pinMode(yellow, OUTPUT);
  10.  
  11. }
  12.  
  13. void loop() {
  14.   digitalWrite(red, HIGH);
  15.   digitalWrite(green, LOW);
  16.   digitalWrite(yellow, LOW);
  17.   delay(delay1);
  18.   digitalWrite(red, LOW);
  19.   digitalWrite(green, HIGH);
  20.   digitalWrite(yellow, LOW);
  21.   delay(delay1);
  22.   digitalWrite(red, LOW);
  23.   digitalWrite(green, LOW);
  24.   digitalWrite(yellow, HIGH);
  25.   delay(delay1);
  26. }

We try to test this code is OK! Done complier and Good upload.

As Video below.

We edit delay time easily! If you do not understand the programming. Please read back. Sample one LED flasher and Blink 2 LED flasher using Arduino Yes You Can!