Error message "count not included in scope"

I get the subject error message when I run this program:

void setup() {
// put your setup code here, to run once:
const int xstep = 2; const int xdir = 5;
const int ystep = 3; const int ydir = 6;
const int zstep = 4; const int zdir = 7;
const int enablexyz = 8;
pinMode(xstep, OUTPUT); pinMode(xdir, OUTPUT);
pinMode(ystep,OUTPUT); pinMode(ydir, OUTPUT);
pinMode(zstep,OUTPUT); pinMode(zdir, OUTPUT);
digitalWrite(xstep,LOW); digitalWrite(xdir,LOW);
digitalWrite(ystep,LOW); digitalWrite(ydir,LOW);
digitalWrite(zstep,LOW); digitalWrite(zdir,LOW);
int count = 0;
// put your main code here, to run repeatedly:
void loop() {
if (count < 120);{
digitalWrite(4,HIGH); delay(1);
digitalWrite(4,LOW); delay(1);
count = count + 1;

I am using this program to “exercise” a TB6560 3-axis controller one axis at a time.


The problem is that count is defined in setup() but you try to use it in loop(). The problem comes from the C concept of scope. Search this site for posts about scope. Move the definition of count either into loop() or make it a global (i.e., defined just before setup()).

Also, please read the two posts by Nick Gammon at the top of this page. It has guidelines for posting here, especially on the use of code tags when posting source code. Also, before you post your code, while in the Arduino IDE, place the cursor in your code window and press Ctrl-T. It will reformat your code into a more common C style. Doing this before posting makes it easier to read your code.

You are using the variable count in loop() but you have defined it within setup(). You should put the line

int count = 0;

before setup() so that count becomes a global variable rather than a local variable.

Or you could define it as a static local variable within loop(). Static means that it remembers its value between calls to loop().

static int count = 0;

And please use the code button </>

so your code looks like this

and is easy to copy to a text editor


Robin's idea of defining count within loop() using the static type qualifier is a good way to go. Doing so encapsulates (i.e., hides) the variable from other parts of the program and is much better than giving it global scope.