Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: wrongway-feldmen on Jan 06, 2016, 07:39 am

Title: random on/off with shift register
Post by: wrongway-feldmen on Jan 06, 2016, 07:39 am
can some one help me I am newbie and would like to make a random output of on/off LEDS with a 74HC595 2 or more if there is any way to do this so no 2 LEDS are on at the same time and they would light some models homes I am building would like to also have them all turn off at start up and then some thing about a PMW on one or two of the pins would get me a dimming effect
Title: Re: random on/off with shit registoer
Post by: larryd on Jan 06, 2016, 07:47 am
Please edit the title of this thread. sh#t to shift

You control the value sent to the shift register, place your limits in your code.
Title: Re: random on/off with shift register
Post by: wrongway-feldmen on Jan 06, 2016, 07:59 am
Please edit the title of this thread. sh#t to shift

You control the value sent to the shift register, place your limits in your code.

sorry I am newbie and not sure what you mien can you show me what it is your telling me
Title: Re: random on/off with shift register
Post by: robtillaart on Jan 06, 2016, 12:41 pm
The shift register can be seen as the binary representation of the value sent to it. This value may contain only one 1 bit and the rest should be 0.

This snippet of code generates random single bit values, up to you to integrate it in a sketch.

Code: [Select]
int x = random(8); // 0..7
int val = 1 << x;   // shift one single 1 bit to the right giving one of { 1,2,4,8,16,32,64,128 }
shiftOut(val);

that's about it.

Do you want to include the option that all leds are OFF?
Title: Re: random on/off with shift register
Post by: wrongway-feldmen on Jan 06, 2016, 02:54 pm
The shift register can be seen as the binary representation of the value sent to it. This value may contain only one 1 bit and the rest should be 0.

This snippet of code generates random single bit values, up to you to integrate it in a sketch.

Code: [Select]
int x = random(8); // 0..7
int val = 1 << x;   // shift one single 1 bit to the right giving one of { 1,2,4,8,16,32,64,128 }
shiftOut(val);

that's about it.

Do you want to include the option that all leds are OFF?

I am very new to all this so sorry for question's that might be kind of dumb and I am sorry but I would like some more help with this if any one wants to chime in what line do I change or add to get the random on/off and yes sir I would like them to all turn off then random on/off or should I use a different sketch and what do I change too add a shift register or more





Title: Re: random on/off with shift register
Post by: robtillaart on Jan 06, 2016, 05:44 pm
you should learn to use [code]   [/code] tags. it is the </> buttin when editing.
Title: Re: random on/off with shift register
Post by: wrongway-feldmen on Jan 07, 2016, 04:20 am
you should learn to use [code]   [/code] tags. it is the </> buttin when editing.

dont know what that means but thank you anyway was looking for some help as a newbie and just learning but thats ok no worries
Title: Re: random on/off with shift register
Post by: arifs on Jan 07, 2016, 06:08 am
hi, i can feel you because i also recently got stuck (also new to arduino), I found my desired effects and method with arrays,  here what i have done for my project where i want to able to change patterns easily. here is the code i used to lit leds one by one, although efficient and fancy codes exist for doing so, but as i said i may be changing this code easily in future. The Ones are led on and Zeros are led off. see how i lit them.

Code: [Select]

int Array [29] = {
  0b00000001,
  0b00000011,
  0b00000111,
  0b00001111,
  0b00011111,
  0b00111111,
  0b01111111,
  0b11111111,
  0b01000000,
  0b00100000,
  0b00010000,
  0b00001000,
  0b00000010,
  0b00001000,
  0b00010000,
  0b00100000,
  0b10000000,
  0b01000000,
  0b00100000,
  0b00010000,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111
};



with this i can call any one pattern or all.

Title: Re: random on/off with shift register
Post by: wrongway-feldmen on Jan 07, 2016, 06:14 pm
hi, i can feel you because i also recently got stuck (also new to arduino), I found my desired effects and method with arrays,  here what i have done for my project where i want to able to change patterns easily. here is the code i used to lit leds one by one, although efficient and fancy codes exist for doing so, but as i said i may be changing this code easily in future. The Ones are led on and Zeros are led off. see how i lit them.

