Using the random number function on C to make delays random
Using the random number function on C to make delays random
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?
- 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
Hello Ziggy,
Dave S's posts on this topic should help.
http://www.matrixmultimedia.com/mmforum ... =29&t=8931
Dave S's posts on this topic should help.
http://www.matrixmultimedia.com/mmforum ... =29&t=8931
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Using the random number function on C to make delays ran
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
- 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
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:
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. Jonny
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
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. Jonny