"<all my variables> was not declared in this scope"

Hey everyone,

I apologize if this seems like a stupid error but I’ve tried googling it and I’ve declared all my variables but for some reason Arduino keeps telling me I haven’t declared the variables in the given scope.

I have the Arduino Genuino/UNO board selected under ‘Tools’

What the code is supposed to do: Basically I’m trying to build a really simple guitar hero game, where I have 3 buttons that act as the inputs, and vibration motors mounted to the buttons act as outputs to signify when the player is supposed to hit the buttons.

How I’m doing this is by writing an array of size 1000x 3 (because there are 3 buttons) where each subsequent row of the array corresponds to a time value (25 ms, say) to emulate when each button should be pressed. Each loop runs for 25 ms and I’ll be using millis() to keep track of total time.

Based on these values, the vibration motors will activate and if all the buttons’ values line up with the array values, its a success , else failure.

Here’s my code:

void setup() {
// First define all the inputs and outputs
  pinMode(2,INPUT); // Pins 2,3,4 are the inputs where the buttons will be connected
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(11,OUTPUT); // Pins 11,12,13 are the outputs where the vibration motors will be connected (and mounted to the vibrators)
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);

  
  boolean Button1flag= LOW; // these are flags... I don't know why these are here
  boolean Button2flag= LOW;
  boolean Button3flag= LOW;

  int Buttonstate1 = digitalRead(2); // these are the button states, read from the arduino
  int Buttonstate2 = digitalRead(3);
  int Buttonstate3 = digitalRead(4);

  int A=0;
  int B=0;
  int C=0;
  
  int i=0; // i will drive the array row change
  
  
  /* this is the array tha would correspond to the song, a poor substitute for a MIDI file but simple. every row is a 25 ms block of the song.
  This is a test , ideally the array would be stored outside the code so I can call different 'songs'*/
   
int array[10][3]={ 
  {0,0,1},
  {0,0,1},
  {0,0,1},
  {0,1,1},
  {0,1,1},
  {0,1,1},
  {0,1,1},
  {0,1,1},
  {0,0,1},
  {0,0,1},
  };

  
  long Delay= 25; //this is the delay, and the duration for which each loop will run
  long t = millis()+ Delay; // millis wil keep track of the time
  

  Serial.begin(9600); // start the serial code at 9600 bpa
}

void loop() {
  // put your main code here, to run repeatedly:
while (millis()<=t) // run the loop while the current value of millis is less than the sum of the previous time and the delay value
{
  if (Buttonstate1==A) //first condition: is button1 equal to the corresponding array value?
      {if (Buttonstate2==B) // second condition
          {if  (Buttonstate3==C) // basically if all three conditions are not met, the output is false
              {
                println("1"); // send a signal to processing to increase the volume of the song to signify success
                
                }
              }
    
  
  }
  else {
        println("0"); // this will tell processing to reduce the volume of the song, signifying failure.
        }
  }

}

i++;
A=(array(i,1));
B=(array(i,2));
C=(array(i,3));

and here are the errors:

body_hero.ino: In function 'void loop()':
body_hero:54: error: 't' was not declared in this scope
body_hero:56: error: 'Buttonstate1' was not declared in this scope
body_hero:56: error: 'A' was not declared in this scope
body_hero:57: error: 'Buttonstate2' was not declared in this scope
body_hero:57: error: 'B' was not declared in this scope
body_hero:58: error: 'Buttonstate3' was not declared in this scope
body_hero:58: error: 'C' was not declared in this scope
body_hero:60: error: 'println' was not declared in this scope
body_hero:68: error: 'println' was not declared in this scope
body_hero.ino: At global scope:
body_hero:74: error: 'i' does not name a type
body_hero:75: error: 'A' does not name a type
body_hero:76: error: 'B' does not name a type
body_hero:77: error: 'C' does not name a type
't' was not declared in this scope

Thank you in advance for any help you can offer

A=(array(i,1));
B=(array(i,2));
C=(array(i,3));

or did you mean:

A = array[i][1];

'A' was not declared in this scopeThe clue to the problem is right there in the error message.

A is declared in the setup() function so is not available outside of that function.

BulldogLowell: A=(array(i,1)); B=(array(i,2)); C=(array(i,3));

or did you mean:

A = array[i][1];

Yeah, I'm sorry I'm fairly new to programming arrays I'll change that

UKHeliBob: 'A' was not declared in this scopeThe clue to the problem is right there in the error message.

A is declared in the setup() function so is not available outside of that function.

Oh my god I'm such an idiot. thank you so much, I've moved the declarations to before void setup(), though I'm still getting these errors:

body_hero:72: error: 'i' does not name a type
body_hero:73: error: 'A' does not name a type
body_hero:74: error: 'B' does not name a type
body_hero:75: error: 'C' does not name a type
'i' does not name a type

Post the code that goes with those errors.

