Author Topic: Setting an random LED high  (Read 15899 times)

0 Members and 1 Guest are viewing this topic.

Offline pelle.jansenTopic starter

  • Contributor
  • Posts: 24
  • Country: nl
    • Check out my Youtube channel!
Setting an random LED high
« on: February 07, 2012, 04:56:19 pm »
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
« Last Edit: February 07, 2012, 06:04:49 pm by pelle.jansen »
 

Alex

  • Guest
Re: Setting an random LED high
« Reply #1 on: February 07, 2012, 05:21:13 pm »
How many LEDs do you have? 8?
 

Offline pelle.jansenTopic starter

  • Contributor
  • Posts: 24
  • Country: nl
    • Check out my Youtube channel!
Re: Setting an random LED high
« Reply #2 on: February 07, 2012, 05:27:38 pm »
How many LEDs do you have? 8?

Yes. 8 LEDs with their appropriate resistors ofcourse.
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17826
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Setting an random LED high
« Reply #3 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
 

Offline pelle.jansenTopic starter

  • Contributor
  • Posts: 24
  • Country: nl
    • Check out my Youtube channel!
Re: Setting an random LED high
« Reply #4 on: February 07, 2012, 06:11:28 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
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.
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17826
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Setting an random LED high
« Reply #5 on: February 07, 2012, 06:14:34 pm »
if you start with a large number and do some random calculation on it, and then do the same calculation on the result, and then do the same calculation on the result you will keep getting different numbers, you just need to use the last digit(s)

for example:

start
A = 256
A = A*25/7
B = (last digit)
Led = B
goto start
 

Offline Jad.z

  • Regular Contributor
  • *
  • Posts: 83
  • Country: 00
Re: Setting an random LED high
« Reply #6 on: February 07, 2012, 06:21:29 pm »
Here is a link to a Binary to Decimal conv c code:
http://www.daniweb.com/software-development/c/code/216372
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1084
  • Country: gb
    • Circuitology - Electronics Assembly
Re: Setting an random LED high
« Reply #7 on: February 07, 2012, 06:27:30 pm »
To make it random, you can use the ADC (assuming you have one) and read the least significant bits and use them as a seed for your random number generator.
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17826
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Setting an random LED high
« Reply #8 on: February 07, 2012, 06:28:39 pm »
what will you supply the ADC with ?
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1084
  • Country: gb
    • Circuitology - Electronics Assembly
Re: Setting an random LED high
« Reply #9 on: February 07, 2012, 06:30:49 pm »
what will you supply the ADC with ?

Whatever you like. Nothing. It's just going to be reading noise anyway.
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17826
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Setting an random LED high
« Reply #10 on: February 07, 2012, 06:32:43 pm »
thats true, I was wondering where could you get some random noise, maybe just connect an opamp to it and an "aerial" on the opamp input, it can pick up mains hum or anything doing around. remember that micro-controller ADC don't have particularly high input impedences, will they just made noise ?
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1084
  • Country: gb
    • Circuitology - Electronics Assembly
Re: Setting an random LED high
« Reply #11 on: February 07, 2012, 06:48:15 pm »
I would expect an on-chip ADC to produce some internal noise even without any input...probably enough to affect the first few LSBs, depending on the resolution. You should get at least one random bit from ADC noise whatever you use, really.
 

Offline SeanB

  • Super Contributor
  • ***
  • Posts: 16284
  • Country: za
Re: Setting an random LED high
« Reply #12 on: February 07, 2012, 07:48:43 pm »
If you are going to use the on board ADC just use 2 resistors to provide it a half supply voltage, and read it as a seed. There will be enough noise on the lower bits if you are using the internal reference and no decoupling cap on the input to the ADC, especially if the chip is not in a low power mode at the time. Then take the random number output and AND it with 00000111 binary, to mask out a value between 0 and 7 dec. Then you can either use a table to drive the outputs or use any other method as you now have a random number with 8 variations.
 

Alex

  • Guest
Re: Setting an random LED high
« Reply #13 on: February 07, 2012, 08:07:55 pm »
You can convert it to decimal in software, but then you have to divide by 32. You must utilise the full range of the random number generated by the rand function, otherwise sometimes no LEDs will be on.

