The code as written on the project book is as follows:
int buttons[6]; // sets up a 6 element array
int buttons[0] = 2; // assigns 2 to the first element of the array
// sets up a 4 element array and intializes it. Starts out with
// frequencies for C (262 Hz), D (294 Hz), E (330 Hz) and F (349 Hz).
// Because it is declared before setup(), it is aglobal variable array
int notes[] = {262, 294, 330, 349};
void setup()
{
Serial.begin(9600); // starts serial comm with computer
}
void loop()
{
int keyVal = analogRead(A0); // reads pin A0 and stores value in keyVal
Serial.println(keyVal);
// using an If Else loop to assign each a value a different tone
if(keyVal == 1023)
{
tone(8, notes[0]);
}
else
if(keyVal >= 990 && KeyVal <= 1010)
{
tone(8, notes[1]);
}
else
if(keyVal >= 505 && keyVal <=515)
{
tone(8,notes[2]);
}
else
if(keyVal >= 5 && keyVal <=10)
{
tone(8,notes[3]);
}
else
{
noTone(8); // stops playing notes when no button is pressed
}
}
As written, it gives me the following two errors:
Arduino: 1.8.8 (Windows Store 1.8.19.0) (Windows 10), Board: "Arduino/Genuino Uno"
sketch_07A_feb18_Keyboard_Instrument:2:14: error: conflicting declaration 'int buttons [0]'
int buttons[0] = 2; // assigns 2 to the first element of the array
C:\Users\luisc\Downloads\sketch_07A_feb18_Keyboard_Instrument\sketch_07A_feb18_Keyboard_Instrument.ino:1:5: note: previous declaration as 'int buttons [6]'
int buttons[6]; // sets up a 6 element array
C:\Users\luisc\Downloads\sketch_07A_feb18_Keyboard_Instrument\sketch_07A_feb18_Keyboard_Instrument.ino: In function 'void loop()':
sketch_07A_feb18_Keyboard_Instrument:26:23: error: 'KeyVal' was not declared in this scope
if(keyVal >= 990 && KeyVal <= 1010)
exit status 1
conflicting declaration 'int buttons [0]'
I´m able to clear the first error by changing the first and second lines to read:
int buttons[] = {2, 0, 0, 0, 0, 0}; // sets up and defines a 6 element array
// int buttons[0] = 2; // assigns 2 to the first element of the array
but I´ve been unable to clear the second error:
'KeyVal' was not declared in this scope
if(keyVal >= 990 && KeyVal <= 1010)
Suggestion are welcome!!!