The Final Code for the Breakout

This final code example sets up all the connections and interrupts.


After the start, it lightens up half of the Led in the ring to show we are alive and the RGB colors are varied through the rainbow using the wheel function adafruit has provided.


Once you turn the encoder, the Led will follow the rotation.


Pressing the switch will light up all Led until the knob is turned again


Enjoy!

#include 
#include 

int spin = nP[RIGHT][3];

volatile int encoderPin1 = nP[RIGHT][4];        // pin connected to INT-1 by default
volatile int encoderPin2 = nP[RIGHT][5];
volatile int number = 0; 
volatile boolean swPressed = false; 
// used in loop and both interrupt routines
int oldnumber = number;
int rpin = nP[RIGHT][2];
int gpin = nP[RIGHT][1];
int bpin = nP[RIGHT][0];
int rval;
int gval;
int bval;
int enPin = nP[LEFT][3];  // Shift registers' Output Enable pin
int latchPin = nP[LEFT][4];  // Shift registers' rclk pin
int clkPin = nP[LEFT][5];  // Shift registers' srclk pin
int clrPin = nP[LEFT][6];  // shift registers' srclr pin
int datPin = nP[LEFT][7];  // shift registers' SER pin

void setup(){
        Serial.begin(9600);
        pinMode(rpin, OUTPUT);
        pinMode(gpin, OUTPUT);
        pinMode(bpin, OUTPUT);
        pinMode(enPin, OUTPUT);  
        pinMode(latchPin, OUTPUT);
        pinMode(clkPin, OUTPUT);
        pinMode(clrPin, OUTPUT);
        pinMode(datPin, OUTPUT);
        digitalWrite(enPin, LOW);
        digitalWrite(latchPin, LOW);
        digitalWrite(clkPin, LOW);
        digitalWrite(clrPin, HIGH);
        digitalWrite(datPin, LOW);

        pinMode(encoderPin1, INPUT);
        digitalWrite(encoderPin1, HIGH);
        pinMode(encoderPin2, INPUT);
        digitalWrite(encoderPin2, HIGH);
        attachInterrupt(1, isr_A, FALLING);
        attachInterrupt(3, isr_SW, RISING);
        Serial.println("Starting Encoder");
        shiftOut16(0x00FF);
}

void loop(){
        int val;
        for (int i=0; i<256; i++){
                Wheel(i);
                SoftPWMServoPWMWrite(rpin, rval);
                SoftPWMServoPWMWrite(gpin, gval);
                SoftPWMServoPWMWrite(bpin, bval);
                delay(30);
                if (oldnumber != number){
                        Serial.print(number);
                        oldnumber = number;
                        LedSingle((uint16_t)number);
                        Serial.println();
                }
                if (swPressed) {
                        shiftOut16(0xFFFF);
                        swPressed = false;
                }
        }
}

void isr_A(){
        delay(3);                                                               // Debounce time
        // Trade off bounce vs missed counts
        int LSB = digitalRead(encoderPin1);
        int MSB = digitalRead(encoderPin2);

        if(LSB == LOW){                                                 // A still LOW ?
                if(MSB == HIGH) number++;
                if(MSB == LOW)  number--;
        }
}

void isr_SW(){                              // SW went LOW
        swPressed = true;
}

void Wheel(byte WheelPos) {
        if(WheelPos < 85) {
                rval = WheelPos * 3;
                gval = 255 - WheelPos * 3;
                bval = 0;
        } 
        else if(WheelPos < 170) {
                WheelPos -= 85;
                rval = 255 - WheelPos * 3;
                gval = 0;
                bval = WheelPos * 3;
        } 
        else {
                WheelPos -= 170;
                rval = 0;
                gval = WheelPos * 3;
                bval = 255 - WheelPos * 3;
        }
}

void LedSingle(int pos) {
        int lpos;
        uint16_t lout;

        lpos = pos % 16;
        lout = 1 << lpos;
        Serial.print("  ");
        Serial.print(lpos);
        Serial.print("  ");
        Serial.print(lout);
        shiftOut16(lout);
}

void shiftOut16(uint16_t data)
{
        byte datamsb;
        byte datalsb;

        datamsb = (data&0xFF00)>>8;  // mask out the MSB and shift it right 8 bits
        datalsb = data & 0xFF;  // Mask out the LSB

        Serial.print("  ");
        Serial.print(datamsb,DEC);
        Serial.print("  ");
        Serial.print(datalsb,DEC);

        digitalWrite(latchPin, LOW);  // first send latch low
        shiftOut(datPin, clkPin, MSBFIRST, datamsb);
        shiftOut(datPin, clkPin, MSBFIRST, datalsb);
        digitalWrite(latchPin, HIGH);  // send latch high to indicate data is done sending
}