Electronics > Microcontrollers

Setting an random LED high

(1/10) > >>

pelle.jansen:
Hello all,
For a project I need to set an random led high.
So what I would have to do is:
1. create a random number between 0 and 7
2. save that number in a variable
3. light the led
4. save same random number in different variable
5. generate new random number
6. compare old and new random number: if they are equal generate new random number.
run rest of code.

Yesterday in the shoutbox I got pretty far but just barely not there. I do have an random number using rand() but i think it is in binary and between 0b00000000 and 0b11111111 so 0 and 255 decimal. Any idea how to convert it to decimal?  The function doesn't take arguments so the only thing I can come up with is to make the function recursive and let it run until it is within parameters. Timing does not matter. It doesn't have to be completely random. Pseudo random is good enough.

Than there is still the issue of setting and clearing the same led as number in the variable. You cant just use RD num = 1; so how would you do that?

I have something like it running in Python but there you have the great random.randrange() ;D

EDIT: i am using the Hi Tech C compiler

Alex:
How many LEDs do you have? 8?

pelle.jansen:

--- Quote from: Alex on February 07, 2012, 05:21:13 pm ---How many LEDs do you have? 8?

--- End quote ---

Yes. 8 LEDs with their appropriate resistors ofcourse.

Simon:
just start with a number, do something to it like multiply and divide it, add or subtract numbers, use a longish number then just use the last decimal digit or do it all in binary and you can just use the last 4 digits, light up the let that corresponds to the number then rerun the computation, you can add in the check that your not using the same number again.

The actual syntax will depend on the language your using

pelle.jansen:

--- Quote from: Simon on February 07, 2012, 05:52:04 pm ---just start with a number, do something to it like multiply and divide it, add or subtract numbers, use a longish number then just use the last decimal digit or do it all in binary and you can just use the last 4 digits, light up the let that corresponds to the number then rerun the computation, you can add in the check that your not using the same number again.

The actual syntax will depend on the language your using

--- End quote ---
I use C with the hi tech picc compiler.
Wouldn't that give the same number every time? Or you would have to randomize the calculations you would need something random again. I already have an random number but it is 8 bit binary 00000000 to 11111111. It would be a solution to let just a single bit count or to convert the binary number to decimal.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version