Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

3-9 min random timer

Status
Not open for further replies.

dyverdown

New Member
I am hoping to find a schematic / program to have a PIC run a reocuring timer that closes a relay randomly at times from 3-9 minutes (no need to be super accurate)

Anyone direct me to something similar?

-Mike
 
Last edited:
You can simply use a random routine to create a pseudo-random interval between 3 and 9 minutes (in reality between 0 and 6 minutes, and add three to it :D ). Obviously you do it in seconds, not minutes.

C:
// Generates random number in range (lower - upper).
unsigned int GetRandom(unsigned int lower, unsigned int upper)
{
    unsigned int num = (unsigned int)(rand() % (upper - lower + 1)) + lower;
    return num;
}

So use "random value = GetRandom(180, 540);"

The main issue is creating a decently random starting point - a simple way is to have a start button.

As the program initialises, have it start a fast timer running, then wait for a start button to be pressed - once the start button is pressed read the fast timer, and use that to initialise the random seed using srand().

Here's a simple test I used recently, which utilised the RTTC to set the random seed.

C:
// Use current time as seed for random generator during testing
    srand((unsigned int)rtcc.time.seconds*rtcc.time.minutes*rtcc.time.hour);
 
Quick and dirty with Arduino or an ATTINY85 using mBlock graphical programming.

Drag and drop function blocks into 3'rd window, configure, then program part.
mBlock generates Arduino code from the block configuration. Use a Nano board,
~$3, or a Nano board as programmer for an ATTINY85.

1692479820571.png


1692479906490.png


1692479544529.png



Regards, Dana.
 
I managed to screw up the block coding, it should be as follows :

1692552392222.png


Regards, Dana.
 
Status
Not open for further replies.

Latest threads

Back
Top