I've written a rotary encoder demo sketch that works perfectly on a Nano using pins D2 and D3, but when I try to run it on a D1 Mini, using pins D1 and D2, I get this repeatedly over the serial monitor (at 115600):
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld
The D1 Mini works fine on other sketches. I know it's something dumb, but I just can't see it. Here's the sketch:
const int aPIN = 1; // encoder pins
const int bPIN = 2;
const byte encoderType = 0; // encoder with equal # of detents & pulses per rev
//const byte encoderType = 1; // encoder with pulses = detents/2. pick one, commment out the other
const int THRESH =(4-(2*encoderType)); // transitions needed to recognize a tick - type 0 = 4, type 1 = 2
const byte ZEERO = 0x80; // byte data type doesn't do negative
volatile int currentValue = 0;
int oldValue = 0;
byte CURRENT; // the current state of the switches
byte INDEX = 15; // Index into lookup state table
byte TOTAL = 0;
// Encoder state table - there are 16 possible transitions between interrupts
int ENCTABLE[] = {0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0};
void setup() {
Serial.begin(9600);
pinMode(aPIN, INPUT_PULLUP); // set up encoder pins as INPUT-PULLUP
pinMode(bPIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(aPIN), Encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(bPIN), Encoder, CHANGE);
}
void loop() {
if(currentValue != oldValue) { // serial print current value when it changes
Serial.println(currentValue);
oldValue = currentValue;
}
}
void Encoder() { // pin change interrupts service routine. interrupts
// automatically disabled during execution
INDEX = INDEX << 2; // Shift previous state left 2 bits (0 in)
if(digitalRead (aPIN)) bitSet(INDEX,0); // If aPIN is high, set INDEX bit 0
if(digitalRead (bPIN)) bitSet(INDEX,1); // If bPIN is high, set INDEX bit 1
CURRENT = INDEX & 3; // CURRENT is the two low-order bits of INDEX
INDEX &= 15; // Mask out all but prev and current
// INDEX is now a four-bit index into the 16-byte ENCTABLE state table
TOTAL += ENCTABLE[INDEX]; //Accumulate transitions
if((CURRENT == 3) || ((CURRENT == 0) && encoderType)) { //A valid tick can occur only at a detent
if(TOTAL == (ZEERO + THRESH)) {
currentValue++;
}
else if(TOTAL == (ZEERO - THRESH)) {
currentValue--;
}
TOTAL = ZEERO; //Always reset TOTAL to 0x80 at detent
}
}