[SOLVED] Variable "Not declared in this scope" after declaring in setup()

Edit: Thanks everyone for your help. I've done tons of arduino projects and I've never managed to screw this up. I can't believe I didn't know that stup() and loop() are different scopes!!!
Thanks again!!

Hi all, I'm having a strange problem that I can't figure out. I have some variables declared in setup() but when I try to use them in loop() I get the old "not declared in this scope" error.

My code so far is:

// Include required libraries
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

// OK at this point

void setup() {
  // put your setup code here, to run once:

  // Set up hall sensor pins
  int hallPin = 0;           // Pin to read for hall sensor output
  int hallState = 0; // Set current hall sensor state
  int lastHallState = 0;    //previous hall output state
  pinMode(hallPin, INPUT);        // initialize Hall Pin as an input

  // OK at this point

  // Begin LCD Screen
  lcd.begin(16, 2);             // LCD has 16 columns and 2 rows

  // Set up buttons
  const int button1Pin = 3;
  pinMode(button1Pin, INPUT);
  const int button2Pin  = 4;
  pinMode(button2Pin, INPUT);

  // OK at this point

  // Set up variables
  int jobLength = 1;            // Length of current job in feet
  int lobRemaining = 1;         // Remaining linear feet in the job
  int footageRolled = 1;        // For tracking the linear footage through the roll
  int footageIncrement = 1;     // Calculate based on roll diameter, magnet spacing
  int rollCount = 1;

  Wire.pins(4, 5);          // Sets data and clock pins for screen i2c

  Serial.begin(9600);       // Begin serial comminucation for diagnostics

}

void loop() {
  // put your main code here, to run repeatedly:

  // Display base menu
  lcd.print("1 for new job");
  lcd.setCursor(1, 0);
  lcd.print("2 for counter");

  // OK at this point

  // Read pins for user input
  int button1State = digitalRead(button1Pin);
  int button2State = digitalRead(button2Pin);
 // Problems now...

}

My error messages are:

C:\Users\vance.langer\Documents\Arduino\Test\Test.ino: In function 'void loop()':

Test:64: error: 'button1Pin' was not declared in this scope

   int button1State = digitalRead(button1Pin);

                                  ^

Test:65: error: 'button2Pin' was not declared in this scope

   int button2State = digitalRead(button2Pin);

                                  ^

exit status 1
'button1Pin' was not declared in this scope

Just what is going on here?

You declare variables in the scope of setup(), and you try to use them in the scope of loop(), so the compiler tells you that you haven't declared them to be used in the scope of loop() . . . .

What's the confusion?

Put your variable declarations before setup() by where all of your #define's are and they'll be global.

1 Like

Hi,
Those variable declarations need to go BEFORE Setup; that is global scope. This means they will be recognized in Setup and Loop.

3 Likes

You are soooo close.

Setup() has a different scope than Loop(). If you want them seen in both, you need to declare it global - outside of setup().

1 Like

Hi, I suggest you start with a simple template like this:

/* YourDuinoStarter Example: Sketch Template
 - WHAT IT DOES
 - SEE the comments after "//" on each line below
 - CONNECTIONS:
   - 
   - 
 - V1.00 09/11/12
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/


void setup()   /****** SETUP: RUNS ONCE ******/
{


}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/


//*********( THE END )***********

I use scope every night to prevent Gingivitis.

I was going to talk about programming scope but you young guys did it first :frowning: .

.

1 Like

hello guys!!!
please help me ,i cant find what type of error is this ??
Im interfacing Graphical LCD ST7920

Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Uno"

final128x64:27:1: error: 'U8GLIB_ST7920_128X64_4X' does not name a type

U8GLIB_ST7920_128X64_4X u8g(10);

^

H:\code\128x64 lcd code\final128x64\final128x64.ino: In function 'void loop()':

final128x64:258:3: error: 'u8g' was not declared in this scope

u8g.firstPage();

^

H:\code\128x64 lcd code\final128x64\final128x64.ino: In function 'void draw()':

final128x64:272:3: error: 'u8g' was not declared in this scope

u8g.setFont(u8g_font_8x13);

^

final128x64:272:15: error: 'u8g_font_8x13' was not declared in this scope

u8g.setFont(u8g_font_8x13);

^

exit status 1
'U8GLIB_ST7920_128X64_4X' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

ph sensor ![ph sensor
hi guys i got problems with yhis

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