#include <EEPROM.h>
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4, 0
};
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4, 4
};
#define SPEAKER 13
#define POTENTIOMETER 0
#define ALLINPUT 0x60
#define BUTTON1 0x40
#define BUTTON2 0x20
#define POTMAX 1023
#define MAX 100
#define MIN -100
#define NOTES 9
#define TEMPO 1.30
int counter = 0;
int pValue;
int pitchIncrease = 0;
int storePitch = 0;
int sw = 1;
bool b1Status;
bool b2Status;
int finalPitch;
int pauseBetweenNotes;
int duration;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
DDRD = DDRD & ~ALLINPUT;
Serial.print("DDRD be like: ");
Serial.println(DDRD);
EEPROM.write(storePitch, 0);
Serial.println("Initialization Complete hehe");
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println(PIND);// 0000 0011
b1Status = PIND & BUTTON1;
b2Status = PIND & BUTTON2;
pValue = analogRead(POTENTIOMETER);
pValue = map(pValue, 0, POTMAX, MIN, MAX);
Serial.println(pValue);
if(b1Status){ // 0100 0001 & 0100 0000
Serial.println("Button 1 Pressed");
sw++;
Serial.print("Switch Counter: ");
Serial.println(sw);
delay(300);
}
if(b2Status){ // 0010 0001 & 0010 0000
Serial.println("Button 2 Pressed");;
EEPROM.update(0, pValue);
Serial.print("Saved Pitch: ");
Serial.println(pValue);
delay(300);
}
if((b1Status) && (b2Status)){ // 0010 0001 & 0010 0000
Serial.println("BOTH Pressed");
sw = 0;
counter = 0;
delay(300);
}
if(sw%2 != 0){
if(counter < NOTES){
EEPROM.put(0, storePitch);
Serial.print("Store Pitch: ");
Serial.println(storePitch);
finalPitch = storePitch + pValue;
if(finalPitch > MAX){
finalPitch = MAX;
}
if(finalPitch < MIN){
finalPitch = MIN;
}
duration = 1000 / noteDurations[counter];
if(melody[counter] == 0){
tone(SPEAKER, melody[counter], duration);
}
else{
tone(SPEAKER, melody[counter] + finalPitch, duration);
}
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
pauseBetweenNotes = duration * TEMPO;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(SPEAKER);
counter++;
}
else{
counter = 0;
noTone(SPEAKER);
}
}
}