Pages: [1]   Go Down
Author Topic: [Solved] Random function without read the Analog 0  (Read 475 times)
0 Members and 1 Guest are viewing this topic.
Turin
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Start with Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to generate a random value between 1 and x, but my project already use all the analog pin so I can't use this page:

http://arduino.cc/en/Reference/random

What can I do? I was thinking to implement a software LFSR.
« Last Edit: May 09, 2012, 12:22:44 pm by stephanboy2030 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you saying the analogue pins are in use as digital pins, and that's the reason you can't use them?
Normally, you only ever call the seed function once, so this could be done in setup.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Turin
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Start with Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use them to check whenever a user press a push button.

I defined them in this way
Code:
#define Power   A0
#define Select  A1
#define Up A2
#define Down A3   
#define Left A4
#define Right A5

and in setup()

Code:
  //Setup the Joystick buttons as inputs
    pinMode(Power, INPUT);
    pinMode(Up, INPUT);
    pinMode(Down, INPUT);
    pinMode(Left, INPUT);
    pinMode(Right, INPUT);
    pinMode(Select, INPUT);

  //Enable pullups on Joystick buttons
    digitalWrite(Power, HIGH);
    digitalWrite(Up, HIGH);
    digitalWrite(Down, HIGH);
    digitalWrite(Left, HIGH);
    digitalWrite(Right, HIGH);
    digitalWrite(Select, HIGH);

and using 6 push buttons between GND and each analog input.
My project is a MP3 player
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, use them as the random seed before you use them as digital pins.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Turin
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Start with Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was confusing how the random function works. It just need to take a first value only one time, not every time that it is called, isn't it?

Thank you so much AWOL
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4678
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The random function is a pseudo random number generator, a complex mathematical function returning randomly spread numbers over a range. Because every new number is calculated based on the previous it has to be initialized at some point (called seeding). For the seeding it's best to use some truly random value as it is the noise on a not connected analog input.
Logged

Turin
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Start with Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks pylon, now it's definitely clear.
Logged

Pages: [1]   Go Up
Jump to: