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.

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 ?
 
What processor are you using? What language do you plan to write your code in?

Generating random numbers on a microcontroller generally doesn't involve using rand(), as there is no sufficient source of entropy (unless you have an external clock or are using caesium on your PCB).

Check these for a start:


https://www.phanderson.com/PIC/16C84/random.html
 
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.
 
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:

https://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).
 
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.

New Articles From Microcontroller Tips

Back
Top