Go Down

Topic: Help with a random number generating 7 seg display problem! (Read 807 times) previous topic - next topic

green_dog252

This is probably a very simple fix, and I'll just put the code up:
Code: [Select]
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!

LarryD

randomSeed(analogRead(0));
random(min, max);
See:
http://arduino.cc/en/Reference/Random
http://arduino.cc/en/Reference/RandomSeed
The way you have it in your schematic isn't the same as how you have it wired up!

Coding Badly


Coding Badly

Code: [Select]
  randomSeed(analogRead(A1));

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

Coding Badly


LarryD

This reads a floating Analog i/p.
The i/p voltage will fluctuate randomly.
The way you have it in your schematic isn't the same as how you have it wired up!

green_dog252


Code: [Select]
  if (num = 0){

Assignment versus comparison.


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


Code: [Select]
  pinMode(A0, INPUT);

Why?


That's the input of the potentiometer. Necessary?

Coding Badly


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).

Coding Badly

This reads a floating Analog i/p.


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

Quote
The i/p voltage will fluctuate randomly.


Wanna bet?

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview