Indice
Descrizione
Nel video impareremo a realizzare un progetto natalizio con LED lampeggianti, canzoni di Natale riprodotte con il buzzer, il potenziometro per regolare il volume, il display LCD per mostrare la canzone attuale e dei pulsanti per accendere il programma e cambiare le canzoni.
#include <RtttlPlayer.h>
#include <LiquidCrystal_I2C.h>
#define ENABLE_PIN 3
#define CHANGE_PIN 2
#define DEBOUNCE_DELAY 50
#define LED_DELAY 200
#define BUZZER_PIN 4
#define BACKLIGHT_PIN 5
int enableState = LOW;
int enableLastReading = LOW;
unsigned long enableTimestamp = 0;
bool enable = false;
int changeState = LOW;
int changeLastReading = LOW;
unsigned long changeTimestamp = 0;
char *songs[] = {
"We Wish You a |Merry Christmas:d=4,o=5,b=170:d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,2g,d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,1g,d,g,g,g,2f#,f#,g,f#,e,2d,a,b,8a,8a,8g,8g,d6,d,d,e,a,f#,2g,1p",
"Jingle Bells:d=4,o=5,b=170:b,b,b,p,b,b,b,p,b,d6,2g.,8a,2b.,8p,c6,c6,c6.,8c6,c6,b,b,8b,8b,b,a,a,b,2a,2d6,1p",
"Silent Night:d=4,o=4,b=125:f#.,8g#,f#,d#.,p.,f#.,8g#,f#,d#.,p.,2c#6,c#6,a#.,p.,2b,b,f#.,p.,2g#,g#,b.,8a#,g#,f#.,8g#,f#,d#.,p.,2g#,g#,b.,8a#,g#,f#.,8g#,f#,d#.,p.,2c#6,c#6,e.6,8c#6,a#,2b.,2d#6,p.,b,f#,d#,f#.,8e,c#,b.4,1p",
"Let it Snow:d=4,o=5,b=125:8c,8c,8c6,8c6,a#,a,g,f,2c,8c,16c,g.,8f,g.,8f,e,2c,d,8d6,8d6,c6,a#,a,2g.,8e.6,16d6,c6,8c.6,16a#,a,8a#.,16a,2f.,c,8c6,8c6,a#,a,g,f,2c,8c.,16c,g.,8f,g.,8f,e,2c,d,8d6,8d6,c6,a#,a,2g.,8e.6,16d6,c6,8c.6,16a#,a,8a.,16g,2f.1p"
};
int songsNumber = sizeof(songs)/sizeof(char *);
int songIndex = 0;
bool songChanged = true;
int led[] = { 12, 11, 10, 9, 8 };
unsigned long ledTimestamp = 0;
RtttlPlayer player(BUZZER_PIN);
LiquidCrystal_I2C lcd(0x3f, 16, 2);
void setup(){
pinMode(ENABLE_PIN, INPUT);
pinMode(CHANGE_PIN, INPUT);
for(int i = 0; i < 5; i++){
pinMode(led[i], OUTPUT);
}
randomSeed(analogRead(A0));
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(BACKLIGHT_PIN, OUTPUT);
}
void loop(){
int enableInput = digitalRead(ENABLE_PIN);
if(enableInput != enableLastReading){
enableTimestamp = millis();
}
if((millis() - enableTimestamp) > DEBOUNCE_DELAY){
if(enableInput != enableState && enableInput == HIGH){
enable = !enable;
songIndex = 0;
songChanged = true;
for(int i = 0; i < 5; i++){
digitalWrite(led[i], LOW);
}
lcd.clear();
analogWrite(BACKLIGHT_PIN, 0);
}
enableState = enableInput;
}
enableLastReading = enableInput;
if(!enable){
return;
}
int changeInput = digitalRead(CHANGE_PIN);
if(changeInput != changeLastReading){
changeTimestamp = millis();
}
if((millis() - changeTimestamp) > DEBOUNCE_DELAY){
if(changeInput != changeState && changeInput == HIGH){
songIndex = ++songIndex % songsNumber;
songChanged = true;
}
changeState = changeInput;
}
changeLastReading = changeInput;
if((millis() - ledTimestamp) > LED_DELAY){
int randomNumber = random(0, 31);
for(int i = 0; i < 5; i++){
digitalWrite(led[i], randomNumber & 1);
randomNumber >>= 1;
}
ledTimestamp = millis();
}
if(songChanged){
player.play(songs[songIndex]);
songChanged = false;
lcd.clear();
analogWrite(BACKLIGHT_PIN, 10);
String songTitle = player.getSongTitle();
int songTitleLength = songTitle.length();
int row = 0;
int column = 0;
for(int i = 0; i < songTitleLength; i++){
if(songTitle[i] != '|'){
lcd.setCursor(column, row);
lcd.print(songTitle[i]);
column++;
}else{
row++;
column = 0;
}
}
}else{
if(player.isPlaying()){
player.update();
}else{
songIndex = ++songIndex % songsNumber;
songChanged = true;
}
}
delay(1);
}