Here is a lightweight solution without multiplications and divisions. You can do a simple set of if/elseif statements. You might need to change this to compile under HITECH C. The idea is to decimate the number into ranges.

unsigned char var;  //declare

var = rand (); //initialise by generating a random number 0...255.

if ((var>=0)  && (var<=31)) light_led(1);
elseif ((var>=32)  && (var<=63)) light_led(2);
elseif ((var>=64)  && (var<=95)) light_led(3);
.....
elseif ((var>=223)  && (var<=255)) light_led(8);

Even if you get the logic a bit wrong, only 1 led will light up because of the elseif statement.

Simples!

EDIT: Sean, the method in your last post above will not work, it only samples regions inside the dynamic range of the random variable. Most of the times the bitwise operation it will return 0. For example, if the random number is 8 in decimal i.e. 0x00001000, then 0x00001000 & 0x00000111 = 0x00000000 i.e. 0.
 

Offline Jad.z

  • Regular Contributor
  • *
  • Posts: 83
  • Country: 00
Re: Setting an random LED high
« Reply #14 on: February 07, 2012, 08:21:09 pm »
What you people are suggestingis a bit too much for his simple application.
You are suggesting to use white noise, to generate a completely random number, for someone who clearly stated that pseudo random is good enough. And lets not forget that the range he needs is 0 to 7 now how random that have to be ::)
Plus he is a beginner who have just started experimenting with PICs and flashing some LEDs; i don't think ADC come next, not yet.
« Last Edit: February 07, 2012, 08:24:01 pm by Jad.z »
 

Offline pelle.jansenTopic starter

  • Contributor
  • Posts: 24
  • Country: nl
    • Check out my Youtube channel!
Re: Setting an random LED high
« Reply #15 on: February 07, 2012, 08:58:23 pm »
I could also use the timer. The PIC16F877A i am using has 3 build in timer modules. All of them are still free but I seem to be unable to set the OPTION_REG register to set the timer options. I get the error "conflicting declarations for variable "OPTION_REG"(/Microchip/Hi_Tech_C_MIDRANGE/include/pic16f877a.h:639). When I go to that line in question it is the volatile unsigned char OPTION REG @0c081. Now I am not that familiar with .h files but it looks fine. Otherwise would it be a good idea to just let the timer run at a low speed, reset when it reaches 7 and read the timer when needed? That might work. Otherwise counting pulses from an external 555 timer ic should also do the trick. By reading the value and resetting when it reaches 7 it should give me an random number. It is just a little fun prototype so it doesn't has to be perfect.

@Jad.z You can learn a lot in 3 weeks. About a month ago I barely knew what a microcontroller was. Now I at least master digital IO and know how to use other things. Like timers. When the option register is not giving you an headache :-\.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11915
  • Country: us
Re: Setting an random LED high
« Reply #16 on: February 07, 2012, 09:39:56 pm »
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?

I think you have a bit of a misunderstanding here, perhaps. As far as a computer is concerned a number is a number. The number "5" for example is the same number in binary or in decimal (or in hex or in octal). It is only the representation on paper that changes, not the size of the number. So there is no need to convert to decimal. You just calculate with the number as an integer--for example, add and subtract, multiply and divide as required.
 

Offline PedroDiogo

  • Regular Contributor
  • *
  • Posts: 62
  • Country: pt
  • EE Student
Re: Setting an random LED high
« Reply #17 on: February 07, 2012, 10:10:50 pm »
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


I suggest you pick the 3 LSB (as you only need to light 8 LEDs) from the output of the rand() function and convert it to decimal.

I can see a simple way to convert it to decimal:

Code: [Select]
#define BIT(word, n) ((word >> n) & 1)
#define randToDecimal(word) BIT(word,0) + BIT(word,1)*2 + BIT(word,2)*4

void main ()
{
int randomNumber = rand();
printf("Random number: %d\n", randomNumber);
printf("3 LSB to decimal:%d\n",randToDecimal(randomNumber));
}

And then use the result to light up the appropriate LED.
However you could use the rand() output to directly light a random number of LEDs each time, without converting it to decimal.
 

Offline Jad.z

  • Regular Contributor
  • *
  • Posts: 83
  • Country: 00