Here’s the updated code:

  boolean Button1flag= LOW; // these are flags... I don't know why these are here
  boolean Button2flag= LOW;
  boolean Button3flag= LOW;

  int Buttonstate1 = digitalRead(2); // these are the button states, read from the arduino
  int Buttonstate2 = digitalRead(3);
  int Buttonstate3 = digitalRead(4);

  int A=0;
  int B=0;
  int C=0;
  
  int i=0; // i will drive the array row change
  
  
  /* this is the array that would correspond to the song, a poor substitute for a MIDI file but simple. every row is a 25 ms block of the song.
  This is a test , ideally the array would be stored outside the code so I can call different 'songs'*/
   
int array[10][3]={ 
  {0,0,1},
  {0,0,1},
  {0,0,1},
  {0,1,1},
  {0,1,1},
  {0,1,1},
  {0,1,1},
  {0,0,1},
  {0,0,1},
  {0,0,1},
  };

  
  long Delay= 25; //this is the delay, and the duration for which each loop will run
  long t = millis()+ Delay; // millis wil keep track of the time
  
void setup() {

// First define all the inputs and outputs
  pinMode(2,INPUT); // Pins 2,3,4 are the inputs where the buttons will be connected
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(11,OUTPUT); // Pins 11,12,13 are the outputs where the vibration motors will be connected (and mounted to the vibrators)
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600); // start the serial code at 9600 bpa
}

void loop() {
  
  // put your main code here, to run repeatedly:
while (millis()<=t) // run the loop while the current value of millis is less than the sum of the previous time and the delay value
{
  if (Buttonstate1==A) //first condition: is button1 equal to the corresponding array value?
      {if (Buttonstate2==B) // second condition
          {if  (Buttonstate3==C) // basically if all three conditions are not met, the output is false
              {
                Serial.print("1"); // send a signal to processing to increase the volume of the song to signify success
                
                }
              }
    
  
  }
  else {
        Serial.print("0"); // this will tell processing to reduce the volume of the song, signifying failure.
        }
  }

}

 i++;
A= array[i][1];
 B= array[i][2];
 C= array[i][3];

and the errors:

body_hero:72: error: 'i' does not name a type
body_hero:73: error: 'A' does not name a type
body_hero:74: error: 'B' does not name a type
body_hero:75: error: 'C' does not name a type
'i' does not name a type
 i++;
 A= array[i][1];
 B= array[i][2];
 C= array[i][3];

why is all of this outside a function? where does it go?

BulldogLowell: i++; A= array[i][1]; B= array[i][2]; C= array[i][3];

why is all of this outside a function? where does it go?

I put these outside the while loop because I didn't want it to refresh inside the while loop, basically I wanted these values to update once every time the while loop completes.

So I shifted this bit:

i++;
A= array*[1];*
B= array*[2];*
C= array*[3];*
to be inside the brackets (another stupid mistake)
the code now compiles and looks like this:
```
*  boolean Button1flag= LOW; // these are flags… I don’t know why these are here
  boolean Button2flag= LOW;
  boolean Button3flag= LOW;

int Buttonstate1 = digitalRead(2); // these are the button states, read from the arduino
  int Buttonstate2 = digitalRead(3);
  int Buttonstate3 = digitalRead(4);

int A=0;
  int B=0;
  int C=0;
 
  int i=0; // i will drive the array row change
 
 
  /* this is the array that would correspond to the song, a poor substitute for a MIDI file but simple. every row is a 25 ms block of the song.
  This is a test , ideally the array would be stored outside the code so I can call different ‘songs’*/
 
int array[10][3]={
  {0,0,1},
  {0,0,1},
  {0,0,1},
  {0,1,1},
  {0,1,1},
  {0,1,1},
  {0,1,1},
  {0,0,1},
  {0,0,1},
  {0,0,1},
  };

long Delay= 25; //this is the delay, and the duration for which each loop will run
  long t = millis()+ Delay; // millis wil keep track of the time
 
void setup() {

// First define all the inputs and outputs
  pinMode(2,INPUT); // Pins 2,3,4 are the inputs where the buttons will be connected
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(11,OUTPUT); // Pins 11,12,13 are the outputs where the vibration motors will be connected (and mounted to the vibrators)
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600); // start the serial code at 9600 bpa
}

void loop() {
 
  // put your main code here, to run repeatedly:
while (millis()<=t) // run the loop while the current value of millis is less than the sum of the previous time and the delay value
{
  if (Buttonstate1==A) //first condition: is button1 equal to the corresponding array value?
      {if (Buttonstate2==B) // second condition
          {if  (Buttonstate3==C) // basically if all three conditions are not met, the output is false
              {
                Serial.print(“1”); // send a signal to processing to increase the volume of the song to signify success
               
                }
              }
   
 
  }
  else {
        Serial.print(“0”); // this will tell processing to reduce the volume of the song, signifying failure.
        }
  }
i++;
A= array[i][1];
B= array[i][2];
C= array[i][3];
}

_```*
thank you so much for helping !!_

snarejunkie

Could you take a break from your focus on your code for a moment and describe what is is you're trying to accomplish.

You have 3 buttons and 3 actuators you refer to as vibrators at this point.

You mention MIDI in a comment and say something about the actuators producing a tone.

It seems you might be trying to do some to the actuators based on states of the 3 buttons.

That's all I can determine from the code.

I got this problem when I wrote an extra curly bracket in the loop function. If anyone’s having the same problem, check if you got an extra curly bracket.