#include <Button.h>
#include <Tone.h>
#define ERROR_WINDOW 100
#define ERROR_WINDOW2 70
Tone speaker1;
Tone speaker2;
int ledchourd1 = 10;
int ledchourd2 = 11;
int ledchourd3 = 12;
int ledflex1 = 13;
int ledon = 14;
int curOctave=0;
Button btndown =Button( 4,PULLDOWN);
Button btnup =Button( 5,PULLDOWN);
Button btnchourd1 =Button( 6,PULLDOWN);
Button btnchourd2 = Button(7,PULLDOWN);
Button btnchourd3 = Button(8,PULLDOWN);
int btnon = 9;
int flex = 5;
int val;
int flexNote;
int num=0;
int note;
boolean playMusic=false;
int btnOnVal;
int notes[]={
262,277,294,311,330,349,370,392,414,440,466,494};
/* there's one button that is on and off and when its on a green led turns on,
and there are three buttons that each play a chourd and when a chourd is playing a led lights up with it.
for the flex sensor isf you bend it one when it plays a realy high note and a led lights up and when you bend it the other way it plays a really low note and a led lights up
and the ldr's each play one note in the scale and there are two buttons and one makes the scale mave up an octive and the other down and octuve.
*/
void setup(){
Serial.begin(9600);
speaker1.begin(2);
speaker2.begin(3);
pinMode (ledchourd1,OUTPUT);
pinMode (ledchourd2,OUTPUT);
pinMode (ledchourd3,OUTPUT);
pinMode (ledflex1,OUTPUT);
pinMode (ledon,OUTPUT);
curOctave=0;
}
void loop(){
btnOnVal=digitalRead(btnon);
if(btnOnVal){
playMusic=true;
}
else{
playMusic=false;
}
if(playMusic){
//turn on LED
digitalWrite(ledon,HIGH);
//////////////////////////////////////////////////////
if(btnup.uniquePress()){
raiseOctave();
}
if(btndown.uniquePress()){
lowerOctave();
}
if(btnchourd1.isPressed()){
speaker1.play(notes[0]);
speaker2.play(notes[4]);
delay(120);
speaker1.stop();
speaker2.stop();
digitalWrite(ledchourd1,HIGH);
}
if(btnchourd1.stateChanged()){
speaker1.stop();
speaker2.stop();
digitalWrite(ledchourd1,LOW);
}
if(btnchourd2.isPressed()){
speaker1.play(notes[1]);
speaker2.play(notes[5]);
digitalWrite(ledchourd2,HIGH);
delay(120);
speaker1.stop();
speaker2.stop();
}
if(btnchourd2.stateChanged()){
speaker1.stop();
speaker2.stop();
digitalWrite(ledchourd2,LOW);
}
if(btnchourd3.isPressed()){
speaker1.play(notes[2]);
speaker2.play(notes[6]);
delay(120);
speaker1.stop();
speaker2.stop();
digitalWrite(ledchourd3,HIGH);
}
if(btnchourd3.stateChanged()){
speaker1.stop();
speaker2.stop();
digitalWrite(ledchourd3,LOW);
}
////////////////////////////////////////////////
//this is the code for the flex sensor
//right now it plays C4-C5
int flexVal=analogRead(flex);
if(flexVal<75){
flexNote=map(flexVal,30,75, 0,11);
speaker1.play(pow(2,curOctave)*notes[flexNote]);
digitalWrite(ledflex1,HIGH);
delay(100);
speaker1.stop();
}
else{
digitalWrite(ledflex1,LOW);
}
////////////////////////////////////////////////
//A0
int valA0 = analogRead(A1);
int valA2 = analogRead(A2);
val = analogRead(A3);
int valA4 = analogRead(A4);
if(valA0>(820-ERROR_WINDOW) &&valA0<(820+ERROR_WINDOW)){
note=notes[7];
Serial.println("A0=2");
}
else if(valA0>(120-ERROR_WINDOW) &&valA0<(120+ERROR_WINDOW)){
note=notes[6];
Serial.println("A0=1");
}
else if(valA2>(900-ERROR_WINDOW) &&valA2<(900+ERROR_WINDOW)){
note=notes[5];
Serial.println("A2-2");
}
else if(valA2>(170-ERROR_WINDOW) &&valA2<(170+ERROR_WINDOW)){
note=notes[4];
Serial.println("A2=1");
}
else if(val>(880-ERROR_WINDOW) &&val<(880+ERROR_WINDOW)){
note=notes[3];
Serial.println("A3-2");
}
else if(val>(150-ERROR_WINDOW) &&val<(150+ERROR_WINDOW)){
Serial.println("A3=1");
note=notes[2];
}
else if(valA4>(830-ERROR_WINDOW) &&valA4<(830+ERROR_WINDOW)){
note=notes[1];
Serial.println("A4-2");
}
else if(valA4>(130-ERROR_WINDOW) &&valA4<(130+ERROR_WINDOW)){
Serial.println("A4=1");
note=notes[0];
}
else{
note=0;
}
playIt();
// Serial.println(valA2);
}
////////////////////////////////////////////////////////////
else{
speaker1.stop();
speaker2.stop();
digitalWrite(ledon,LOW);
}
}
void lowerOctave(){
if(curOctave>0){
curOctave--;
Serial.print("curOctave=");
Serial.println(curOctave);
}
setOctave(curOctave);
}
void raiseOctave(){
if(curOctave<3){
curOctave++;
Serial.print("curOctave=");
Serial.println(curOctave);
}
setOctave(curOctave);
}
void setOctave(int o){
curOctave=o;
}
int getOctave(){
return curOctave;
}
void playIt(){
if(note){
curOctave=getOctave();
Serial.println(curOctave);
note=(note*int(pow(2,curOctave)));
Serial.println(note);
if(num==0){
speaker1.play(note);
num=1;
}
if(num==1){
speaker2.play(note);
num=0;
}
}
else{
speaker1.stop();
speaker2.stop();
num=0;
}
}