Okay this issue is driving me crazy. First off here is my code:
long freq[9];
int countnote = 0;
int countbeat = 0;
long a[5000];
int b[5000];
int j = 0;
long average1 = 0;
int play2 = 1;
int playback1 = 0;
int playstart = 0;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, OUTPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
freq[0] = 220;
freq[1] = 246.9;
freq[2] = 261.6;
freq[3] = 293.7;
freq[4] = 329.2;
freq[5] = 349.2;
freq[6] = 392;
freq[7] = 440;
freq[8] = 493.9;/*
int i;
for(i = 0;i<5000;i++)
{
a[i] = 0;
b[i] = 0;
}
*/
}
void play1()
{
int i;
long sum = 0;
int count = 0;
long average2 = 0;
for(i =2;i<11;i++)
{
if(digitalRead(i) == HIGH)
{
sum = sum + freq[i-2];
++count;
}
}
if(count > 0)
{
average2 = sum/count;
tone(11,average2);
}
else
{
average2 = 0;
noTone(11);
}/*
//this if statement will run when a new note or non note is played(average1 is not equal to average2)
if(average1 != average 2 && j < 5000)
{
a[j] = millis() - playstart;
b[j] = average1;
average1 = average2;
playstart = millis();
++j;
}
*/
}
/*
void playback()
{
int i;
for(i = 0; i<5000; i++)
{
if(b[i] > 0)
{
tone(11,b[i],a[i]);
}
else
{
delay(a[i]);
}
}
}
*/
void loop()
{
// if(digitalRead(12) == HIGH)
// {
//playstart = millis();
// play2 = 1;
// playback1 = 0;
// }
// else if(digitalRead(13) == HIGH)
// {
// playback1 = 1;
// play2 = 0;
//}
if(play2 == 1)
{
play1();
}
//else
//{
// playback();
//}
}
Now I have a board set up so that when I press a specific button it plays a certain tone out to the speaker, so in effect it is an electronic keyboard. Now I am trying to code it so that when in play mode it will record and you can play it back. My issue is that when I uncomment the commented sections the speaker will no longer play tones when the buttons are pressed. As you can see it is locked in play mode with the comments. The commented sections only alter arrays which have nothing to do with playing the notes, and are only to store the notes played into memory. So when I uncomment the piano should still work. It's almost like my code has too many "statements" to process.
Help Please.
b stores frequencies
a stores the length of the note