Code: [Select]

int Array [29] = {
  0b00000001,
  0b00000011,
  0b00000111,
  0b00001111,
  0b00011111,
  0b00111111,
  0b01111111,
  0b11111111,
  0b01000000,
  0b00100000,
  0b00010000,
  0b00001000,
  0b00000010,
  0b00001000,
  0b00010000,
  0b00100000,
  0b10000000,
  0b01000000,
  0b00100000,
  0b00010000,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111
};



with this i can call any one pattern or all.


I dont know if I am doing this right but I dont even have a basic code to start with I just copied the shitout code over but seem complicated to me
Title: Re: random on/off with shift register
Post by: runaway_pancake on Jan 08, 2016, 04:16 am
The following is the link to the standard tutorial on the subject -
https://www.arduino.cc/en/Tutorial/ShiftOut (https://www.arduino.cc/en/Tutorial/ShiftOut)
There is a schematic that, erroneously and infamously, shows the use of a capacitor on the Latch pin - DO NOT USE IT.
I have attached a revised drawing with the correction.
Once you learn how to knock out bytes then you can learn how to generate a random number and then incorporate the two.
Title: Re: random on/off with shift register
Post by: wrongway-feldmen on Jan 10, 2016, 07:01 am
I dont know if I am doing this right but I dont even have a basic code to start with I just copied the shitout code over but seem complicated to me
Code: [Select]
//Random blinking LEDS using a 74HC595 shift register. Original code found on bildr.org

    int SER_Pin = 11;    //pin 15 on the 75HC595
    int RCLK_Pin = 8;   //pin 12 on the 75HC595
    int SRCLK_Pin = 12; //pin 10 on the 75HC595
   
int Array [29] = {
  0b00000001,
  0b00000011,
  0b00000111,
  0b00001111,
  0b00011111,
  0b00111111,
  0b01111111,
  0b11111111,
  0b01000000,
  0b00100000,
  0b00010000,
  0b00001000,
  0b00000010,
  0b00001000,
  0b00010000,
  0b00100000,
  0b10000000,
  0b01000000,
  0b00100000,
  0b00010000,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111,
  0b00000000,
  0b11111111
};


         
    #define number_of_74hc595s 1 //How many of the shift registers are there daisey chained?
    #define numOfRegisters number_of_74hc595s * 9
         
    int registers[numOfRegisters];
         
    void setup(){
         
      Serial.begin(9600);
      setRegister(1, LOW);
  setRegister(2, LOW);
  setRegister(3, LOW);
  setRegister(4, LOW);
  setRegister(5, LOW);
  setRegister(6, LOW);
  setRegister(7, LOW);
  setRegister(8, LOW);
     
  writeRegisters();
         
      pinMode(SER_Pin, OUTPUT);
      pinMode(RCLK_Pin, OUTPUT);
      pinMode(SRCLK_Pin, OUTPUT);
      randomSeed(analogRead(A0));
         
      clearRegisters();
      writeRegisters();
    }               

    void clearRegisters(){
      //set all registers to LOW
      for(int i = numOfRegisters; i >  0; i--){
         registers[i] = LOW;
      }
    }


    void writeRegisters(){
    //Set and display registers
      digitalWrite(RCLK_Pin, LOW);
         
      for(int i = numOfRegisters; i >  0; i--){
        digitalWrite(SRCLK_Pin, LOW);
           
        int val = registers[i];
           
        digitalWrite(SER_Pin, val);
        digitalWrite(SRCLK_Pin, HIGH);
      }
      digitalWrite(RCLK_Pin, HIGH);
    }

    void setRegister(int index, int value){
      //set individual register
      registers[index] = value;
    }


    void loop(){
     clearRegisters();

     for(int i= 0; i < numOfRegisters; i ++){
       int randNum = random(2);
           
       if(randNum == 1){
         setRegister(i, HIGH);
       }
     }

     writeRegisters();
     delay(333);
    }



I put together some code from this and that with help from you guys THANK YOU so much helping me get started but would like to add PWM to the OE pin so I can do some random dimming if possible and will try to figure out what to change to get 2 or more registers going