Ciao a tutti,
un amico compie 50 anni e, visto che sulla torta “artistica” metterà un modellino (bello grosso) di uno di quei vecchi e bellissimi van anni '60/'70 della Volkswagen (il Kombi per la precisione), mi ha chiesto se fosse possibile, con Arduino, realizzare “l'impianto luci”, comprensivo di quattro frecce lampeggianti. Lo so, un qualsiasi Atmel (anche Attiny, che vorrei utilizzare per rendere più piccola la millefori da imboscare nel modellino del furgone a progetto finito) è sicuramente sovradimensionato per una cosa del genere ma, da newbie quale mi pregio di essere, non mi vengono altre soluzioni in mente
Ho pensato di utilizzare un pulsante collegato a un input digitale che, a seconda di quante volte viene premuto, accende prima i fari, poi le frecce e alla terza pressione spegne tutto.
Realizzato l'immondo circuito su breadboard, e scritto/scopiazzato un po' di codice, funziona tutto o almeno sembra.
Io però mi complico sempre la vita e ho pensato di mettere un cicalino che, alla terza pressione del pulsante, gli suoni la classica “Tanti auguri”. E qui mi sono impallato.
Il codice non fa altro che leggere se il pulsante è stato premuto, e ogni volta incrementa di 1 il suo "statopulsante". Quando il valore supera 3, azzero. Con uno switch mi regolo quindi per far accendere fari e far lampeggiare le frecce (per farlo ho utilizzato paro paro l'esempio Blink without delay, mettendolo in una funzione).
Mi funzionano le prime due pressioni del pulsante; 1: accende "i fari" e 2 avvia le frecce. Alla terza pressione, le frecce si bloccano, e la musichetta suona in loop (ma dovrebbe suonare una volta sola, chi glielo dice di looppare?).
Ho anche provato, nel "case 3", a fare qualsiasi altra cosa (accendere il LED 13, stampare qualcosa sulla seriale, ma il risultato è sempre lo stesso, non riesco a capire perché.
Di seguito il codice, che spero possa essere più esplicativo di tutto 'sto mio sproloquio
#define LUCI 9 // pin di output delle luci
#define FRECCE 10 // pin di output delle frecce
#define PULSANTE 11 // pin di input del pulsante
byte statopulsante = 0; // pulsante non premuto
int valpulsante = 0; // val conserva lo stato del pin di input
// Variabili per far lampeggiare le frecce, cannibalizzato dall' esempio Blink without Delay
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 500; // interval at which to blink (milliseconds)
int speakerPin = 7;
void setup() {
Serial.begin(9600);
pinMode(PULSANTE, INPUT); // imposta il pin digitale come input
pinMode(LUCI, OUTPUT); // imposta il pin digitale come output
digitalWrite(LUCI, LOW); // spegne il led all'avvio, non si mai
pinMode(FRECCE, OUTPUT); // imposta il pin digitale come output
digitalWrite(FRECCE, LOW); // spegne il led all'avvio, non si mai
pinMode(speakerPin,OUTPUT);
}
void loop() {
valpulsante = digitalRead(PULSANTE); // legge e conserva il valore dell'input
if ((valpulsante == HIGH)) {
statopulsante++;
if (statopulsante > 3) { statopulsante = 0; } // se il pulsante viene premuto più di 3 volte si riparte da zero
delay(350);
}
switch ( statopulsante ) {
case 0:
Serial.println("Premere pulsante");
digitalWrite(LUCI, LOW); // spegne i fari all'avvio, non si mai
digitalWrite(FRECCE, LOW); // spegne le frecce all'avvio, non si mai
break;
case 1:
Serial.println(statopulsante);
digitalWrite(LUCI, HIGH); // accendo le luci
break;
case 2:
Serial.println(statopulsante);
frecce(); // accendo le frecce
break;
case 3:
Serial.println(statopulsante);
auguri();
// digitalWrite(13, HIGH); // accendo le luci
//Serial.print("PIPPO");
break;
} // fine switch
} // fine loop
void frecce (){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(FRECCE, ledState);
}
}
// codice per musichetta di auguri
//==========================================================================
// Author : CYTRON TECHNOLOGIES SDN BHD
// Project : Arduino Duemilanove
// Project description : Project_11: Piezo Buzzer - Melody
//==========================================================================
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_C4_1 260
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
// notes in the melody:
int melody[] = {
NOTE_C4_1,NOTE_C4, NOTE_D4, NOTE_C4,NOTE_F4,NOTE_E4,
NOTE_C4_1,NOTE_C4,NOTE_D4,NOTE_C4,NOTE_G4,NOTE_F4,
NOTE_C4_1,NOTE_C4,NOTE_C5,NOTE_A4,NOTE_F4,NOTE_E4,NOTE_D4,
NOTE_AS4,NOTE_AS4,NOTE_A4,NOTE_F4,NOTE_G4,NOTE_F4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
6, 6, 3, 3,3,3,
6, 6, 3, 3,3,3,
6, 6, 3, 3,3,3,3,
6, 6, 3, 3,3,3 };
void auguri()
{
for (int thisNote = 0; thisNote < 26; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(speakerPin, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration + 50; //delay between pulse
delay(pauseBetweenNotes);
noTone(speakerPin); // stop the tone playing
}
}
Non capisco anzitutto perché il "delay" delle frecce si blocchi... non dovrebbe continuare a prescindere da quel che avviene nello sketch? E poi non mi spiego perché la musichetta vada in loop anziché essere eseguita soltanto una volta.
Dove sto sbagliando?
Grazie in anticipo per l'aiuto