Go Down

Topic: random delay (Read 2013 times) previous topic - next topic

Frogeraie

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?

mrbbp

#1
Apr 15, 2006, 11:30 pm Last Edit: Apr 15, 2006, 11:59 pm by mrbbp Reason: 1
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: [Select]
int temps;

void setup() {
 beginSerial(115200);
}

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


i hope :-?

eric

mrbbp

#2
Apr 16, 2006, 12:03 am Last Edit: Apr 16, 2006, 12:11 am by mrbbp Reason: 1
well

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

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: [Select]
temps=((analogRead(0)%2)+1)*1000;
generate a number between 1 and 2 multiply by 10000

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

regards

eric

mrbbp

#3
Apr 16, 2006, 12:28 am Last Edit: Apr 16, 2006, 01:00 am by mrbbp Reason: 1
after some test..

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

have better random result (between min and max value) you want :P

eric :D

Frogeraie

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

mrbbp

#5
Apr 16, 2006, 10:19 pm Last Edit: Apr 16, 2006, 10:22 pm by mrbbp Reason: 1
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: [Select]
printInteger(AnalogRead(0));

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

eric

Go Up