Having trouble debugging my LED/ shaft encoder code

Hi, could someone help me figure out what I did wrong here. I am new to arduino programming but I think I am having trouble with my for loops.

This project uses the code supplied with bliptronics programmable RGB leds. I modified it to do what I need but its not working to good. What I have is a strip of 16 leds rotating around a shaft. I have a photointerrupter and encoder wheel on it. The encoder wheel has 40 gaps what I am trying to do is to trigger all green LED’s when the motore spins the encoder and it crosses the first gap. Then it should turn off all leds for the rest of the revolution and trigger all the green leds again every time it passes gap 40 and starts a new rotation (giving me a vertical line of leds at 0 degrees and no light the rest of the rotation)

What I am getting is all green led’s always on all of the time… my 0 to 80 counter does not seem to be updating so its always zero and thus all leds go on all the time… The rest of the code works fine. the latch() function works and so does the setup() and sendbyte() functions. the problem is in the loop()… any help would be appreciated…

// Example code to use with LED strip with 1606 controller chip
// Code by Ben Moyes, bliptronics.com November 2010
// Please give credit or buy some LEDs from me!

// *       L2ggrrbb
// *       00 - LED off
// *       01 - LED on (max bright)
// *       10 - LED fade up   (start at min bright)
// *       11 - LED fade down (start at max bright)

// Set which pins you will use to connect to the LED strip, you need 4 digital pins.
#define SIPIN 2
#define DIPIN 3
#define CLKPIN 4
#define LATCHPIN 5

#define LEDS 16    //Set this to the number of LEDs in your strip
#define CLOCK_DELAY (LEDS * 250)/1000  //This is the required delay between sending a bit and the clock line being fired.
                                        //You can tweak this value lower until you find LEDs on the end of your chain misbehaving
                                        //In fact, if you are running less than a few hundred LEDs you can get away with zero here.
                                        //250nSec per LED is the specified value for the chip.

const int  encoderPin = 8;    // the pin that the encoder is attached to
int encoderState = 0;         // current state of the encoder.  encoder wheel has 40 slots and 40 solid spaces between slots
                              // a "0" means photointerrupter is not blocked... "1" means photointerrupter is being blocked by encoder wheel
int prevEncoderState = 0;     // previous state of the encoder

// These are the instructions that can be loaded for each LED in the strip.
// You must OR a Command or Commandx2 with any combination of the color operations.
// eg Command | BlueOn | RedDown will make an LED fad from Purpple to Blue.
// eg Commandx2 | GreenUp will make an LED fad UP to Green at double speed.
/* #define Command B10000000 
#define Commandx2 B11000000 // Use this one to make dimming twice as fast. this code was commented out of code because it is not being used in this sketch
#define BlueOff B00000000   // it was left here as reference to get the binary codes to drive the led's
#define BlueOn B00010000 
#define BlueUp B00100000 
#define BlueDown B00110000 
#define RedOff B00000000 
#define RedOn B00000100 
#define RedUp B00001000 
#define RedDown B00001100 
#define GreenOff B00000000 
#define GreenOn B00000001 
#define GreenUp B00000010 
#define GreenDown B00000011 
*/


void setup()
{
  //Set up our pins to connect to the LED strip.
  pinMode(encoderPin, INPUT);
  pinMode(SIPIN, OUTPUT);
  pinMode(DIPIN, OUTPUT);
  pinMode(CLKPIN, OUTPUT);
  pinMode(LATCHPIN, OUTPUT);
  digitalWrite(SIPIN, LOW);
  digitalWrite(DIPIN, LOW);
  digitalWrite(CLKPIN, LOW);
  digitalWrite(LATCHPIN, LOW);
}  

