Pages: [1]   Go Down
Author Topic: Help with a random number generating 7 seg display problem!  (Read 509 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is probably a very simple fix, and I'll just put the code up:
Code:
int potnum;
int duration;
long randNum;
void setup() {
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
 
  pinMode(A0, INPUT);
 
  Serial.begin(9600);
  randomSeed(analogRead(A1));
}

void loop() {
  randNum = random(10);
  seg(randNum);
}

void seg(long num){
  if (num = 0){
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  Serial.println(num);
  delay(pot());
  }
  if (num = 1){
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  Serial.println(num);
  delay(pot());
  }
  if (num = 2){
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  Serial.println(num);
  delay(pot());
  }
  if (num = 3){
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  Serial.println(num);
  delay(pot());
  }
  if (num = 4){
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  Serial.println(num);
  delay(pot());
  }
  if (num = 5){
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  Serial.println(num);
  delay(pot());
  }
  if (num = 6){
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  Serial.println(num);
  delay(pot());
  }
  if (num = 7){
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  Serial.println(num);
  delay(pot());
  }
  if (num = 8){
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  Serial.println(num);
  delay(pot());
  }
  if (num = 9){
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  Serial.println(num);
  delay(pot());
  }
}

int pot(){
  potnum = map(analogRead(A0), 0, 1024, 0, 500);
  return potnum;
}

 

If you go ahead and run this code for yourself, you'll see that it outputs 1, 2, 3...9 and then starts over. I want this to output random numbers on a seven segment display (I'm using an SN7446AN to achieve this).

The whole side of the program that outputs number on a display and the part that determines how quickly these numbers are displayed works fine, but the numbers aren't random! They just go on from 1-9 (it skips 0 for some reason).

So my questions are: Why does the code skip 0 and why aren't the numbers outputted random? I'm fairly new to making subroutines in Arduino, so I'm sure that's where my problem is.

please help! Thank you!
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2189
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

randomSeed(analogRead(0));
random(min, max);
See:
http://arduino.cc/en/Reference/Random
http://arduino.cc/en/Reference/RandomSeed
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12291
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 if (num = 0){

Assignment versus comparison.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12291
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  randomSeed(analogRead(A1));

In the worst case is pointless.  In the extremely rare best case produces 1024 unique sequences.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12291
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  pinMode(A0, INPUT);

Why?
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2189
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This reads a floating Analog i/p.
The i/p voltage will fluctuate randomly.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (num = 0){

Assignment versus comparison.

*facepalm* Sigh... i'm embarrassed to have had to bring this question up. Thank you.

Code:
  pinMode(A0, INPUT);

Why?

That's the input of the potentiometer. Necessary?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12291
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


You are welcome.

Necessary?

No.  You should only call pinMode on digital pins (on pins you will be accessing using digital functions like digitalRead and digitalWrite).
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12291
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This reads a floating Analog i/p.

I assume you are referring to this...
Code:
randomSeed(analogRead(A1));

Quote
The i/p voltage will fluctuate randomly.

Wanna bet?
Logged

Pages: [1]   Go Up
Jump to: