Está en la página 1de 5

boolean t = true;

int i = 12;
void setup()
{
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}

void loop()
{
digitalWrite(i,HIGH);
delay(50);
digitalWrite(i,LOW);
if(t == true)
{
i = i - 1;
}
else
{
i = i + 1;
}
if(i < 5)
{
i = 6;
t = false;
}
if(i > 12)
{
i = 11;
t = true;
}

}
/*
For Loop Iteration

Demonstrates the use of a for() loop.


Lights multiple LEDs in sequence, then in reverse.

The circuit:
- LEDs from pins 2 through 7 to ground

created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/ForLoop
*/

int timer = 100; // The higher the number, the slower the timing.

void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}

// loop from the highest pin to the lowest:


for (int thisPin = 7; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
USING ARDUINO NANO

int led_pin=13;
void setup()
{
pinMode(led_pin,OUTPUT);
digitalWrite(led_pin,HIGH);
delay(1000);
digitalWrite(led_pin,LOW);
}
void loop()
{
}
Output:- Led will blink once...
But modifying code,
int led_pin=13;
void setup() {
pinMode(led_pin,OUTPUT);
}
void loop()
{
digitalWrite(led_pin,HIGH);
delay(500);
digitalWrite(led_pin,LOW);
delay(500);
}
Output:- Led will blink for infinity.
Conclusion:
The pinMode command sets the LED pin to be an output. The first digitalWrite
command says to
set pin 13 of the Arduino to HIGH, or +5 volts. This sends current from the pin,
through the resistor, through the LED (which lights it) and to ground. The
delay(500) command waits for 500 msec. The second digitalWrite command
sets pin 13 to LOW or 0 V stopping the current thereby turning the LED off.
Code within the brackets defining the loop() function is repeated forever,
which is why the LED blinks.

También podría gustarte