Sheet arduino n.1 (blinking led)¶
Blinking led¶
[Lezione del 29 ott 2019]
Il mio contenuto dello sketch per l’esercitazione è il seguente:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | int led = 8;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
int astatus = 0; // this will toggle between 0 / 1
int high_time = 0;
int low_time = 100;
while(1){
if(astatus==0)
high_time = 500;
else
high_time = 1000;
drive_led(high_time, low_time);
astatus++;
astatus = astatus % 2;
}
}
void drive_led(int high_time, int low_time){
digitalWrite(led, HIGH);
delay(high_time);
digitalWrite(led, LOW);
delay(low_time);
}
|
Accendere alternativamente 3 led¶
[Lezione del 04 nov 2019]
In coda alla scheda della esercitazione n.1 vi è la richiesta di accendere alternativamente due o tre led. Siccome siamo ingordi, facciamo tre. Il seguente video mostra lo schema montato e l’esecuzione dello sketch.
Lo sketch per accendere in alternativa i tre led è il seguente:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /* Embedded Systems Architecture - hardware
* application on 04th nov 2019
*
* turn on one by one 3 leds
*/
#define GLED 0 // green led port index in array LEDS
#define YLED 1 // yellow led port index
#define RLED 2 // red led port index
int LEDS[3] = {8, 9, 10};
void setup() {
for(int n = GLED; n <= RLED; n++)
pinMode(LEDS[n], OUTPUT);
}
void loop() {
int led_ndx = 0; // this will cicle from 0 to 2
int high_time = 1000; // turn on the led for 1000 msecs
int low_time = 0; // turn off the led forever
while(1){
drive_led(LEDS[led_ndx], high_time, low_time);
led_ndx++;
led_ndx %= 3;
}
}
/* drive the led @ the indicated port
* params:
* - int led port to drive
* - int high_time msecs to wait with led turned on, 0 means forever
* - int low_time msecs to wait with led turned off, 0 means forever
*/
void drive_led(int led, int high_time, int low_time){
digitalWrite(led, HIGH);
if(high_time != 0)
delay(high_time);
else
return;
digitalWrite(led, LOW);
if(low_time != 0)
delay(low_time);
}
|
Alla linea 11 vi è la definizione di un array di tre interi, che contengono
ognuno il numero della porta di un LED. Vi accediamo con gli indici
GLED
, YLED
, e RLED
, definiti con direttive di precompilazione
alle linee da 7 a 9.
Il resto consiste in un ciclo infinito in cui incrementiamo l’indice led_ndx
e
gli riassegniamo il modulo della divisione per 3. In tal modo cicliamo sugli indici
da 0 a 2 e accendiamo di conseguenza il led presente alla porta LEDS[led_ndx]
.
Per accendere il led usiamo la funzione drive_led
, leggermente modificata
per supportare il forever: quando il tempo di spegnimento (o accensione)
è zero, allora significa per sempre. L’accensione è prioritaria.
Accendere un led tramite pulsante¶
Nella stessa scheda della esercitazione n.1, vi è la richiesta di accendere un led secondo comando di un pulsante. Ecco il video del relativo assemblaggio e test
Lo sketch è il seguente:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /* Embedded Systems Architecture - hardware
* application on 04th nov 2019
*
* turn on the led when you push the button
*/
#define BUTTON 8 // button port
#define RLED 9 // red led port
void setup() {
pinMode(RLED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop() {
int button_status;
button_status = digitalRead(BUTTON);
digitalWrite(RLED, button_status);
}
|
Questo sketch è estremamente semplice: legge il valore di tensione dal pulsante e lo riscrive sul led.