Re: Setting an random LED high
« Reply #18 on: February 07, 2012, 10:12:06 pm »
Actually I once read a code that generate a random number by counting the bounces of a tactile switch.

A bit off topic, but here is an application note on switch bounce:
http://pdfserv.maxim-ic.com/en/an/AN287.pdf

And here is an example on debouncing using software:
http://www.secondvalleysoftware.com/hardware/io/pdfs/softDebounce.pdf

Extra one (I know you can't speak assembly, but you can read the description of the steps  :P)
http://members.ee.net/brey/Debounce.PDF
 

Offline senso

  • Frequent Contributor
  • **
  • Posts: 951
  • Country: pt
    • My AVR tutorials
Re: Setting an random LED high
« Reply #19 on: February 08, 2012, 12:54:20 am »
There is also the shift solution.
int i = rand() % 8;
PORTA = 1<<i;

And its done.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Setting an random LED high
« Reply #20 on: February 08, 2012, 01:26:30 am »
Since you're just playing around, Senso's code is all you probably need.  The C function rand() returns a pseudo-random number of so many bits.  (Consult your compiler documentation.)  You then use the C modulus operator like Senso shows in his example to turn the number rand() returns into a number between, in your case, 0 and 7.

The problem with rand() is that every time you power-up or reset, the numbers it generates will be the same as last time.  If you're worried about that then read up on the srand() function.  This provides a 'seed' for the pseudo-random number generator.  Where do you get a good seed?  Read the Wikipedia page for "Hardware random number generator."  You basically need a white noise source such as a thermistor or avalanche diode.  You might be able to utilize a timer and/or even some sort of user interaction to seed the generator as well. 

You need to seed the generator only once after power-on/reset.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9960
  • Country: nz
Re: Setting an random LED high
« Reply #21 on: February 08, 2012, 01:45:30 am »
id probably just program an array of random numbers into the micros eeprom and step through the array index jumping in steps derived from the value obtained and values either side of it.
But to avoid the exact values at startup you'd need to periodically store the current random value in eeprom and use that to seed the system at startup.
« Last Edit: February 08, 2012, 01:53:04 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Setting an random LED high
« Reply #22 on: February 08, 2012, 02:31:15 am »
I guess the funny thing about randomness, at least on microcontrollers, is that it isn't simple.  You can get a good seed on a PC just by using the timer.  (As in the date and time, how many seconds the PC has been running, etc.)  You generally don't have this luxury on a MCU.

It's not terribly satisfying to see your program do the exact same thing each time you power it up when it is supposedly doing something 'at random.'  LOL.

Some MCU have built-in temperature sensors.  The lower-order bits of that sensor is another source of 'random' bits for your seed.
 

Offline eliocor

  • Supporter
  • ****
  • Posts: 521
  • Country: it
    • rhodiatoce
Re: Setting an random LED high
« Reply #23 on: February 08, 2012, 10:13:15 am »
I think it is very simple:

Code: [Select]
unsigned char Value=0, oldValue=0 ;

for (;;)
{
   Value = rand() & 0x07 ; // mask all most significant bits except the 3 LSB:
                           // 'Value' is between 0 and 7
   if (Value != OldValue)
      {
      OldValue = Value ;
      // light the 'nth' LED using the number (0-7) stored in 'Value'
      }
 }
If you need a seed for your random generator, the suggestion to use the Least Significant Bits (LSB) of an ADC channel is a good one.
 

Offline pelle.jansenTopic starter

  • Contributor
  • Posts: 24
  • Country: nl
    • Check out my Youtube channel!
Re: Setting an random LED high
« Reply #24 on: February 08, 2012, 04:29:56 pm »
I think it is very simple:

Code: [Select]
unsigned char Value=0, oldValue=0 ;

for (;;)
{
   Value = rand() & 0x07 ; // mask all most significant bits except the 3 LSB:
                           // 'Value' is between 0 and 7
   if (Value != OldValue)
      {
      OldValue = Value ;
      // light the 'nth' LED using the number (0-7) stored in 'Value'
      }
 }
If you need a seed for your random generator, the suggestion to use the Least Significant Bits (LSB) of an ADC channel is a good one.
This workes like a charm! Does just what I need it to do. Thanks for the input all.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf