Go Down

Topic: Rotary Encoder  (Read 474 times) previous topic - next topic

Lamex5

I want to use two rotary encoders in my project. please, can someone tell me what did ı do wrong?

Lamex5

ı can't read any value from the LCD screen.

econjack

Use code tags (i.e., the "<" and "/>") to present you code in all posting on these Forums. Just click on symbols in the edit block and place your source code between the tags:

Code: [Select]

#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);

//BİRİNCİ ENKODER
 
// 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);
}


Many readers here don't like to open files with unknown content even if they end in INO. Also, the code tags properly display your code even if control characters are present, plus it will use less real estate on the screen.

Go Up