0

I'm trying to use the sketch bellow to work with my WeMos D1 mini (ESP8266 Arduino platform) together with Rotary Encoder KY-040 or MJKDZ, and it's not working for some reason, but the same sketch is working great on the Arduino Uno (with different pin assignment - 2, 3, 4).

Can someone explain to me what is the issue with the sketch?

const int PinA = 14; // Used for generating interrupts using CLK signal
const int PinB = 12; // Used for reading DT signal
const int PinSW = 13; // Used for the push button switch
int lCnt = 0; // Keep track of last rotary value
volatile int vPos = 0; // Updated by the ISR (Interrupt Service Routine)
void isr_event () {
 static unsigned long lIsrTmr = 0; // Last Interrupt time
 unsigned long IsrTmr = millis(); // Interrupt time
 // If interrupts come faster than 5ms, assume it's a bounce and ignore
 if (IsrTmr - lIsrTmr > 5) {
 if (digitalRead(PinB) == LOW)
 {
 vPos++ ; // Could be +5 or +10
 }
 else {
 vPos-- ; // Could be -5 or -10
 }
 // Restrict value from 0 to +100
 vPos = min(10, max(0, vPos));
 // Keep track of when we were here last (no more than every 5ms)
 lIsrTmr = IsrTmr;
 }
}
void setup()
{
 Serial.begin(9600);
 // Rotary pulses are INPUTs
 pinMode(PinA, INPUT);
 pinMode(PinB, INPUT);
 pinMode(PinSW, INPUT_PULLUP); // Switch is floating so use the in-built PULLUP so we don't need a resistor
 attachInterrupt(digitalPinToInterrupt(PinA), isr_event, LOW); // Attach the routine to service the interrupts
 Serial.println(F("Starting...")); // Ready to go!
}
void loop()
{
 // Is someone pressing the rotary switch?
 if ((!digitalRead(PinSW))) {
 vPos = 0;
 while (!digitalRead(PinSW))
 delay(10);
 }
 // If the current rotary switch position has changed then update everything
 if (vPos != lCnt) {
 // Write out to serial monitor the value and direction
 Serial.println(vPos);
 // Keep track of this new value
 lCnt = vPos ;
 }
}
Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Jul 17, 2018 at 10:28
5
  • 1
    Define "not work"... That phrase can mean anything from "jittery movement" to "the whole thing exploded and burned off my eyebrows". Commented Jul 17, 2018 at 11:09
  • @Majenko No, didn't exploded just not getting any output to the Serial monitor. Commented Jul 17, 2018 at 11:28
  • 1
    Not even the initial "Starting" message? Commented Jul 17, 2018 at 11:29
  • Yes, I get the message "Starting..." Commented Jul 17, 2018 at 11:33
  • To be sure 100%, I recompiled for Arduino Uno as we speak and tested with both Rotary Encoder type ( KY-040 and MJKDZ) and it's working as expected. But as I said, it's not outputing nothing except "Starting..." on ESP8266 platform. @Majenko Commented Jul 17, 2018 at 11:39

1 Answer 1

1

The ESP8266 doesn't know what a "LOW" interrupt is. You will have to change your code to use FALLING, RISING or CHANGE interrupts only.

answered Jul 17, 2018 at 11:36
3
  • Tried all of them, the FALLING gives me the best result, also I made pinMode(PinA, INPUT); and removed digitalPinToInterrupt(), and made PinB to be attachInterrupt, but the output is weird, there are duplicates and messed counts too. link Commented Jul 17, 2018 at 12:18
  • perhaps your debounce isn't - perhaps this code can help Commented Jul 18, 2018 at 4:15
  • @JaromandaX tried that to and it's working really bad, skipping and messing numbers up Commented Jul 19, 2018 at 17:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.