Using the random number function on C to make delays random

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
ziggy1976
Posts: 16
Joined: Sat Jan 19, 2008 6:54 am
Contact:

Using the random number function on C to make delays random

Post by ziggy1976 »

Hello, I would like to make interrupt or delays random in a program. So when a button is pressed, a buzzer is activated. A random delay or interrupt of 5 to 10 seconds should elapse, then the buzzer is activated again. A random delay between 2 and 10 sec' duration then starts. Is there a way to seed the random function, and then use the value generated to set the delay times?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using the random number function on C to make delays ran

Post by Benj »

Hello Ziggy,

Dave S's posts on this topic should help.
http://www.matrixmultimedia.com/mmforum ... =29&t=8931

ziggy1976
Posts: 16
Joined: Sat Jan 19, 2008 6:54 am
Contact:

Re: Using the random number function on C to make delays ran

Post by ziggy1976 »

I've started the flow chart, I need help with the c-code. How do I use the random value to influence the delays?
Attachments
sTART_TIMERII.fcf - Main.JPG
(42.64 KiB) Downloaded 1798 times

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Using the random number function on C to make delays ran

Post by JonnyW »

Hi Ziggy.

In your flowchart, create a variable and call it, say, 'rnd_seconds' - if you are only worried about an accuracy to the nearest second you can create this as a byte or int, it doesnt matter.

Now create a calculation icon containing the code:

Code: Select all

  rnd_seconds = random() % delay_time
Where delay_time is the time you would like to wait for as a maximum. You can then use rnd_seconds in the delay field and this will wait between 0 and delay_time.

I'm sorry, but I dont have v3, so cant post a solution file but the screenshot below shows a macro you could call that allows you to wait between 'min' and 'max' seconds. You can call this from the various places in your program. Note this macro uses local variables.

I hope this helps.
img.PNG
Macro to delay randomly
(21.18 KiB) Downloaded 2890 times
Jonny

Post Reply