Im using a rotary encoder to change the brightness of the Blinkm, the blinkm works, it sets to red fine, but the interrupt is not working. the encoder is pins 2 and 3 and common on the encoder is GND. Any ideas?
#include <Wire.h>
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
int value = 0;
void setup() {
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send("o");
Wire.endTransmission();
Wire.beginTransmission(0x09);
Wire.send("c");
Wire.send(0xff); // value for red channel
Wire.send(0x00); // value for blue channel
Wire.send(0x00); // value for green channel
Wire.endTransmission(); // leave I2C bus // debug - remember to comment out
// before final program run
}
void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
}
void doEncoder(){
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos - 1; // CCW
}
else {
encoder0Pos = encoder0Pos + 1; // CW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
if ( encoder0Pos < 0){
encoder0Pos = encoder0Pos + 255;
}
else if ( encoder0Pos > 255){
encoder0Pos = encoder0Pos -255;
}
value = encoder0Pos;
// set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send("c"); // ‘c’ == fade to color
Wire.send(value); // value for red channel
Wire.send(value); // value for blue channel
Wire.send(value); // value for green channel
Wire.endTransmission(); // leave I2C bus // debug - remember to comment out
}