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

Progamming problem

Status
Not open for further replies.
Hi, I need to light an LED ( output digital 1 ) but in random hours , i dont know the algorithm , but im sure im gonna use srand() and rand ? but i dont know how to embolyee them for that task ?
 

edeca

Active Member

edeca

Active Member
What compiler do you use? I know nothing about that chip, but perhaps we can find some sample code if we know what you are using.
 

edeca

Active Member
The problem is that you need a good source of "random" data. There are various algorithms that will give you a "random" number but you need a good seed to start with.

Here's a really good article that explains the problems:

http://www.electro-tech-online.com/custompdfs/2009/06/68hc11random.pdf

Depending what you are using the numbers for, this might be fine. If you just need a random interval, get a number from the table method he describes in the article and apply it to a timer for a delay (either in seconds, minutes, with post-scaling or without).
 

kchriste

New Member
Forum Supporter
Im using Keil C51
I think its an algorithmic problem not really realted to the uC or the compiler , right?
Correct.
If your compiler supports the rand function, then go ahead and use it to generate the delay between LED OFF and LED ON. Beware that the program using the rand function will give the same string of "random" numbers every time the micro is reset. It will appear to be random, but nothing generated by an algorithmic function really is.
One way to seed using the srand function is to take a number from a free running timer based on when the user last pushed a button, etc.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top