Trouble with Code for Shift Register and LEDs

Hi LMR. After stumbling onto this site last month I fell in love with the idea robotics. I bought a Arduino starter kit last week, to start learning the basics.  One of the tutorials I did involved counting in binary using a shift register and 8 LEDs. I didn't really understand the code for the tutorial or how the shift operator works so I started with something more basic.

So far using the shift register, I can control the LEDs individually, use loops to make the LEDs cycle through and do night rider effect. Now i'm trying to use bitwise operators to get the same cycling effect as I did with regular loops. I almost have it but my last LED is being skipped. I did some troubleshooting and I think its because the counter is being reset before the 128bit led is turning on. I just can't seem fix it. Below is my code. If knows how i can fix this it would be appreciated :)

 

 

// Pin Variables

int data = 2; 

int clock = 3;

int latch = 4;

 

// Variable to represent LEDs' states

byte LED = 1;

 

// Pot variables

int potPin = 0;

int potVal = 0;

 

// Pin initialization & Serial start

void setup()

{

  Serial.begin(9600);

  pinMode(data, OUTPUT);

  pinMode(clock, OUTPUT);  

  pinMode(latch, OUTPUT);  

 

}

 

 

 

void loop()                     

{

  digitalWrite(latch, LOW);  // Set latch low so data can be written

 

// Resets LED states once last LED has be turned on.

 if (LED >= 128)

    LED = 1;

 

  // Loops for all 8 bits

  for (int i = 0; i < 8 ; i ++) 

  {

    if(LED & (1 << (i)))

    {

      digitalWrite(data, HIGH);

      Serial.print(1);

    }

    else

    {

      digitalWrite(data, LOW);

      Serial.print(0);

    }

 

    digitalWrite(clock, HIGH);  // Load bit(i)

    delay(5);

    digitalWrite(clock, LOW);   

  }

 

  LED = (LED << 1);  // binary offset by 1 to the left

 

  digitalWrite(latch, HIGH); // Sends data 

 

  potVal = analogRead(potPin);

  delay(potVal);

   Serial.println();

 

 

}

 

Yup, you nailed it

Now you know, you can fix it!

Oh well, a bit more help than: Your loop can be summarized like this (always make these when coding something bigger than a blinky LED).

reset pattern if it equals 10000000
show pattern
shift pattern

You could write that in full, since it repeats only eight seven times. Let’s include the bit patterns.

pattern = 00000001
show pattern = 00000001
shift pattern
show pattern = 00000010
shift pattern
show pattern = 00000100
shift pattern
show pattern = 00001000
shift pattern
show pattern = 00001000
shift pattern
show pattern = 00010000
shift pattern
show pattern = 00100000
shift pattern
show pattern = 01000000
shift pattern
show pattern = 10000000 nah let’s reset the pattern instead

Now let’s see what happens when we did not have a reset condition:
… (as above)
show pattern = 01000000
shift pattern
show pattern = 10000000
shift pattern
show pattern = 00000000
shift pattern
show pattern = 00000000
shift pattern
… (ad infinitum)

I suggest you change that reset condition. This means you may have to preset (PPPPreset!) your initial pattern with something (different).

 

I believe rik is saying …

there are 8 bits in a byte and the eighth bit’s value would be 127. Therefore, you will never see a LED light for the ninth bit or 128.

Except that I am not

%0000 0000  =    0   (I added a space to separate the nybbles)
%0000 0001  =    1
%0000 0010  =    2
%0000 0100  =    4
%0000 1000  =    8
%0001 0000  =   16
%0001 0000  =   32
%0100 0000  =   64
%1000 0000  =  128

127 decimal would be %0111 1111

Yes one byte (usually) has 8 bits. Make sure to use a byte type variable. A word type variable (usually) has twice the number of bits. This means that shifting a word with value 128 (to the left) will result in a value 256 instead of 0.

**My apologies **

I must have misunderstood what you were saying. … And that NEVER happens to me. :stuck_out_tongue: I will go sit in the corner now and offer knowledgeable information when I have it.

Thanks

Thanks for the help guys. I finally got it working last night.  I’m pretty busy with finals at university right now so I only get to tinker during study breaks :stuck_out_tongue: