random delay

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?

i’m not in top for arduino…

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

this should work.

int temps;

void setup() {
  beginSerial(115200);
}

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

i hope :-?

eric

well

it seems it’s really really really :-? bad action to use PSEUDO random function :stuck_out_tongue:

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:

temps=((analogRead(0)%2)+1)*1000;

generate a number between 1 and 2 multiply by 10000

temps=(analogRead(0)%1000)+1000;

generate a number between 1000 and 2000.

regards

eric

after some test…

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

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

eric :smiley:

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

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

printInteger(AnalogRead(0));

don’t forget to add a delay (300ms) between each loop otherwise your proc “get high”

eric