void loop()
{
  int encoderStep; // This is a number from 0 to 79 relates to each time the shaft encoder changes state per revolution.
  int ledNumber;   // There are 16 leds in this project. ledNumber is an integer from 0-15 corresponding to the individual LED's
  encoderState = digitalRead(encoderPin); // reads encoder state
   if (encoderState != prevEncoderState){ //Is there a change from High to LOW or from LOW to HIGH in the encoder output???
         if ( encoderStep==0) {// encoderstep 0 is the first step in encoder wheel.  If the encoder is at "0" then light up all 16 LED's
               for(ledNumber=0;ledNumber < 16; ledNumber++) {
                     SendByte(B10000000|B00000001);  
                     latch(); 
               }
               encoderStep=encoderStep+1; // step encoder position counter by 1
         } 
         else { //if encoderStep doesnt equal 0 then turn off all LED's
              for(ledNumber=0;ledNumber < 16; ledNumber++) {
                  SendByte(B10000000|B00000000);
              }
              encoderStep=encoderStep+1;  //step encoder position counter by 1
         }
   }
   if (encoderStep==80) { //check to see if encoder has done a full turn (80) if yes, then reset the counter for the next rotation
       encoderStep=0;
   }
   prevEncoderState = encoderState;  //update the previous value of the encoder with the current one      
}


void SendByte(unsigned char it)
{
  //Send out one byte, don't forget to LATCH it by calling latch()
  //Note that for LARGE number of LEDs you may need to slow things down a little here.  
  digitalWrite(CLKPIN, LOW);

  char x;
     for(x=0;x < 8; x++)
    { 
    if(B10000000 & it)
      digitalWrite(DIPIN, HIGH); 
    else
      digitalWrite(DIPIN, LOW); 
    it = it<<1;
    // Wait here needs to be 250 x Number of LEDs nano seconds to allow data to propagate before being clocked in
    delayMicroseconds(CLOCK_DELAY);
    digitalWrite(CLKPIN, HIGH);
    digitalWrite(CLKPIN, LOW);

    }
}

void latch()
{
  digitalWrite(LATCHPIN, HIGH);
  delayMicroseconds(1);  // spec sheet specifies minimum latch pulse of 1us
  digitalWrite(LATCHPIN, LOW);
}

It doesn't appear that you are using the serial pins, so, why are there no Serial.begin(), Serial.print(), or Serial.println() statements? These are invaluable for debugging.

Does the encoder actually work? Serial.print(encoderState) and find out.

What value are you actually sending out using SendByte? Serial.print(it) and find out.

I see that the clock pin is set HIGH and immediately set LOW. Does it need to be HIGH for some period of time?

Sorry I did use serial to debug my photointerrupter and it works good. I also stripped the code down to just sending the led's signals and they light up and turn off fine (B10000000 | B00000001 turns on green) , (B10000000|B00000000 turns off the led)... the latch() function is like hitting [ENTER] and processes the above commands

I tried printing out my encoderStep variable to the serial port and it always came up "0" even though the arduino was recognizing when the encoder switched HIGH or LOW.

Just as Paul said; Serial is helpful for debugging.
I rewrote the code a little bit and nothing seems to be wrong. It is going inside the for loop when turning LEDS off.

Perhaps you missed the latch(); in that else statement?

The rewritten code for debugging.

// Example code to use with LED strip with 1606 controller chip
// Code by Ben Moyes, bliptronics.com November 2010
// Please give credit or buy some LEDs from me!

// *       L2ggrrbb
// *       00 - LED off
// *       01 - LED on (max bright)
// *       10 - LED fade up   (start at min bright)
// *       11 - LED fade down (start at max bright)

// Set which pins you will use to connect to the LED strip, you need 4 digital pins.
#define SIPIN 2
#define DIPIN 3
#define CLKPIN 4
#define LATCHPIN 5

#define LEDS 16    //Set this to the number of LEDs in your strip
#define CLOCK_DELAY (LEDS * 250)/1000  //This is the required delay between sending a bit and the clock line being fired.
//You can tweak this value lower until you find LEDs on the end of your chain misbehaving
//In fact, if you are running less than a few hundred LEDs you can get away with zero here.
//250nSec per LED is the specified value for the chip.

