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); 
}
}

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.

Edited 2 Years Ago by Suzie999

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.

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.

Edited 2 Years Ago by richieking

This article has been dead for over six months. Start a new discussion instead.