Dopo l’albero di Natale con Scratch, ecco le luci di Natale con Arduino:
Il relativo codice (migliorabilissimo):
/*
Arrays
Demonstrates the use of an array to hold pin numbers
in order to iterate over the pins in a sequence.
Lights multiple LEDs in sequence, then in reverse.
Unlike the For Loop tutorial, where the pins have to be
contiguous, here the pins can be in any random order.
The circuit:
* LEDs from pins 3 through 11 to ground
created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
modified 08 Dec 2014
by Gaspar Torriero
The original code is in the public domain.
http://www.arduino.cc/en/Tutorial/Array
*/
int N;
int timer = 100; // The higher the number, the slower the timing.
int selector = 4;
int ledPins0[] = {
3, 4, 5, 6, 7, 8, 9, 10, 11}; // an array of pin numbers to which LEDs are attached
int ledPins1[] = {
3, 6, 9, 4, 7, 10, 5, 8, 11}; // an array of pin numbers to which LEDs are attached
int ledPins2[] = {
3, 11, 4, 10, 5, 9, 6, 8, 7}; // an array of pin numbers to which LEDs are attached
int ledPins3[] = {
3, 5, 7, 9, 11, 4, 6, 8, 10}; // an array of pin numbers to which LEDs are attached
int pinCount = 9; // the number of pins (i.e. the length of the array)
void setup() {
for (int N = 3; N < 12; N++) {
pinMode(N,OUTPUT);
}
}
void loop() {
timer = analogRead(A0);
// one of the next two lines must be uncommented
selector = (analogRead(A1) / 255); // knob-selected status
//selector = random(3); //random status
if (selector == 0) {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins0[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins0[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins0[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins0[thisPin], LOW);
}
}
else if (selector == 1) {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins1[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins1[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins1[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins1[thisPin], LOW);
}
}
if (selector == 2) {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins2[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins2[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins2[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins2[thisPin], LOW);
}
}
else if (selector == 3) {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins3[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins3[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins3[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins3[thisPin], LOW);
}
}
}