for the life of me I cannot get the reprap encoder to fire off on an interrupt. can anyone take a look and see where my error is?
Code:
#include <PinChangeInterrupt.h>
#include "U8glib.h"
#define encoderPin1 31
#define encoderPin2 33
#define pinEncButt 35
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
U8GLIB_ST7920_128X64_1X u8g(23, 17, 16); // SPI Com: SCK = en = 23, MOSI = rw = 17, CS = di = 16;
void draw(void) {
u8g.setFont(u8g_font_fub11);
u8g.drawStr( 0, 12, "Test Encoder");
u8g.setFont(u8g_font_fub20);
char buf[8];
sprintf(buf, "%d %d",digitalRead(pinEncButt), encoderValue);
u8g.drawStr( 10, 40, buf);
u8g.setFont(u8g_font_6x12);
u8g.drawStr( 0, 63, "OK fine");
}
void updateEncoder(){
//Serial.println( digitalRead(encoderPin1) );
Serial.println("int hit");
int MSB = digitalRead(encoderPin1);
int LSB = digitalRead(encoderPin2);
u8g.drawStr( 0, 63, "f it");
int encoded = (MSB << 1) |LSB;
int sum = (lastEncoded << 2) | encoded;
if(sum==0b1101 || sum==0b0100 || sum==0b0010 || sum==0b1011) encoderValue++;
if(sum==0b1110 || sum==0b0111 || sum==0b0001 || sum==0b1000) encoderValue--;
lastEncoded = encoded; //store this value for next time
}
void setup(void) {
Serial.begin(9600);
if (u8g.getMode()==U8G_MODE_R3G3B2) { u8g.setColorIndex(255); }
else if (u8g.getMode()==U8G_MODE_GRAY2BIT) { u8g.setColorIndex(3); }
else if (u8g.getMode()==U8G_MODE_BW) { u8g.setColorIndex(1); }
else if (u8g.getMode()==U8G_MODE_HICOLOR) { u8g.setHiColorByRGB(255,255,255);}
pinMode(encoderPin1, INPUT_PULLUP);
pinMode(encoderPin2, INPUT_PULLUP);
pinMode(pinEncButt, INPUT_PULLUP);
attachPinChangeInterrupt(encoderPin1, updateEncoder, CHANGE);
attachPinChangeInterrupt(encoderPin2, updateEncoder, CHANGE);
Serial.println("start");
}
void loop(void) {
//Serial.println("enc-1= " + String(digitalRead(encoderPin1)));
//Serial.println("enc-2= " + String(digitalRead(encoderPin2)));
u8g.firstPage();
do { draw(); } while( u8g.nextPage() );
/delay(50);
}