Shift Register Problems

Hello, and thanks for reading this.

Over the past few days I have been learning how to use a 8 bit shift register using different tutorials and my previous knowledge of binary. I have been working on using the code to use 8 leds as a "Cylon Eye" if you must know and for each step I wanted three leds to be lit at a time. I found the magic number was 7, then 7 * 2 = 14, 14 *2 = 28, and so on up until it hits 224 then the code would start to divide by two.

int latch = 8; //middle
int clock = 12; //bottom
int data = 11; //top
byte leds = 7;
byte o = 0;
int l = 7;
int run = 0;

void setup(){
  pinMode(latch, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(data, OUTPUT);
  Serial.begin(9600);
}

void loop(){
   if(leds == 7){
     run = 0;
   }
   if(leds == 224){  // I think the problem is here If I change it to 112 it works fine it only uses 7 leds though
     run = 1;
   }
   //Right to Left
   if(run == 0){
     update();
     delay(125);
     bitClear(o, 0); 
     update2();
     Serial.println(leds);
     leds = leds * 2;
     delay(1);
   }
   //Left to Right
   if(run == 1){
     update();
     delay(125);
     bitClear(o, 0);
     update2();
     Serial.println(leds);
     leds = leds / 2;
     delay(1);
   }
}
     
  ///TURNS LEDS OFF
void update2(){
  digitalWrite(latch, LOW);
  shiftOut(data, clock, LSBFIRST, o);
  digitalWrite(latch, HIGH);
}

///TURNS LEDS ON
void update(){
 digitalWrite(latch, LOW);
 shiftOut(data, clock, LSBFIRST, leds);
 digitalWrite(latch, HIGH); 
}

If you look I put a comment next to where I think the error is

When the program hits 224 all the lights turn off instad of the three proper lights turning on and the program seems to 'stutter' then it goes back and works properly. However when I change the value to 112 instead of 224 it works perfectly just with 7 leds and not 8.

All I have figured out is that it should work, but I seem to be doing some thing wrong and I'm not sure what it is. I have every thing plugged in properly and such.

If you have any Idea as to what my problem is please tell me or give me a hint. Also if you have advice to how I could do it a better way I would love the extra help.

Thank you all very much.

-Cayoke

You create the value 224, but it never gets shifted out. I might take a simpler approach - put the combos in an array and just cycle thru the array, blink without delay style:

byte ledsArray [] = {0b00000111,0x0001110, 0x00011100, 0x00111000, 0b01110000, 0b11100000,0b01110000, 0b00111000,0b00011100, 0b00001110,};

void loop(){
currentTime = millis();
if ( (currentTime - previousTime) >= duration){
previousTime = previousTime + duration; // all time variables are  unsigned long
x=x+1;
if (x== 10){ x=0;}
  digitalWrite(latch, LOW);
  shiftOut(data, clock, LSBFIRST, ledsArray[x]);
  digitalWrite(latch, HIGH);
} // end time check
//
// do other stuff, read buttons, check serial data, etc
//
} // end loop

Alrighty, I'll have to sit down with that code for a bit. I have not done much work with arrays, however I kind of see what you are going at with it. However with the previous code if the value isnt getting shifted out I guess I'll add that to the if statement like this?

if(leds == 224){
     run = 1;
     digitalWrite(latch, LOW);
      shiftOut(data, clock, LSBFIRST, 224);
      digitalWrite(latch, HIGH); 
   }

I'll have to try it tomorrow. Thanks for the suggestion! All the more reason I need to work on arrays more.

Just for fun (untested):

#define LATCH   8
#define CLOCK  12
#define DATA   11

#define DELAY  125

uint8_t leds = 0b00000111;
uint8_t direction = 1;

void setLEDs()
{
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, LSBFIRST, leds);
  digitalWrite(LATCH, HIGH);
}

void setup()
{
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void loop()
{
  setLEDs();

  if (direction)
    leds <<= 1;
  else
    leds >>= 1;

  if ((leds & 1) != 0 || (leds & 0x80) != 0)
    direction = !direction;

  delay(DELAY);
}

int2str I uploaded your code to the board and it still does the same thing my code does so I decided to look into it further to make sure my Register was not messed up. I tried a diffrent register and no luck, then I decided to double check the arduino shift out tutorial to make sure I did it right and I noticed this.

From now on those will be refered to as the dataPin, the clockPin and the latchPin respectively. Notice the 0.1"f capacitor on the latchPin, if you have some flicker when the latch pin pulses you can use a capacitor to even it out.[/quote]

Think that may be the issue? It only blinks at that one instance and since int2str’s code does the same I’m hoping it is the lack of a capacitor and not my board.

Thanks for your help so far.

  • Cayoke

cayoke: Think that may be the issue? It only blinks at that one instance and since int2str's code does the same I'm hoping it is the lack of a capacitor and not my board.

Schematic? Little video clip?

DO NOT PUT ANY CAPS ON CONTROL LINES!!!

This code has the same problem as yours - the next to last data either does not get put into place, or it is tested and direction is changed before it gets shifted out.

Also like yours, it uses delay(125) so the processor can do nothing els e in between LED updates.

CrossRoads: This code has the same problem as yours - the next to last data either does not get put into place, or it is tested and direction is changed before it gets shifted out.

Please explain? I've tested this code last night and it's working 100%.

Also like yours, it uses delay(125) so the processor can do nothing els e in between LED updates.

So? The OP has not indicated yet that his code needs to do anything else. This also seems unrelated to his problem.

OP had said yours was not working as intended.

I am trying to introduce blink without delay whenever it seems suited.

CrossRoads:
OP had said yours was not working as intended.

You seemed to indicate it’s a problem with the code…

I really would like to see a wiring diagram and a short video of the problem to be better able to help.

I have a wiring diagram for you here. The banded wires go to the LEDS while solid wires go from the register to the controller.

I will try and get a short video up.