Pages: [1]   Go Down
Author Topic: Random  (Read 701 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I try to turn on max 5 leds by pressing 2 buttons.
For this I used this script below.
But, after reset, the most time the same led is beaming.

Is there any improvement on it?

Gr.
Johan

Code:
int led[] = {3,4,5,6,7,8};
int numberLeds = 6;
int button1 = 9;
int button2 = 10;
 
void setup() {               
  for (i=0;i<5;i++){
    pinMode(led[i],OUTPUT);
    digitalWrite(led[i],LOW);
  }
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
}
 
void loop() {
  randomSeed(analogRead(0));
  if (digitalRead(button1)&&digitalRead(button2)){
    if(random(100)>50){
      digitalWrite(led[random(numberLeds)],HIGH);
    }
    if(random(100)>50){
      digitalWrite(led[random(numberLeds)],HIGH);
    }
    if(random(100)>50){
      digitalWrite(led[random(numberLeds)],HIGH);
    }
    if(random(100)>50){
      digitalWrite(led[random(numberLeds)],HIGH);
    }
    if(random(100)>50){
      digitalWrite(led[random(numberLeds)],HIGH);
    }
    while(true){
      delay(1000);
    }
  }
}
Logged

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

You random seed should be 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.

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


Why analogRead is a bad choice...
http://arduino.cc/forum/index.php/topic,66206.msg630884.html#msg630884

The reliable but not very sexy way to seed random...
http://arduino.cc/forum/index.php/topic,66206.msg537783.html#msg537783

And, of course, heed AWOL's advice.
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As there's manual input ( button push ), you could use millis() of the first button press as a random seed, too.
Or simply call random(); in the beginning of loop() where now is your randomSeed(), this will be repeated an undefinite number of times, until both buttons are pressed.

BTW: Your code does not generate a sequence at all, as it never ends the loop().
Strange design... you need to press 3 buttons: Reset, then the 2 others together.

Edit: sent too early
« Last Edit: June 13, 2012, 04:54:59 am by michael_x » Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks all.
It's working for the purpose we want.
And yes, it's a strange design, but we have no time to make it better.
(pupils always start to late with their projects...)

Gr.
Johan
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    while(true){
      delay(1000);
    }
That looks plain stupid. If you want the Arduino to stop doing anything useful,
Code:
    while(true)
    {
    }
It makes no difference whether it executes the while loop several million times per second, or wastes time in the delay() function.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Personally I use
Code:
while(1);
It is short, simple and easy to type.  The danger is it could it lost in the code and cause it to "Hang".
Logged

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

Code:
exit( 1 );

...is a good choice.  Stops execution.  Easy to spot.  No ambiguity regarding its purpose.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga137096a48cc0c731052cadfb69c39b34
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
exit( 1 );

...is a good choice.  Stops execution.  Easy to spot.  No ambiguity regarding its purpose.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga137096a48cc0c731052cadfb69c39b34

Would this still allow you to continue to service interrupts?
I assume the Watch Dog would still restart, even after an exit.
Logged

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

Would this still allow you to continue to service interrupts?

No.  exit disables interrupts.

Quote
I assume the Watch Dog would still restart, even after an exit.

If the watchdog is configured to generate an interrupt the interrupt is ignored.  If the watchdog is configured to reset the processor then, when it expires, the processor is reset.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No.  exit disables interrupts.

Yes, that's a trap. Compare:

Code:
Serial.println ("Goodbye!");
while (true) ;

to:

Code:
Serial.println ("Goodbye!");
exit (1);

One prints "Goodbye!", one doesn't.
Logged

Pages: [1]   Go Up
Jump to: