Hi
I'm a noob. Can anyone please explain why I get a "'ranum' was not declared in this scope" error in this script? I was obviously using Arduino.

void setup()
{
   pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(09, OUTPUT);
 double ranum = rand()%11;

}
void loop()
{
 if (ranum==0){
  digitalWrite(13, LOW);   // Turn on the LED
  digitalWrite(12, LOW); 
  digitalWrite(10, LOW); 
  digitalWrite(09, LOW); 
}
else if (ranum==1){
  digitalWrite(13, HIGH);   // Turn on the LED
  digitalWrite(12, LOW); 
  digitalWrite(10, LOW); 
  digitalWrite(09, LOW); 
}
else {
  digitalWrite(13, LOW);   // Turn on the LED
  digitalWrite(12, HIGH); 
  digitalWrite(10, LOW); 
  digitalWrite(09, LOW); 
}
}

Recommended Answers

All 4 Replies

I expect the error comes first from line #12.

There is no ranum variable declared or accessible within the local scope of your loop() function.

Inside a function, the following variables exist:

1) Parameters you passed in

2) Any variables you create inside that function.

3) Any global variables that exist everywhere.

ranum is none of these, so it does not exist inside the function loop.

You just need to move line 7 to between line 11 and 12. That will fix it.

There are 2 things to do according to what you wanna achieve.

1.Bring double ranum = rand()%11; line 7 on the first line in the void loop function.
or2. make double ranum = rand()%11; a global declaration as you have been advised.

Also i just wanna know how your programme is organized.. that is in a class or just procedual function method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.