I want to see the progress distance with the rotation of the Rotary Encoder

I want to see the progress distance with the rotation of the Rotary Encoder. I will connect the encoder to a coil with a diameter of 10cm. I want it to read the rotation speed of the coil and write it to the screen instantly in meters/minute. But no matter what I did, it didn't work correctly. I give the code below. What should I do? Can you help me?

#include <UTFT.h>
#include <TFT.h>
#include <RotaryEncoder.h>

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

#define PIN_IN1 A2
#define PIN_IN2 A3
#define IMPULSES_PER_ROTATION 360 // Bir turda kaç impuls olduğunu buraya girin
#define DISTANCE_PER_IMPULSE 0.02777777 // Her impulsun aldığı mesafeyi buraya girin (örneğin, 0.02777777 cm)

RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
unsigned long lastMillis = 0; // Son hesaplamanın yapıldığı zamanı takip etmek için kullanılır
int lastPos = 0; // Son pozisyonu saklar

UTFT myGLCD(CTE32HR, 38, 39, 40, 41, true);

void setup() {
  myGLCD.InitLCD();
  myGLCD.setFont(BigFont);
  Serial.begin(9600);
  while (!Serial);
  Serial.println("RotaryEncoder kütüphanesi testi.");
}

void loop() {
  myGLCD.clrScr();

  // Sol taraftan hizalı metinleri yazdır
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("1. KAT Hz (M/Dak):", 10, 30);
  myGLCD.print("2. KAT Hz (M/Dak):", 10, 70);
  myGLCD.print("3. KAT Hz (M/Dak):", 10, 110);

  // Burada encoder değerini okuyun (örnek olarak rastgele bir değer ekledim)
  encoder.tick();
  int newPos = encoder.getPosition();
  unsigned long currentMillis = millis();

  // Encoder değerini ekranda görüntüle
  if (currentMillis - lastMillis >= 10) {
    float metersPerMinute = ((newPos - lastPos) * (31.416 / 500)) * 60; // Metre/Dakika cinsinden hızı hesapla
    Serial.print("İlerleme Hızı (metre/dakika): ");
    Serial.println(metersPerMinute);
    lastMillis = currentMillis;
    lastPos = newPos;
}
  
  myGLCD.setColor(255, 255, 255);
  myGLCD.printNumI(newPos, 320, 30);
  myGLCD.printNumI(newPos, 320, 70);
  myGLCD.printNumI(newPos, 320, 110);

  //delay(100); // Ekranın hızlıca güncellenmesini engellemek için bir gecikme ekledik
  // loop fonksiyonun sonu
}

Maybe it will be easier if you used getRPM() ?

Perhaps a higher baud rate would help - you are trying to print every 10ms at a low baud rate.

In detail: what does it do, and how is that different from what it should do?

Your Serial prints are indeed too frequent. For a human looking at it, twice a second (every 500 ms) is normally more than enough, humans are too slow to process information much faster anyway. At 9600 bps it takes about 1 ms to send one character, you're trying to print 30-odd characters every 10 ms. That's not going to work.

You define DISTANCE_PER_IMPULSE and IMPULSES_PER_ROTATION yet you don't use them in your code.

Assuming you're using an Uno or similar: A2 and A3 only have pin change interrupts; does your RotaryEncoder use those interrupts? If not you're going to lose tons of ticks - especially with the code effectively all the time being stuck communicating on Serial.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.