Jump to content
Sign in to follow this  

need some coding help- 7 pins with 7 leds

Recommended Posts

So lets start with the problem

7 pins, with 7 leds, I need to turn them on and off in a psudo-random order.

only one pin at a time should be on.


now i have been thinking about this and haven't come up with any way to do this.

its easy to go increment them one at a time, or shift bits in the P1OUT register, but it will not give me the effect i need.




any ideas?





Share this post

Link to post
Share on other sites

My white noise generator basically did the same thing, only to one pin.




You could do something like...


 unsigned long int newr;
 unsigned char lobit;
 unsigned char b31, b29, b25, b24;
 unsigned char pin;

 // extract four chosen bits from the 32-bit register
 b31 = (reg & (1uL << 31)) >> 31;
 b29 = (reg & (1L << 29)) >> 29;
 b25 = (reg & (1L << 25)) >> 25;
 b24 = (reg & (1L << 24)) >> 24;

 // XOR the four bits together
 lobit = b31 ^ b29 ^ b25 ^ b24;

 // shift and incorporate new bit at bit position 0
 newr = (reg << 1) | lobit;

 // replace register with new value
 reg = newr;

 // choose a pin randomly
 pin = 1 << (rand()&7);

 // output value
 switch (reg & 1) { // work on the last bit of the register
   case 0:
     P1OUT |= pin;
   case 1:
     P1OUT &= ~pin;


It works on the idea of a linear feedback shift register, which you can read about at Wikipedia: http://en.wikipedia.org/wiki/Linear_fee ... t_register


Hope this helps!

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...