How to make random number work in loop?

Im new to this so decided as a starting project i would make a program that converts a number up to 4095(i have 12 led's) into binary and shows it on the led's. I got it all working and wanted it to make a random number every 5 seconds after the last one, but i cant use randomseed outside of loop or setup, and if i use it in loop it changes the
number before its done converting, but if i put it in setup its not in the scope of the loop. Here is the code without me attempting to randomize it:

int number = 15;
int digitNbr = 12;
void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  if (number - pow(2, digitNbr) >= 0 && (number > 0)) {   // this check if a number is greater that 2^digitnbr,
    digitalWrite(digitNbr + 2, HIGH);                  // and if it is, it takes it away and lowers digitnbr by 
    number = number - pow(2, digitNbr);  // one, and loops. if not, it takes one away and checks again, until
    digitNbr -= 1;   // number = 0, then it stops. (the digitalwrite is just for the led's, ignore it
  }
  else if (number > 0) {
    digitNbr = digitNbr - 1;
  }
}

and here it is with my randomizing attempts

int number = 15;
int digitNbr = 12;
randomSeed(analogRead(A0));
void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  if (number - pow(2, digitNbr) >= 0 && (number > 0)) {
    digitalWrite(digitNbr + 2, HIGH);
    number = number - pow(2, digitNbr);
    digitNbr -= 1;
  }
  else if (number > 0) {
    digitNbr = digitNbr - 1;
  }
  else if (number == 0) {
    delay(5000);
    int number = random(4095);
    digitNbr = 12;  // when the number reaches 0, this is supposed to wait 5 sec, reset it to a random number and 
                           // set digitnbr back to 12, but the problem is that it doesnt change it on the scope i want(i think)

  }

i keep getting this: " error: expected constructor, destructor, or type conversion before '(' token
randomSeed(analogRead(A0));"
^
also, when i change the number in that else if at the end manually, the main converter doesnt start up again.
Thanks

you need to call analogRead () within a function – setup ()

randomSeed() is a function. Outside of a function, you can only put declarations. Move the call to randomSeed() to inside setup().

WARNING: The ‘pow()’ function returns a floating-point value (float) which often produces incorrect results when truncated to an integer. Bitwise shift operators (<< and >>) are recommended for bit manipulation.

It is usually easier to read and safer to use an array of pin numbers rather than requiring all the pin numbers to be consecutive and doing math on pin numbers.

const int BitCount = 12;
const byte OutputPins[BitCount] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};


void setup()
{
  Serial.begin(115200);  // 9600 baud is so 1980's


  randomSeed(analogRead(A0));  // Initialize the RNG with a floating analog input


  for (int i = 0; i < BitCount; i++)
    pinMode(OutputPins[i], OUTPUT);
}


void loop()
{
  unsigned number = random(4096); // 0..4095


  // Display 12 bits of binary
  for (int i = 0; i < BitCount; i++)
  {
    digitalWrite(OutputPins[i], (number >> i) & 1);
  }


  delay(5000);
}

johnwasser:
It is usually easier to read and safer to use an array of pin numbers rather than requiring all the pin numbers to be consecutive and doing math on pin numbers.

const int BitCount = 12;

const byte OutputPins[BitCount] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

void setup()
{
 Serial.begin(115200);  // 9600 baud is so 1980’s

randomSeed(analogRead(A0));  // Initialize the RNG with a floating analog input

for (int i = 0; i < BitCount; i++)
   pinMode(OutputPins[i], OUTPUT);
}

void loop()
{
 unsigned number = random(4096); // 0…4095

// Display 12 bits of binary
 for (int i = 0; i < BitCount; i++)
 {
   digitalWrite(OutputPins[i], (number >> i) & 1);
 }

delay(5000);
}

thanks, makes sense now. (most of it)

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