1

The idea is when a switch is pushed, the corresponding tone will be played as well as lighting up the corresponding LED. I have the circuit set up to where each array matches another, for example Switch one will play tone 1 and light LED 1. When the switch is released the LED turns off and the tone no longer plays.

However I have to use an Array for the sketch instead of declaring each individual switch, led, and tone. My issue is when I compile the code, I get the error message "i" is not declared. Not sure where my error is within the for loop in the setup portion of the sketch.

I tried following the example from the Exploring Arduino web page but it did not help.

#define NOTE_C   262 //HZ
#define NOTE_D   294 //HZ
#define NOTE_E   330 //HZ
#define NOTE_G   392 //HZ
#define NOTE_A   440 //HZ

const int SPEAKER = 9;   //speaker on pin 10
int LED[5] = {13, 12, 11, 10, 8};   //led pins
int ledcount = 5;
int buttonState; 
int buttonState1;
int buttonState2;
int buttonState3;
int buttonState4;

//Button Array
int Buttons[5]= {3, 4, 5, 6, 7};
int ButtonCount = 5;
const int numButtons = sizeof(Buttons) / sizeof (Buttons[0]);

//Tone Note Array
int Notes[5] = {NOTE_C, NOTE_D, NOTE_E, NOTE_G, NOTE_A};

void setup()
{
  for (int i = 0; i < sizeof (Buttons) / sizeof(Buttons[0]); i++){
  pinMode(Buttons[], INPUT);
}
  for (int i = 0; i < sizeof (LED) / sizeof (LED[0]); i++);{
  pinMode(LED[], OUTPUT);
}
  for (int i = 0; i < numButtons; i++){
  pinMode(Buttons[i], INPUT);
  debouncer[i].attach(Buttons[i]);
  debouncer[i].interval(100);
}
  buttonState = digitalRead(Buttons[0]);
  buttonState1 = digitalRead(Buttons[1]);
  buttonState2 = digitalRead(Buttons[2]);
  buttonState3 = digitalRead(Buttons[3]);
  buttonState4 = digitalRead(Buttons[4]);
}


void loop()
{


  if (buttonState == HIGH)
  { tone(SPEAKER, Notes[0]);
  digitalWrite(LED[0], HIGH);
  }
  else if (buttonState1 == HIGH)
  {tone(SPEAKER, Notes[1]);
  digitalWrite(LED[1], HIGH);
 }
  else if (buttonState2 == HIGH)
  {tone(SPEAKER, Notes[2]);
  digitalWrite(LED[2], HIGH);
 }
  else if (buttonState3 == HIGH)
  {tone(SPEAKER, Notes[3]);
  digitalWrite(LED[3], HIGH);
 }
  else if (buttonState4 == HIGH)
  {tone(SPEAKER, Notes[4]);
   digitalWrite(LED[4], HIGH);
 }

  else
 {
  digitalWrite(LED[5], LOW);
  noTone(SPEAKER);
 }
}
  • I have to use an Array ..... is this a school assignment? – jsotola Oct 15 '18 at 4:48
  • @jsotola, It is a school assignment.Its why i ask for suggestions and not completed codes. It doesnt help if someone does the work for me. Can only learn through doing. Millis() was a great thing to learn through here, millis() will not even be covered through the curriculum. However I don't think ill need millis() in this code. Im now using the Bounce2 library and it gives a debounce interval. Ill update my code for the changes made. – Steven Oct 15 '18 at 12:32
  • i ask for suggestions and not completed codes .... i do not see that – jsotola Oct 15 '18 at 16:27
  • @jsotola I do apologize for not making that clearer in this post. – Steven Oct 15 '18 at 18:50
4

You have a rogue ; here:

  for (int i = 0; i < sizeof (LED) / sizeof (LED[0]); i++);{
  pinMode(LED[], OUTPUT);                                 ^------HERE
}

That is terminating the for, making it empty, and the following block is a completely different scope.

Another thing I spotted - you aren't putting i into your array index LED[] (should be LED[i]). The same is true with Buttons[].

However, on a different subject, this:

buttonState == digitalRead(Buttons[0]);
buttonState == digitalRead(Buttons[1]);
buttonState == digitalRead(Buttons[2]);
buttonState == digitalRead(Buttons[3]);
buttonState == digitalRead(Buttons[4]);

is pretty meaningless. You are comparing values and throwing away the result.

In a similar vein:

if (buttonState = Buttons[0])

stores the current value of Buttons[0] into buttonState and succeeds if Buttons[0] is anything other than 0. The same goes for all the others.

You need to sort out your comparisons (==) and your assignments (=) and also sort out what you think you are doing with all those buttonStates at the top of loop.

  • I did not notice the extra semi colons which explains why it was not declared in the scope. I made the changes as well, tone plays and led lights up just wont turn off. Im assuming ill need to debounce the switches in order to get them to work. – Steven Oct 14 '18 at 15:34
  • I debounced the switches and im still having the same issue. Its debounced via array for loop, through the bounce2 library. I am still not getting the led or other switches to work properly. It will not let me post the new code in a comment either. – Steven Oct 14 '18 at 21:41
  • Unless you fixed all the other issues with your code you have no hope of it working. – Majenko Oct 14 '18 at 21:56
  • I got it working now thank you for your help. The issue ended up being the two things you pointed out, and how i was calling on the array. – Steven Oct 15 '18 at 20:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.