Pages: 1 [2]   Go Down
Author Topic: [SOLVED] Random LED's  (Read 1497 times)
0 Members and 1 Guest are viewing this topic.
Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John! Although this way I won't be able to use shift registers won't I? because in my final project I will be using 27 LED's. But that's not to big of a problem, in that case I will use I2C I/O expanders.

You can use an unsigned long (32-bit integer) to keep track of which LEDs you want on and off.
Code:
int timer = 1000; //Sets timer
const int pinCount = 6; //Sets the number of pins
int Number = 3; //Sets the amount of LEDs that will go on
unsigned long pattern;
int thisPin;
int alreadySet;

void loop(){
  // Turn off all the LEDs
  pattern = 0;

  // Turn on "Number" randonm LEDs
  for (int i=0; i<Number; i++) {
    do {
      thisPin = random(pinCount);
      alreadySet = pattern & (1<<thisPin));
      pattern |= (1<<thisPin);
    }
    while (alreadySet);  // If the LED was already on, try a different one
  }
  //  Send 'pattern' to shift registers
  delay(timer);
}

So i thought this did the trick since I tested it with one shift register (register pin 0 - 7), today I tried daisy chaining an other one to it (register pin 0 - 15). And I ran in to the same problem: most of the time the desired amount of LEDs are burning but about 10% of the time a couple of them don't light up (I guess it has some thing to do with the pattern being full or something?). Is there a fix for this?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you have to show your code.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you have to show your code.

yeah it was basically the code you gave me, modified for shift register usage

it works when my loop is like this:

Code:
void loop(){

    // Turn on "Number" randonm LEDs
  for (int i=0; i<Number; i++) {
    do {
      thisPin = random(8);
      alreadySet = pattern & (1<<thisPin);
      pattern |= (1<<thisPin);
      setRegisterPin(thisPin, HIGH);
      writeRegisters();
    }
    while (alreadySet);  // If the LED was already on, try a different one
  }
pattern = 0;


  delay(timer);
  
  clearRegisters();

}

but as soon as I change the 8 in:
Code:
thisPin = random(8)
to a higher number, (starting to use the second shift register, which is a 74HC595 by the way) I get the problem (most of the time the desired amount of LEDs are burning but about 10% of the time a couple of them don't light up)

Here is the complete code if needed

Code:
int timer = (1000);
int SER_Pin = 8;   //pin 14 on the 75HC595
int RCLK_Pin = 9;  //pin 12 on the 75HC595
int SRCLK_Pin = 10; //pin 11 on the 75HC595
int Number = 4;
int thisPin;
int alreadySet;

#define shiftCount 2 //How many shift registers
#define pinCount shiftCount * 8 //How many pins

boolean registers[pinCount];
unsigned long pattern;

void setup(){
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);
  Serial.begin(9600);

  //reset all register pins
  clearRegisters();
  writeRegisters();
}              

//set all register pins to LOW
void clearRegisters(){
  for(int i = 0; i <  pinCount; i++){
     registers[i] = LOW;
    
  }
}

//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){

  digitalWrite(RCLK_Pin, LOW);

  for(int i = pinCount - 1; i >=  0; i--){
    digitalWrite(SRCLK_Pin, LOW);

    int val = registers[i];

    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);

  }
  digitalWrite(RCLK_Pin, HIGH);

}

//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}

void loop(){

    // Turn on "Number" randonm LEDs
  for (int i=0; i<Number; i++) {
    do {
      thisPin = random(8);
      alreadySet = pattern & (1<<thisPin);
      pattern |= (1<<thisPin);
      setRegisterPin(thisPin, HIGH);
      writeRegisters();
    }
    while (alreadySet);  // If the LED was already on, try a different one
  }
pattern = 0;


  delay(timer);
  
  clearRegisters();

}
« Last Edit: February 10, 2013, 08:47:26 am by Mubanga » Logged

Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh and my set up is like this:
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code looks OK.  Have you checked to see that all of the lights work?  Watch each light for 10 seconds or until if comes on.  It might be that one or more of your LEDs is not working (bad connection) and so the random patterns will sometimes hit a non-working LED.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code looks OK.  Have you checked to see that all of the lights work?  Watch each light for 10 seconds or until if comes on.  It might be that one or more of your LEDs is not working (bad connection) and so the random patterns will sometimes hit a non-working LED.

I ran a test and there was indeed a lose wire, thanks!
« Last Edit: February 10, 2013, 09:35:06 am by Mubanga » Logged

Pages: 1 [2]   Go Up
Jump to: