Not declared in this scope errors

Hi there!
I keep getting these "not declared in this scope" errors, even though I am declaring these in the setup function, (which I thought made them global). But I've included them in the setup function or brfore the setup fonction and I get the same errors regardless It's like the compiler can't see them What am I doing wrong?
Thanks!

ERRORS:

C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino: In function 'void loop()':
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:34:7: error: 'Zbutton' was not declared in this scope
   if (Zbutton == 1)     // if z button is pressed 'Enter joystick CALIBRATION MODE'
       ^~~~~~~
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:36:5: error: 'calmode' was not declared in this scope
     calmode = 1;     // calibration mode on
     ^~~~~~~
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:36:5: note: suggested alternative: 'calloc'
     calmode = 1;     // calibration mode on
     ^~~~~~~
     calloc
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:39:7: error: 'calmode' was not declared in this scope
       calmode = 0;     // calibration mode off
       ^~~~~~~
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:39:7: note: suggested alternative: 'calloc'
       calmode = 0;     // calibration mode off
       ^~~~~~~
       calloc
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:41:6: error: 'Cbutton' was not declared in this scope
      Cbutton = 0;   // reset c button state to NOT PRESSED
      ^~~~~~~
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino: At global scope:
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:46:9: error: expected constructor, destructor, or type conversion before '(' token
   While (calmode == 1)   // while inCALIBRATION MODE. read the nunchuk and move the servo with the Y joystick position
         ^
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:55:9: error: expected constructor, destructor, or type conversion before '(' token
   While (calmode == 0)   // while CALIBRATION MODE is OFF
         ^
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:65:2: error: 'Zbutton' does not name a type
  Zbutton = 0;   // reset Z BUTTON state to "NOT PRESSED'
  ^~~~~~~
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:66:2: error: 'Cbutton' does not name a type
  Cbutton = 0;   // reset C BUTTON state to 'NOT PRESSED'
  ^~~~~~~
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:68:1: error: 'daPayload' does not name a type
 daPayload=nunchuck_get_data(); // get more nunchuk data
 ^~~~~~~~~
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:69:22: error: expected constructor, destructor, or type conversion before ';' token
  nunchuk_print_data();  // go print the nunchuk data to the serial monitor for debugging ourposes
                      ^
C:\Users\Japper\Documents\Arduino\RobotGlove\RobotGlove\RobotGlove.ino:70:5: error: expected declaration before '}' token
     }
     ^
Multiple libraries were found for "Servo.h"
  Used: C:\Users\Japper\Documents\Arduino\libraries\Servo
  Not used: C:\Users\Japper\AppData\Local\Arduino15\libraries\Servo
exit status 1

Compilation error: 'Zbutton' was not declared in this scope

CODE:

```

#include <Servo.h>
#include <Wire.h>
///////////////////////////////
// ----RobotGlove.ino ====   //
//////////////////////////////

Servo servo;                     // Define servo

void setup() {
  int nunchuk_buf[6];  	    // array to storenunchuk data,
  int MaxServ = 0;        	// maximum servo pos
  int MinServ = 0;         	// minimum  servo pos
  int ServPos = 0;         	// actual servo pos
  int calmode = 0;         	// 1 =RUN mode, 0 = CALIBRATE mode
  int Zbutton = 0;		      // nunchuk Z BUTTON, 1 = pressed, 0= not pressed
  int Cbutton = 0;		      // nunchuk C BUTTON, 1 = pressed, 0= not pressed
  int  daPayload;		        // data received from nunchuk, 0= failed , 2= succedded
  Wire.begin();
  servo.attach(11); 		    // Set servo to digital pin 11

} 
 //------------------------------- END OF SETUP --------------------------------


void loop() 
{
  Wire.beginTransmission(0x52);  	// transmit to nunchuk device 0x52
  Wire.write(0x40);              		// send the nunchuk memory address
  Wire.write(0x00);              		// send  the nunchuk  a zero to request data.
  Wire.endTransmission();        		// stop transmitting
  Serial.begin(19200);		// Set up serial port fot thr serial monitor for debugging purposes

  if (Zbutton == 1)  			// if z button is pressed 'Enter joystick CALIBRATION MODE'
  {
    calmode = 1;  			// calibration mode on
  }
    else {
      calmode = 0;  			// calibration mode off
     Zbutton = 0;			// reset z button state to NOT PRESSED
     Cbutton = 0;			// reset c button state to NOT PRESSED

    }
  }

  While (calmode == 1) 		// while inCALIBRATION MODE. read the nunchuk and move the servo with the Y joystick position
{
     daPayload=nunchuck_get_data();	// go get the nunchuk data
     nunchuk_print_data();		// go print the nunchuk data to the serial monitor for debugging ourposes 
      delay(500);			// allow for settling time
    if (Cbutton == 1) 			 // if c button is pressed 'Exit joystick CALIBRATION MODE', and enter RUN MODE  where the servo is continually  		 {				// cycled between the  maximum to minimum automatically based on CALIBEATION MODE measured limits  
  
      calmode = 0;  			    
  }
  While (calmode == 0) 		// while CALIBRATION MODE is OFF
{
   daPayload=nunchuck_get_data();	// get more data from the nunchuk
   nunchuk_print_data();		// set to print the newnunchuk  data to the serial monitor for debugging purposes
    delay(500);			// wait for settling time
    if (Zbutton == 1)  			// if z button is pressed 'Enter joystick CALIBRATION MODE again'
    {
      calmode = 1; 			// CALIBRATION MODE is ON
    }
  }
 Zbutton = 0;			// reset Z BUTTON state to "NOT PRESSED'
 Cbutton = 0;			// reset C BUTTON state to 'NOT PRESSED'
 
daPayload=nunchuck_get_data();	// get more nunchuk data 
 nunchuk_print_data();		// go print the nunchuk data to the serial monitor for debugging ourposes
    }
  delay(500);			// allow for settling time


}  // ======================= END OF LOOP=============
```

