Rotary Encoder

I want to use two rotary encoders in my project but ı couldn't. Please, can someone tell me what did ı do wrong?

I added code in the topic.

RotaryEncoderInterrrupts_2S_.ino (3.82 KB)

Lamex5:
I want to use two rotary encoders in my project but ı couldn't. Please, can someone tell me what did ı do wrong?

I added code in the topic.

So what was the problem ?

ı can't read any value from the LCD screen. But when ı use one of the rotary encoders ı can read value.

Rather than making people download your code, post it with with CODE TAGS.

What Arduino board are you using?

Arduino Mega 2560 Pro.

I understood thank you. I'm new at this, and I'm going to pay attention.

#include <LiquidCrystal.h>
const int rs = 46, en = 47, d4 = 48, d5 = 50, d6 = 52, d7 = 53;
int dt1=1;
int dt2=1;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//first ENCODER

// Used for generating interrupts using CLK signal(A)
const int PinA1 =18;

// Used for reading DT signal(A)
const int PinB1 = 30;

// Used for the push button switch(A)
const int PinSW1 = 32;

// Keep track of last rotary value(A)
int lastCount1 = 0;

// Updated by the ISR (Interrupt Service Routine)(A)
volatile int virtualPosition1 = 0;

//İKİNCİ ENKODER

// Used for generating interrupts using CLK signal(A)
const int PinA2 =19;

// Used for reading DT signal(A)
const int PinB2 = 10;

// Used for the push button switch(A)
const int PinSW2 = 33;

// Keep track of last rotary value(A)
int lastCount2 = 0;

// Updated by the ISR (Interrupt Service Routine)(A)
volatile int virtualPosition2 = 0;

void firstencoder () {
static unsigned long lastInterruptTime1 = 0;
unsigned long interruptTime1 = millis();
// If interrupts come faster than 5ms, assume it's a bounce and ignore
if (interruptTime1 - lastInterruptTime1 > 5) {
if (digitalRead(PinB1) == LOW)
{
virtualPosition1-=dt1 ; // Could be -5 or -10
}
else {
virtualPosition1+=dt1 ; // Could be +5 or +10
}
// Restrict value from 0 to +100
//virtualPosition = min(100, max(0, virtualPosition));
}
// Keep track of when we were here last (no more than every 5ms)
lastInterruptTime1 = interruptTime1;
}

void secondencoder () {
static unsigned long lastInterruptTime2 = 0;
unsigned long interruptTime2 = millis();

// If interrupts come faster than 5ms, assume it's a bounce and ignore
if (interruptTime2 - lastInterruptTime2 > 5) {
if (digitalRead(PinB2) == LOW)
{
virtualPosition2-=dt2 ; // Could be -5 or -10
}
else {
virtualPosition2+=dt2 ; // Could be +5 or +10
}

// Restrict value from 0 to +100
//virtualPosition = min(100, max(0, virtualPosition));
}
// Keep track of when we were here last (no more than every 5ms)
lastInterruptTime2 = interruptTime2;
}

void setup() {
// Just whilst we debug, view output on serial monitor
//Serial.begin(115200);

lcd.begin(16, 2);

// Rotary pulses are INPUTs
pinMode(PinA1, INPUT);
pinMode(PinB1, INPUT);
pinMode(PinA2, INPUT);
pinMode(PinB2, INPUT);

// Switch is floating so use the in-built PULLUP so we don't need a resistor
pinMode(PinSW1, INPUT_PULLUP);
pinMode(PinSW2, INPUT_PULLUP);

// Attach the routine to service the interrupts
attachInterrupt(digitalPinToInterrupt(PinA1), firstencoder , LOW);
attachInterrupt(digitalPinToInterrupt(PinA2), secondencoder, LOW);

// Ready to go!
// Serial.println("Start");
}
void loop() {

// Is someone pressing the rotary switch?
if ((!digitalRead(PinSW1))) {
if(dt1==1)dt1=5;else dt1=1;;
while (!digitalRead(PinSW1))
delay(10);
//Serial.println("Reset");
}
if ((!digitalRead(PinSW2))) {
if(dt2==1)dt2=5;else dt2=1;;
while (!digitalRead(PinSW2))
delay(10);
//Serial.println("Reset");
}

// If the current rotary switch position has changed then update everything
if (virtualPosition1 != lastCount1) {

// Write out to serial monitor the value and direction
lcd.setCursor(0,0);
lcd.print("ENK1:");
lcd.print(virtualPosition1);
lcd.print(" ");
// Keep track of this new value
lastCount1 = virtualPosition1 ;
}
if (virtualPosition2 != lastCount2) {

// Write out to serial monitor the value and direction
lcd.setCursor(0,8);
lcd.print("ENK2:");
lcd.print(virtualPosition2);
lcd.print(" ");
// Keep track of this new value
lastCount2 = virtualPosition2 ;
}
delay(100);
}

  1. Next time use code tags.

  2. You don't need to debounce an encoder. If it does bounce then you just get a short sequence of up-down-up-down-up until it settles on the final position.

  3. Initialize your lastCount variables to something different to the initial value of position. That will force it to update the screen on the first iteration of loop().

  4. Print something to the screen in setup() like "starting" or the version number of this program.

thank you.