Skip to content

Instantly share code, notes, and snippets.

@marcelo-reis
Last active May 9, 2019 23:59
Show Gist options
  • Save marcelo-reis/75212f53e66e261a47d932008c21a65b to your computer and use it in GitHub Desktop.
Save marcelo-reis/75212f53e66e261a47d932008c21a65b to your computer and use it in GitHub Desktop.
Arduino - Faz um pequeno piano de 3 Botões, onde cada botão toca um trecho de musica em um Buzzer e acende um LED correspondente.
const int BUZZER = 10; // Buzzer
const int LED_RED = 13; // Led Vermelho
const int LED_YLW = 12; // Led Amarelo
const int LED_GRN = 11; // Led Verde
const int BTN_RED = 9; // Botão Vermelho
const int BTN_YLW = 8; // Botão Amarelo
const int BTN_GRN = 7; // Botão Verde
// Frequencias de cada nota musical
const int C = 261; // Dó
const int D = 293; // Ré
const int E = 329; // Mi
const int F = 349; // Fá
const int G = 391; // Sol
const int A = 440; // Lá
const int B = 493; // Si
void setup() {
// put your setup code here, to run once:
pinMode(BUZZER, OUTPUT );
pinMode(LED_RED, OUTPUT);
pinMode(LED_YLW, OUTPUT);
pinMode(LED_GRN, OUTPUT);
pinMode(BTN_RED, INPUT_PULLUP);
pinMode(BTN_YLW, INPUT_PULLUP);
pinMode(BTN_GRN, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
// Salva o estado de cada botao
bool ESTADO_BTN_R = digitalRead(BTN_RED);
bool ESTADO_BTN_Y = digitalRead(BTN_YLW);
bool ESTADO_BTN_G = digitalRead(BTN_GRN);
if (ESTADO_BTN_R == LOW){
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_YLW, LOW );
digitalWrite(LED_GRN, LOW );
tone(BUZZER, C); // Dó
delay(250);
tone(BUZZER, D); // Ré
delay(250);
tone(BUZZER, E); // Mi
delay(250);
tone(BUZZER, F); // Fá
delay(500);
noTone(BUZZER);
delay(25);
tone(BUZZER, F);//Fá
delay(200);
noTone(BUZZER);
delay(50);
tone(BUZZER, F); //Fá
delay(200);
}
else if (ESTADO_BTN_Y == LOW){
digitalWrite(LED_RED, LOW );
digitalWrite(LED_YLW, HIGH);
digitalWrite(LED_GRN, LOW );
tone(BUZZER, C); // Dó
delay(250);
tone(BUZZER, D); // Ré
delay(250);
tone(BUZZER, C); // Dó
delay(250);
tone(BUZZER, D); // Ré
delay(500);
noTone(BUZZER);
delay(25);
tone(BUZZER, D); // Ré
delay(200);
noTone(BUZZER);
delay(50);
tone(BUZZER, D); // Ré
delay(200);
}
else if (ESTADO_BTN_G == LOW){
digitalWrite(LED_RED, LOW );
digitalWrite(LED_YLW, LOW );
digitalWrite(LED_GRN, HIGH);
tone(BUZZER, C); // Dó
delay(250);
tone(BUZZER, G); // Sol
delay(250);
tone(BUZZER, F); // Fá
delay(250);
tone(BUZZER, E); // Mi
delay(500);
noTone(BUZZER);
delay(25);
tone(BUZZER, E); // Mi
delay(200);
noTone(BUZZER);
delay(50);
tone(BUZZER, E); // Mi
delay(200);
}
else{
noTone(BUZZER);
digitalWrite(LED_RED, LOW );
digitalWrite(LED_YLW, LOW );
digitalWrite(LED_GRN, LOW );
}
}
@marcelo-reis
Copy link
Author

@marcelo-reis
Copy link
Author

Lista de Componentes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment