Pages: [1]   Go Down
Author Topic: random delay  (Read 1192 times)
0 Members and 1 Guest are viewing this topic.
Rochefort, France
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino is a king
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i would like to have randomized loop duration between 1 and 2 seconds. to do that i tried to use:

  r = random (1,2);
  
   delay (r*1000);

but it doesn't work.
anybody knows why?
Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i'm not in top for arduino...

but in C : rand(0,xx) generate an integer number betwen 0 and xx.

this should work.
Code:
int temps;

void setup() {
  beginSerial(115200);
}

void loop() {
  temps = rand(1,2)*1000;
  printInteger(temps);
  delay(temps);
}

i hope :-?

eric
« Last Edit: April 15, 2006, 04:59:45 pm by mrbbp » Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

well

it seems it's really really really :-? bad action to use PSEUDO random function smiley-razz

follow mellis solution here
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1143219447/2#2

the idea is to read an analogInPin and use a modulo
like this:
Code:
temps=((analogRead(0)%2)+1)*1000;
generate a number between 1 and 2 multiply by 10000

Code:
temps=(analogRead(0)%1000)+1000;
generate a number between 1000 and 2000.

regards

eric
« Last Edit: April 15, 2006, 05:11:26 pm by mrbbp » Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

after some test..

Code:
temps=(analogRead(1)%100)*10+1000;

have better random result (between min and max value) you want smiley-razz

eric smiley-grin
« Last Edit: April 15, 2006, 06:00:22 pm by mrbbp » Logged

Rochefort, France
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino is a king
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

eric,
thanks a lot it works fine.
do you have any idea of the range of the numbers generated by the unconnected analog pin?
something else: in one code you proposed you had
printInteger();
where do you get it printed? i get nothing.
francois
Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

PrintInteger() write integer if the "serial Monitor" is ON (litle rect with circle on top icon, on top of Arduino gui), in the balck area, in the bottom of arduino GUI...
this is somthing like a debug feature.

if you want to explore value just look the number with the "serial monitor".

Code:
printInteger(AnalogRead(0));

don't forget to add a delay (300ms) between each loop otherwise your proc "get high"

eric
« Last Edit: April 16, 2006, 03:22:54 pm by mrbbp » Logged

Pages: [1]   Go Up
Jump to: