Linking Four SHift Register
Moderators: adafruit_support_bill, adafruit

Linking Four SHift Register

by bob500000 on Mon Apr 15, 2013 4:43 am

Hi Guys,

Is it possible to link 4 shift registers via one arduino board, I am either looking at using just 4 pins on the board, I have all the code that I want to run, just the linking of the shift registers.

I was intially thinking about doing the following:

Code: Select all | TOGGLE FULL SIZE
//First Shift Register
Clock1 int 1;
Data1 int 2;
OutputEnable1 int 3;
Latch1 int 4;

//Second Shift Register
Clock2 int 5;
Data2 int 6;
OutputEnable2 int 7;
Latch2 int 8;

//Third Shift Register
Clock3 int 9;
Data3 int 10;
OutputEnable3 int 11;
banned int 12;

//Fourth Shift Register
Clock4 int 13;

\\Analog Pins
Data4 int14;
OutputEnable4 int 15;
Latch4 int 16;

However this uses 16 ports on the board, I currently use 3 ports already which drive 1 set of LED's and 2 switches.

is there a simplier solution?

Cheers.
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by adafruit_support_mike on Mon Apr 15, 2013 10:38 am

When you say 'shift registers', do you mean the 74HC595?

If so, you're in luck. The '595 is designed to be daisy-chained. There's a pin named 'Q7S' which connects to the last bit in the chip's internal shift register, and all you need to do is connect that to the 'DS' (serial data input) pin of the next chip.

Daisy-chained '595s all use the same serial-data and display-output signals, so you can run a whole string of them from the same pins you'd need to run a single one.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10841
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

by bob500000 on Mon Apr 15, 2013 1:14 pm

Thanks mike, I am talking about the 74HC595.

So the code I have will work?

In terms of power, will I need additional support, I am currently running 3 boards on a 12v power supply and am wondering if this is going to suffice running 4 shift registers.

Cheers
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by adafruit_support_mike on Tue Apr 16, 2013 3:16 pm

As written, no it won't. In C, you need to put the type declaration (int) before the name of the variable (Clock, etc). Also, you only need the pin assignments for a single chip.

Here's a rough version of what the code should look like:

Code: Select all | TOGGLE FULL SIZE

/*  The original code for your first shift register

    'const' tells the compiler these values should never change
    'char' tells the compiler to use 8-bit slots to store the values
*/
const char Clock = 1;
const char Data = 2;
const char OutputEnable = 3;
const char Latch = 4;

/*  A new variable to count the number of chips in the chain
*/
const char registerCount = 4;

digitalWrite( Latch, LOW );     //  get ready to shift data

/*  I've written this code to count backwards, because the first
    bit you send as data will end up going farthest.  This arrangement
    puts chip-1, bit-1 closest to the Arduino and chip-4, bit-8
    farthest away.
*/
for (char i=registerCount ; i ; i--) {
    for (char j=8 ; j ; j--) {
        digitalWrite( Clock, LOW );
        digitalWrite( Data, chipAndBit( i, j ) );
        digitalWrite( Clock, HIGH );
    }
}

digitalWrite( Latch, HIGH );    //  shift everything to output


/*  This routine decides what value each chip/bit should have
*/
char chipAndBit (char c, char b) {
    if (random() > .5) {
        return( HIGH );
    } else {
        return( LOW );
    }
}

Now, that's written the long way to give you a better idea of what's going on. You can eliminate the two loops that send data and manage the clock by using the Arduino library's shiftOut() method. There's a page dedicated to driving a '595 with shiftOut here: http://arduino.cc/en/Tutorial/ShiftOut
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10841
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

by bob500000 on Tue Apr 16, 2013 4:57 pm

Thanks mate,
Looking at your code was dreading how you put it together.

Link to image forum, sorry will not let me up upload currently.

viewtopic.php?f=25&t=38620

I currently have the following code and am wondering if you could take a look at it, I had 4 shift registers daisy chained together and need it to do the following:

When the user presses button 0 once the first sequence is carried out

when the user presses button 0 twice the second sequence is carried out

When the user presses button 1 the second sequence is carried out

when the user presses button 1 twice the first sequence is carried out

when the first set of LED's are lit, meaning both tubes they need to turn off to allow the second lot to follow suit lighting the same colours as the first tubes, as bit confusing I know but to help I uploaded a picture to show the project.

Anyway once they have lit up they need to light the final set which will be based upon pin 10. to complete the cycle.

Code: Select all | TOGGLE FULL SIZE
#include <Button.h>

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int outputEnablePin = 3;
int ledPin = 10;
//Button trigger = Button(2,LOW);
int sr1Colour = 0;  // Set for ShiftReg1 as 0 = off, 1 = green, 2 = red
int sr2Colour = 0;

byte leds = 0;
byte button[] = {8, 9};
byte switch_value = 0;
boolean btnOn = false;
int count = 0;

#define NUMBUTTONS sizeof(button)
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
  setBrightness(0);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  pinMode(outputEnablePin, OUTPUT);
  //pinMode(2,INPUT); //Trigger switch sits on pin 2
  Serial.begin(9600);
  Serial.println("Welcome");
  byte i;
 
   for (i=0; i< NUMBUTTONS; i++) {
    pinMode(button[i], INPUT);
    digitalWrite(button[i], HIGH);
  }
    //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;
}
//byte pressCount1 = 0;
SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(button[index]);   // read the button
    /*   
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
   
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
          count = count+1;
          //switch_value = switch_value + 1;

      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
 /////////////////////////////////////////////////////
 //This is Button 1 Set Press
 /////////////////////////////////////////////////////
 
  if(justpressed[0] && btnOn)
  {
    count;
    sequence2();
    justpressed[0] = 0;
    btnOn = false;
  } 
  if(justpressed[0]) {
    Serial.println("Button 0 pressed");
    btnOn = true;
    count;
    justpressed[0] = 0;
    sequence1();
   }
//  /////////////////////////////////////////////////////
// //This is Button 2 Set Press
// /////////////////////////////////////////////////////
if(justpressed[1] && btnOn)
  {
    count;
    sequence1();
    justpressed[1] = 0;
    btnOn = false;
  } 
  if(justpressed[1]) {
    Serial.println("Button 1 pressed");
    btnOn = true;
    count;
    justpressed[1] = 0;
    sequence2();
   }
//  /////////////////////////////////////////////////////
// //Run final Sequence1
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal1();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence2
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal2();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence3
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal3();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence4
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal4();
  ledPin = HIGH;
}
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister
 ////////////////////////////////////////////////////
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister2
 ////////////////////////////////////////////////////
void updateShiftRegister2()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister3
 ////////////////////////////////////////////////////
void updateShiftRegister3()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister4
 ////////////////////////////////////////////////////
void updateShiftRegister4()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister5
 ////////////////////////////////////////////////////
void updateShiftRegister5()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister6
 ////////////////////////////////////////////////////
void updateShiftRegister6()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister7
 ////////////////////////////////////////////////////
void updateShiftRegister7()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister8
 ////////////////////////////////////////////////////
void updateShiftRegister8()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is setBrightness
 ////////////////////////////////////////////////////
void setBrightness(byte brightness) // 0 to 255
{
  analogWrite(outputEnablePin, 255-brightness);
}
/////////////////////////////////////////////////////
 //This is Sequence 1
 ////////////////////////////////////////////////////
void sequence1(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
   sr1Colour = 1; //Set SR1 to green
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 2
 ////////////////////////////////////////////////////
void sequence2(){
  Serial.println("button 2");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister2();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3)
    {
      Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister2();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[1] && btnOn && (count >=3)) {
      leds = 0;
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
   sr1Colour = 2; //Set SR1 to red
   
  leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 3
 ////////////////////////////////////////////////////
void sequence3(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister3();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister3();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
  sr1Colour = 2; //Set SR1 to red
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 4
////////////////////////////////////////////////////
void sequence4(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister3();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister4();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
  sr1Colour = 1; //Set SR1 to Green
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 5
////////////////////////////////////////////////////
void sequence5(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister5();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister5();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 6
////////////////////////////////////////////////////
void sequence6(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister6();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister6();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 7
////////////////////////////////////////////////////
void sequence7(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister7();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister7();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 8
////////////////////////////////////////////////////
void sequence8(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister8();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister8();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000);
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Final Sequence 1
////////////////////////////////////////////////////
 void sequenceFinal1(){
   sequence5();
   sequence7();
 }
/////////////////////////////////////////////////////
 //This is Final Sequence 2
////////////////////////////////////////////////////
void sequenceFinal2(){
   sequence5();
   sequence8();
 }
/////////////////////////////////////////////////////
 //This is Final Sequence 3
////////////////////////////////////////////////////
void sequenceFinal3(){
   sequence6();
   sequence7();
 }
/////////////////////////////////////////////////////
 //This is Final Sequence 4
////////////////////////////////////////////////////
void sequenceFinal4(){
   sequence6();
   sequence8();
 }

cheers.
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by adafruit_support_mike on Wed Apr 17, 2013 11:45 pm

Looks okay overall, but there are a few things to tweak:

This line isn't quite correct:
Code: Select all | TOGGLE FULL SIZE
#define NUMBUTTONS sizeof(button)

It works for this specific piece of code because you've defined the array 'button' to contain bytes, and sizeof() returns the number of bytes in a data structure. It's much safer to write that kind of thing like this though:
Code: Select all | TOGGLE FULL SIZE
#define NUMBUTTONS sizeof(button) / sizeof(button[0])

That divides the total size of the array by the size of a single cell, giving you the number of items in the array.


This bit of the setup() function:
Code: Select all | TOGGLE FULL SIZE
   for (i=0; i< NUMBUTTONS; i++) {
    pinMode(button[i], INPUT);
    digitalWrite(button[i], HIGH);
  }

Contradicts itself.. it doesn't make sense to write to a pin that's configured for INPUT. You can lose the digitalWrite() line.


In these sections:
Code: Select all | TOGGLE FULL SIZE
  if(justpressed[0] && btnOn)
  {
    count;
    sequence2();
    justpressed[0] = 0;
    btnOn = false;
  } 

the line containing 'count;' doesn't do anything. It isn't really wrong, and the compiler will probably throw that statement away, but the code will be easier to read if you remove all the lines like that.


This chunk probably won't do what you want:
Code: Select all | TOGGLE FULL SIZE
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister();
    delay(50);
      setBrightness(255);
  }

You've commented out the loop, but the block containing the code will still execute once.. and I have no idea what 'bitSet(leds, -5)' will do. From what I can see in the Arduino reference, the second argument needs to be a positive integer.


If I were writing code to do the same thing I'd probably arrange it differently, but that's a question of personal style and not a question of function. If the code runs, does what you want it to, and you can understand what's going on if you leave it alone for six weeks and read it again, you're good.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10841
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

by bob500000 on Thu Apr 18, 2013 3:26 am

Hi Mike thanks for the advice,

Curently almost all of it works, the buttons function proactively, sometimes they work sometimes they dont having a problem getting my head around the button code so e-engineered some previous code.

the count; was fo test puposes, doesnt really do anything useful apart from tell me how many times the buttons have pressed and then eset if its more than 3 to turn all the LED's off.

basically the LED code is to fade each LED on individually, when it has ran though the shift register.

bitSet(leds, -5); - This turns the LED's off once the next sequence starts, if you was to write it out, can you show me how you would do it.

cheers
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by coolr on Thu Apr 18, 2013 8:28 am

Can I get the status to tell which LED is on??
coolr
 
Posts: 2
Joined: Thu Apr 18, 2013 8:26 am

Re: Linking Four SHift Register

by bob500000 on Thu Apr 18, 2013 10:01 am

Your going to need to write a flag and then do an if statement to show if it is true.

The way I did it, was to create to variables sr1colour and sr2colour, these both =0 but when a set of LED's was lit then the at the end of the sequence the sr1colour or sr2colour values were changed depending whcih side of the shift register ran.

For example

if the first shift register ran the first 4 bits then this would be sr1colour as the first 4 were attached to green LED'S

if the firt shift register ran the last 4 bits then this would be sr2colour, as the last 4 were attached to red LED's

So in therory you could use the following code to show each time an LED is lit

heres a break down of what the code actually does:

Code: Select all | TOGGLE FULL SIZE
void sequence1(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
   


This first bit of code cycles through the first 4 bits on the shift register, lighting each one individually, the updateshift register is the cycle moving forward fading on the LED's one at a time,

Code: Select all | TOGGLE FULL SIZE
Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }


This bit carries on from the first but checks to see if the button has been pressed >= 3 times, if it has then the sequence will stop and the LED's will turn off, thus reseting the program.

Hope this helps.
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by adafruit_support_mike on Thu Apr 18, 2013 2:35 pm

bob500000 wrote:bitSet(leds, -5); - This turns the LED's off once the next sequence starts, if you was to write it out, can you show me how you would do it.


Assuming a bit value of 0 means "turn the LED off", I'd just say:

Code: Select all | TOGGLE FULL SIZE
leds = 0;


I generally handle bitmasks with the bitwise logic functions or by setting the values directly:

Code: Select all | TOGGLE FULL SIZE
//  set every other bit in the byte:
leds = B10101010;

//  shift the bits left and add another 1 at the end:
leds <<= 1;
leds += B00000001;


That last line could be written more simply (leds++), but writing it in that form tells the reader I'm playing with the bit patterns.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10841
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

by bob500000 on Thu Apr 18, 2013 4:45 pm

Hi mike,

I understand that although, first off the LEDs don't turn off when you put them at 0, have to use a value that is less than 0; not too sure why this is,

Looking at the cycle I am only using some of the register and it needs to switch been the lsb and the msb to light the right colour.

Example being that the lsb will light 3 LEDs in green and on the same register the msb lights 3 LEDs in red. So the cycle proposed is more complex than just defining which side of the register is lit and setting the amount of LEDs lit!

Can you confirm that the rest of the code will do what I require it to especially the final sequence parts

Cheers
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by bob500000 on Mon Apr 22, 2013 5:22 am

Hi mike any ideas on the above,

Not to rush but got a deadline for next week!

Cheers
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by adafruit_support_mike on Mon Apr 22, 2013 10:52 am

If the compiler likes your syntax I don't see any other problems. The best test is to compile it, load it, and let it run.

Are you running into problems with some specific part of the code or feature?
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10841
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

by bob500000 on Mon Apr 22, 2013 11:44 am

Yeah all works fine apart from the last bit is where I need it to look at the sr1colour and the sr2colour and then works out if the values true or false and if so which method to call depending on the if statements detailed in final, I can make it get so far but then it decides to light all the LEDs on the third and fourth shift registers really confused why that would be unless I need to start from the last and move to the first!
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

by adafruit_support_mike on Mon Apr 22, 2013 3:03 pm

If you're talking about this section:

Code: Select all | TOGGLE FULL SIZE
//  /////////////////////////////////////////////////////
// //Run final Sequence1
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal1();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence2
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal2();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence3
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal3();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence4
// /////////////////////////////////////////////////////

if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal4();
  ledPin = HIGH;
}

It looks like you have a copy/paste error. The test that triggers sequenceFinal3() is the same as the one that triggers sequenceFinal1(). Same for sequenceFinal2() and sequenceFinal4():

Code: Select all | TOGGLE FULL SIZE
//    these test for sr1Colour == 1 and sr2Coulour == 1
if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal1();
  ledPin = HIGH;
}
if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal3();
  ledPin = HIGH;
}


//    these test for sr1Colour == 1 and sr2Coulour == 2
if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal2();
  ledPin = HIGH;
}
if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal4();
  ledPin = HIGH;
}
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10841
Joined: Thu Feb 11, 2010 2:51 pm