Skip to content

Instantly share code, notes, and snippets.

@AlessandroSpallina
Created April 3, 2015 11:21
Show Gist options
  • Save AlessandroSpallina/e1ff8ba170de32b76f66 to your computer and use it in GitHub Desktop.
Save AlessandroSpallina/e1ff8ba170de32b76f66 to your computer and use it in GitHub Desktop.
Arduino button led buzzer
/****************************************************************************
* Copyright © 2015 Alessandro Spallina
* email: [email protected]
* website: aleksnote.altervista.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************
* Questo listato permette di far suonare ad un piezoelettrico attivo per arduino
* una piccola melodia 8bit alla pressione del bottone relativo (e accensione del led
* corrispondente); puoi, aprendo un monitor comunicante con la porta seriale 9600
* di Arduino, verificare l'azione in esecuzione su Arduino e inviare ad esso delle
* note che suonerà come se fosse un bel pianoforte da quattro soldi, lol.
NOTES (8bit):
do -> c
re -> d
mi -> e
fa -> f
sol -> g
la -> a
si -> b
DO -> C
*/
// *** CONSTANTS ***
const int buzzer=6;
const int ledOnBoard=13;
const int blueLed=9;
const int yellowLed=10;
const int blueButton=2;
const int yellowButton=3;
//*******************
// *** GLOBALS ***
int yellowState=0;
int blueState=0;
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
//*******************
// *** MUSIC ***
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(buzzer, HIGH);
delayMicroseconds(tone);
digitalWrite(buzzer, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration)
{
int i;
for(i=0; i<8; i++)
{
if(names[i]==note)
playTone(tones[i], duration);
}
}
//Chinese Childrens Fake Toy
void yellowPlay()
{
int i;
int length = 15;
char notes[] = "ccggaagffeeddc ";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
Serial.println("***yellowMusic***");
for(i=0;i<length;i++)
{
if(notes[i]==' ')
delay(beats[i]*tempo);
else
playNote(notes[i], beats[i]*tempo);
delay(tempo / 2);
}
Serial.println("***end***");
}
void bluePlay()
{
int i;
int length = 27;
char notes[] = "edcdeeedddeeeedcdeeeeddedc";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 1};
int tempo = 200;
Serial.println("***blueMusic***");
for(i=0;i<length;i++)
{
if(notes[i]==' ')
delay(beats[i]*tempo);
else
playNote(notes[i], beats[i]*tempo);
delay(tempo / 2);
}
Serial.println("***end***");
}
//*******************
// *** CANONICALS ***
void setup() {
pinMode(ledOnBoard, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(blueButton, INPUT);
pinMode(yellowButton, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
char incomingByte;
yellowState=digitalRead(yellowButton);
blueState=digitalRead(blueButton);
if(Serial.available()>0)
{
incomingByte=Serial.read();
playNote(incomingByte, 300);
digitalWrite(ledOnBoard, HIGH);
}
if(yellowState==HIGH)
{
digitalWrite(ledOnBoard, HIGH);
digitalWrite(yellowLed, HIGH);
Serial.println("YELLOW BUTTON PRESSED");
yellowPlay();
}
if(blueState==HIGH)
{
digitalWrite(ledOnBoard, HIGH);
digitalWrite(blueLed, HIGH);
Serial.println("BLUE BUTTON PRESSED");
bluePlay();
}
digitalWrite(ledOnBoard, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(blueLed, LOW);
}
//*****************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment