Pages: [1]   Go Down
Author Topic: Random  (Read 422 times)
0 Members and 1 Guest are viewing this topic.
Iowa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Sorry I am very new at this. I was playing around with making a 7 segment led number light up. Then i thought it would be neat to make it display a random number. I am using random() but every time i use it i end up with 4. How do i get a real random number?

here is the code if anyone wish to see it:
Code:
//
int topBar = 12;
int middleBar = 11;
int bottomBar = 10;
//
int rightTBar = 9;
int rightBBar = 8;
//
int leftTBar = 7;
int leftBBar = 6;
//
int randNumber = 0;
//
int doOnce = 0;


void setup()                  
{
  pinMode(topBar, OUTPUT);
  pinMode(middleBar, OUTPUT);
  pinMode(bottomBar, OUTPUT);    
  pinMode(rightTBar, OUTPUT);  
  pinMode(rightBBar, OUTPUT);    
  pinMode(leftTBar, OUTPUT);    
  pinMode(leftBBar, OUTPUT);    
}

void setCharacter(int whichNum)
{
  if (whichNum == 0){
    // 0
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, HIGH);
    digitalWrite(bottomBar, LOW);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, LOW);
    digitalWrite(leftBBar, LOW);
  }
  else if (whichNum == 1){
    // 1
    digitalWrite(topBar, HIGH);
    digitalWrite(middleBar, HIGH);
    digitalWrite(bottomBar, HIGH);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, HIGH);
    digitalWrite(leftBBar, HIGH);
  }
  else if (whichNum == 2){
    // 2
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, LOW);
    digitalWrite(bottomBar, LOW);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, HIGH);  
    digitalWrite(leftTBar, HIGH);
    digitalWrite(leftBBar, LOW);
  }
  else if (whichNum == 3){
    // 3
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, LOW);
    digitalWrite(bottomBar, LOW);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, HIGH);
    digitalWrite(leftBBar, HIGH);
  }
  else if (whichNum == 4){
    // 4
    digitalWrite(topBar, HIGH);
    digitalWrite(middleBar, LOW);
    digitalWrite(bottomBar, HIGH);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, LOW);
    digitalWrite(leftBBar, HIGH);
  }
  else if (whichNum == 5){
    // 5
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, LOW);
    digitalWrite(bottomBar, LOW);
    digitalWrite(rightTBar, HIGH);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, LOW);
    digitalWrite(leftBBar, HIGH);
  }
  else if (whichNum == 6){
    // 6
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, LOW);
    digitalWrite(bottomBar, LOW);
    digitalWrite(rightTBar, HIGH);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, LOW);
    digitalWrite(leftBBar, LOW);
  }
  else if (whichNum == 7){
    // 7
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, HIGH);
    digitalWrite(bottomBar, HIGH);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, HIGH);
    digitalWrite(leftBBar, HIGH);
  }
  else if (whichNum == 8){
    // 8
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, LOW);
    digitalWrite(bottomBar, LOW);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, LOW);
    digitalWrite(leftBBar, LOW);
  }
  else if (whichNum == 9){
    // 9
    digitalWrite(topBar, LOW);
    digitalWrite(middleBar, LOW);
    digitalWrite(bottomBar, LOW);
    digitalWrite(rightTBar, LOW);
    digitalWrite(rightBBar, LOW);  
    digitalWrite(leftTBar, LOW);
    digitalWrite(leftBBar, HIGH);
  }
  delay(50);
}

void loop()
{
  if(doOnce == 0){
    for (int i=0; i <= 30; i++){
       randNumber = random(9);
      setCharacter(randNumber);
   }
  doOnce = 1;
  }
}
Logged

Iowa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a picture of it. The led is an old one i had picked up when i was a kid - it is a common anode one. I was hoping to get the number to stop on a random one every time i pressed the reset button, but it just likes 4 right now.

Logged

SF Bay Area (USA)
Online Online
Tesla Member
***
Karma: 106
Posts: 6381
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

random() will return the same sequence of pseudo-random numbers unless you "seed" it with a truly
random number.  See the reference page on random()

Getting truly random numbers is actually pretty tough.   For dice-like applications, the usual solution is to use the extern human to help by using something like the number of milliseconds between the time the micro was reset and the time that the user hit a button, or the amount of time that the button is held down (in microseconds :-)) like:

Code:
void loop(void) {
  if (digitalRead(button) == 1) {
    myrandom = myrandom + 1;  /* count every loop while button is pressed */
  } else {
    if (myrandom > 0) {  /* Do we have a new random number */
      seg7display(myrandom % 9);
      myrandom = 0;   /* indicate number has been displayed
    }
  }
}
Logged

Brisbane, Australia
Offline Offline
God Member
*****
Karma: 1
Posts: 593
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Another way to make a seed is to try and wire up a circuit which gives a random output.

Using a analog input and something like temperature comes to mind.
A clock would also work well.
Logged

Iowa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Thank you both for your replies. I went back and re-read the reference to random() and randomSeed() - made more sense this time.

I did as they suggest and did an analogRead on an empty port and the "noise" in that port was used as the seed. This is sort of like Cheaters solution - using a random output.

I also put my own switch in the circuit and tried it how westfw suggested - let it run while the button is down and then stop it on button release. That worked well too.

I guess i was just used to how flash works and i saw random() and ran with it.

Thanks again!
Logged

Pages: [1]   Go Up
Jump to: