Does not name a type using Random Number in another tab

I am VERY new to programming with the Arduino Uno R3 but have use some older controllers so please go easy on me. Trying to get back into this after about 10 years.

Very simple 3 line sketch using a tab. I keep getting the error "randomNumber does not name a type" during compiling. If I put all three lines in the main program it compiles. What am I doing wrong?

Thanks in advance
David

Main Program:
long randomNumber;

void setup() {
// put your setup code here, to run once:
randomSeed(analogRead(0));
}

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

}

In a tab called Pick I have just this single line
randomNumber = random(300);

The code in the tab is included in the build, so it is outside of a function! you need to put this inside a function.

See all the tabs (they are *.ino files in your project) as one long file.
The code on top is the file with the project name, that is in the most left tab.

You can not put a code line outside setup() and loop(). That means you can not put a code line in a tab.

void setup()
{
  ...
}

void loop()
{
  ...
}

randomNumber = random(300);

You can put functions in tabs.

The main sketch

long randomNumber;

void setup()
{
  Serial.begin(115200);
  randomSeed(analogRead(0));
  pick();
  Serial.println(randomNumber);
}

void loop()
{
}

In the tab

void pick()
{
  randomNumber = random(300);
}
1 Like

Thanks for all the replies. Don't know where my head was at!!!! Getting old is no fun. Thanks again for your help

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