const int  encoderPin = 8;    // the pin that the encoder is attached to
int encoderState = 0;         // current state of the encoder.  encoder wheel has 40 slots and 40 solid spaces between slots
// a "0" means photointerrupter is not blocked... "1" means photointerrupter is being blocked by encoder wheel
int prevEncoderState = 0;     // previous state of the encoder

// These are the instructions that can be loaded for each LED in the strip.
// You must OR a Command or Commandx2 with any combination of the color operations.
// eg Command | BlueOn | RedDown will make an LED fad from Purpple to Blue.
// eg Commandx2 | GreenUp will make an LED fad UP to Green at double speed.
/* #define Command B10000000 
 #define Commandx2 B11000000 // Use this one to make dimming twice as fast. this code was commented out of code because it is not being used in this sketch
 #define BlueOff B00000000   // it was left here as reference to get the binary codes to drive the led's
 #define BlueOn B00010000 
 #define BlueUp B00100000 
 #define BlueDown B00110000 
 #define RedOff B00000000 
 #define RedOn B00000100 
 #define RedUp B00001000 
 #define RedDown B00001100 
 #define GreenOff B00000000 
 #define GreenOn B00000001 
 #define GreenUp B00000010 
 #define GreenDown B00000011 
 */

int encoderStep = 0;

void setup()
{
  //Set up our pins to connect to the LED strip.
  pinMode(encoderPin, INPUT);
  pinMode(SIPIN, OUTPUT);
  pinMode(DIPIN, OUTPUT);
  pinMode(CLKPIN, OUTPUT);
  pinMode(LATCHPIN, OUTPUT);
  digitalWrite(SIPIN, LOW);
  digitalWrite(DIPIN, LOW);
  digitalWrite(CLKPIN, LOW);
  digitalWrite(LATCHPIN, LOW);
  Serial.begin(9600);
}  

void loop()
{
  encoderState++;
  //Serial.println(encoderState);
  //Serial.println(encoderStep);
  delay(1000);
  if (encoderState != prevEncoderState){ //Is there a change from High to LOW or from LOW to HIGH in the encoder output???
    if ( encoderStep==0) {// encoderstep 0 is the first step in encoder wheel.  If the encoder is at "0" then light up all 16 LED's
      for(int ledNumber=0;ledNumber < 16; ledNumber++) {
        SendByte(B10000000|B00000001);  
        latch(); 
      }
      encoderStep=encoderStep+1; // step encoder position counter by 1
    } 
    else { //if encoderStep doesnt equal 0 then turn off all LED's
      for(int ledNumber=0;ledNumber < 16; ledNumber++) {
        SendByte(B10000000|B00000000);
        Serial.println(ledNumber);
      }
      encoderStep=encoderStep+1;  //step encoder position counter by 1
    }
  }
  if (encoderStep==80) { //check to see if encoder has done a full turn (80) if yes, then reset the counter for the next rotation
    encoderStep=0;
  }
  prevEncoderState = encoderState;  //update the previous value of the encoder with the current one   
}


void SendByte(unsigned char it)
{
  //Send out one byte, don't forget to LATCH it by calling latch()
  //Note that for LARGE number of LEDs you may need to slow things down a little here.  
  digitalWrite(CLKPIN, LOW);

  char x;
  for(x=0;x < 8; x++)
  { 
    if(B10000000 & it)
      digitalWrite(DIPIN, HIGH); 
    else
      digitalWrite(DIPIN, LOW); 
    it = it<<1;
    // Wait here needs to be 250 x Number of LEDs nano seconds to allow data to propagate before being clocked in
    delayMicroseconds(CLOCK_DELAY);
    digitalWrite(CLKPIN, HIGH);
    digitalWrite(CLKPIN, LOW);

  }
}

void latch()
{
  digitalWrite(LATCHPIN, HIGH);
  delayMicroseconds(1);  // spec sheet specifies minimum latch pulse of 1us
  digitalWrite(LATCHPIN, LOW);
}