Sunteți pe pagina 1din 13

Project 1 : Blink an LED with a time period of 1000ms.

This example shows the simplest thing you can do with an Arduino to see physical output: it
blinks an LED.
Hardware Required

1. Arduino Board
2. LED
Circuit Diagram

To build the circuit, attach a 220-ohm resistor to pin 13. Then attach the long leg of an LED (the
positive leg, called the anode) to the resistor. Attach the short leg (the negative leg, called the cathode) to
ground. Then plug your Arduino board into your computer, start the Arduino program, and enter the code
below.
Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this
example with no hardware attached, you should see that LED blink.

Schematic Diagram

Program Code

int led = 13;


void setup() {
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

Detailed Description

int led = 13;

// the setup routine runs once when you press reset:


void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:


void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Project II : Dancing Light with 6 LEDs.
This example shows you how you can turn on a sequence of pins whose numbers are
neither contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and
then use for loops to iterate over the array.
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm
resistors, just like in the For Loop. However, here the order of the LEDs is determined by their order in
the array, not by their physical order. You don't have to have the pins sequential to one another, or even in
the same order. You can rearrange them however you want.
Hardware Required

1. Arduino Board
2. (6) 220 ohm resistors
3. (6) LEDs
4. hook-up wire
5. breadboard
Circuit Diagram

Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.
Schematic Diagram

Program Code

int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int timer = 100;

void setup(){
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
}

void loop() {
digitalWrite(pin2, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
delay(timer);

digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);

digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);

digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);

digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin6, LOW);
delay(timer);

digitalWrite(pin7, HIGH);
delay(timer);
digitalWrite(pin7, LOW);
delay(timer);
digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin6, LOW);
delay(timer);

digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);

digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);

digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
}

Detailed Description

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


the timing.
int ledPins[] = {
2, 7, 4, 6, 5, 3 }; // an array of pin numbers to which
LEDs are attached
int pinCount = 6; // the number of pins (i.e. the
length of the array)

void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);

// loop from the highest pin to the lowest:


for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}

Project III. Dancing light with Simple FOR Instructions.


Often you want to iterate over a series of pins and do something to each one. For instance, this
example blinks 6 LEDs attached the Arduino by using a for() loop to cycle back and forth through digital
pins 2-7. The LEDS are turned on and off, in sequence, by using both the digitalWrite() and delay()
functions .
Hardware Required

1. Arduino Board
2. (6) 220 ohm resistors
3. (6) LEDs
4. hook-up wire
5. breadboard
Circuit Diagram

Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.

Schematic Diagram
Program Code

int timer = 100void setup() {

for (int thisPin = 2; thisPin < 8; thisPin++) {


pinMode(thisPin, OUTPUT);
}
}
void loop() {
for (int thisPin = 2; thisPin < 8; thisPin++) {
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
}
for (int thisPin = 7; thisPin >= 2; thisPin--) {
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
}
}

Detailed Description

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);
}
}
Project IV. Dancing light with Simple FOR Instructions.
This variation on the For Loop example shows how to use an array. An array is a variable with
multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice
cube tray. It's like a series of linked cups, all of which can hold the same maximum value.
The For Loop example shows you how to light up a series of LEDs attached to pins 2 through 7 of the
Arduino, with certain limitations (the pins have to be numbered contiguously, and the LEDs have to be
turned on in sequence).
This example shows you how you can turn on a sequence of pins whose numbers are neither
contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and then use
for loops to iterate over the array.
This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm
resistors, just like in the For Loop. However, here the order of the LEDs is determined by their order in
the array, not by their physical order.
This technique of putting the pins in an array is very handy. You don't have to have the pins
sequential to one another, or even in the same order. You can rearrange them however you want.
Hardware Required

1. Arduino Board
2. (6) 220 ohm resistors
3. (6) LEDs
4. hook-up wire
5. breadboard
Circuit Diagram

Connect six LEDS, with 220 ohm resistors in series, to digital pins 2-7 on your Arduino.

Program Code

int timer = 100;


int ledPins[] = {
2, 7, 4, 6, 5, 3 };
int pinCount = 6;
void setup() {
// for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
delay(timer);

for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {

digitalWrite(ledPins[thisPin], HIGH);
delay(timer);

digitalWrite(ledPins[thisPin], LOW);
delay(timer);
}
}
Detailed Description

int timer = 100; // The higher the number, the slower the
timing.
int ledPins[] = {
2, 7, 4, 6, 5, 3 }; // an array of pin numbers to which LEDs
are attached
int pinCount = 6; // the number of pins (i.e. the length of
the array)

void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}

S-ar putea să vă placă și