Apri il manuale o leggi l'articolo di presentazione sul sito del FabLab
const int ledPin13 = 13;
const int ledPin12 = 12;
const int ledPin11 = 11;
const int potenziometro = A0;
const int buzzer = 6;
unsigned long tempoLed13 = 0;
unsigned long tempoLed12 = 0;
unsigned long tempoNota = 0;
unsigned long inizioNota = 0;
const unsigned long intervalloLed13 = 1000;
const unsigned long intervalloLed12 = 500;
const unsigned long durataNota = 100;
bool statoLed13 = LOW;
bool statoLed12 = LOW;
bool notaAlta = true;
bool buzzerAttivo = false;
void setup() {
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(potenziometro, INPUT);
}
void loop() {
unsigned long tempoAttuale = millis();
if (tempoAttuale - tempoLed13 >= intervalloLed13) {
tempoLed13 = tempoAttuale;
statoLed13 = !statoLed13;
digitalWrite(ledPin13, statoLed13);
}
if (tempoAttuale - tempoLed12 >= intervalloLed12) {
tempoLed12 = tempoAttuale;
statoLed12 = !statoLed12;
digitalWrite(ledPin12, statoLed12);
}
int valorePotenziometro = analogRead(potenziometro);
int luminosita = map(
valorePotenziometro,
0,
1023,
0,
255
);
analogWrite(ledPin11, luminosita);
unsigned long intervalloNote = map(
valorePotenziometro,
0,
1023,
1000,
150
);
if (!buzzerAttivo &&
tempoAttuale - tempoNota >= intervalloNote) {
tempoNota = tempoAttuale;
inizioNota = tempoAttuale;
buzzerAttivo = true;
if (notaAlta) {
tone(buzzer, 1000);
} else {
tone(buzzer, 250);
}
notaAlta = !notaAlta;
}
if (buzzerAttivo &&
tempoAttuale - inizioNota >= durataNota) {
noTone(buzzer);
buzzerAttivo = false;
}
}
const int BUZZER_PIN = 6;
// Frequenze delle note
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_C5 523
// Melodia semplificata
int melody[] = {
NOTE_G4, NOTE_G4, NOTE_G4,
NOTE_E4, NOTE_AS4, NOTE_G4,
NOTE_E4, NOTE_AS4, NOTE_G4,
NOTE_D4, NOTE_D4, NOTE_D4,
NOTE_E4, NOTE_AS4, NOTE_F4,
NOTE_E4, NOTE_AS4, NOTE_G4
};
// Durata: 4 = quarto, 8 = ottavo, 2 = mezza
int durations[] = {
4, 4, 4,
8, 8, 4,
8, 8, 2,
4, 4, 4,
8, 8, 4,
8, 8, 2
};
void setup() {
int numberOfNotes = sizeof(melody) / sizeof(melody[0]);
for (int i = 0; i < numberOfNotes; i++) {
int noteDuration = 1000 / durations[i];
tone(BUZZER_PIN, melody[i], noteDuration);
delay(noteDuration * 1.30);
noTone(BUZZER_PIN);
}
}
void loop() {
// Ripete la melodia ogni 2 secondi
delay(2000);
setup();
}
Sabato 24 ottobre