variable not being defined


Your variables are local to the if block and unknown outside it.

Variables can be global or local. But they can be even more local, for example inside a if-statement.

int x = 0;    // global variable

void setup()
  int a = 1;  // only known inside the setup() function (and deeper level)

  if( x == 0)
    int b = 100;  // only known inside the if-statement and deeper level
      if( b < 10)
        int c = x + a + b + c;  // variable 'c' is only known here
        Serial.println( c);

[EDIT] sterretje wrote the same, but will post this anyway.

The int in front of the variable name means you are creating a new variable. So you have 2 different versions of x1spr1 but each one's scope is only the if or else block that they are created in. So by the time you get round to using them they're out of scope.

You need to define them at a higher level. If they're only used in setup() then create them in there BEFORE anything else. Otherwise they should be global i.e. defined outside ANY function.


i am getting errors saying “x1spr1 is not defined inside the scope” or something like that

If you’re unsure, it’s best to use the IDE’s handy tool, and copy the exact error message you’re seeing.

It might also be a good idea to post full code. The mentioning of "undefined reference to 'loop'" is correct because you don't have a loop() function in your code.

Similar mylcd is nowhere to be found.

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