Help us help you.

That's exactly the problem.
Move them out of setup, like every example you've seen

Declaring a variable in any function makes it local to that function. Global variables need to be declared outside of any function, usually near the start of a sketch before they are used

Please read the forum posting guidelines.

Post your code.

It's in the original post

Post#1does not contain the code that caused the error.

Wuh?

It is now.

As others have mentioned... variables should be declared BEFORE the setup() routine.

Ah I see. I scrolled down. Typically people don't combine code and error message. Sorry.

One thing wrong


#include <Servo.h>
#include <Wire.h>
///////////////////////////////
// ----RobotGlove.ino ====   //
//////////////////////////////

Servo servo;                     // Define servo
  int nunchuk_buf[6];  	    // array to storenunchuk data,
  int MaxServ = 0;        	// maximum servo pos
  int MinServ = 0;         	// minimum  servo pos
  int ServPos = 0;         	// actual servo pos
  int calmode = 0;         	// 1 =RUN mode, 0 = CALIBRATE mode
  int Zbutton = 0;		      // nunchuk Z BUTTON, 1 = pressed, 0= not pressed
  int Cbutton = 0;		      // nunchuk C BUTTON, 1 = pressed, 0= not pressed
  int  daPayload;		        // data received from nunchuk, 0= failed , 2= succedded

void setup() {
  Wire.begin();
  servo.attach(11); 		    // Set servo to digital pin 11

} 
 //------------------------------- END OF SETUP --------------------------------


void loop() 
{
  Wire.beginTransmission(0x52);  	// transmit to nunchuk device 0x52
  Wire.write(0x40);              		// send the nunchuk memory address
  Wire.write(0x00);              		// send  the nunchuk  a zero to request data.
  Wire.endTransmission();        		// stop transmitting
  Serial.begin(19200);		// Set up serial port fot thr serial monitor for debugging purposes

  if (Zbutton == 1)  			// if z button is pressed 'Enter joystick CALIBRATION MODE'
  {
    calmode = 1;  			// calibration mode on
  }
    else {
      calmode = 0;  			// calibration mode off
     Zbutton = 0;			// reset z button state to NOT PRESSED
     Cbutton = 0;			// reset c button state to NOT PRESSED

    }
  }

  While (calmode == 1) 		// while inCALIBRATION MODE. read the nunchuk and move the servo with the Y joystick position
{
     daPayload=nunchuck_get_data();	// go get the nunchuk data
     nunchuk_print_data();		// go print the nunchuk data to the serial monitor for debugging ourposes 
      delay(500);			// allow for settling time
    if (Cbutton == 1) 			 // if c button is pressed 'Exit joystick CALIBRATION MODE', and enter RUN MODE  where the servo is continually  		 {				// cycled between the  maximum to minimum automatically based on CALIBEATION MODE measured limits  
  
      calmode = 0;  			    
  }
  While (calmode == 0) 		// while CALIBRATION MODE is OFF
{
   daPayload=nunchuck_get_data();	// get more data from the nunchuk
   nunchuk_print_data();		// set to print the newnunchuk  data to the serial monitor for debugging purposes
    delay(500);			// wait for settling time
    if (Zbutton == 1)  			// if z button is pressed 'Enter joystick CALIBRATION MODE again'
    {
      calmode = 1; 			// CALIBRATION MODE is ON
    }
  }
 Zbutton = 0;			// reset Z BUTTON state to "NOT PRESSED'
 Cbutton = 0;			// reset C BUTTON state to 'NOT PRESSED'
 
daPayload=nunchuck_get_data();	// get more nunchuk data 
 nunchuk_print_data();		// go print the nunchuk data to the serial monitor for debugging ourposes
    }
  delay(500);			// allow for settling time


}  // ======================= END OF LOOP=============

that I fixed.

It always was.
The post is unedited
Complete with misspelled "while"

Why the heck is serial begin in loop() when it should be in setup()?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.