From 87a06ecc655b00ca1e23b82aec20eaf89f7e88e9 Mon Sep 17 00:00:00 2001 From: pcgaldo Date: Wed, 19 Jun 2024 16:51:43 +0200 Subject: [PATCH] Atualizar tachometer_v1.ino --- tachometer_v0.ino => tachometer_v1.ino | 41 +++++++++++++++++++------- 1 file changed, 30 insertions(+), 11 deletions(-) rename tachometer_v0.ino => tachometer_v1.ino (78%) diff --git a/tachometer_v0.ino b/tachometer_v1.ino similarity index 78% rename from tachometer_v0.ino rename to tachometer_v1.ino index 88c620c..70ea905 100644 --- a/tachometer_v0.ino +++ b/tachometer_v1.ino @@ -39,6 +39,7 @@ unsigned long lastTime = 0; // To store the last time when RPM was calculated unsigned long currentTime = 0; // To store the current time float rpm = 0; // Variable to store the RPM value float speed_m_min = 0; // Variable to store the speed in meters per minute +float lastSpeed_m_min = -1; // Variable to store the last speed value // Constants for encoder and roller const int pulsesPerRevolution = 1000; // Pulses per revolution of the encoder @@ -53,6 +54,14 @@ void setup() { lcd.init(); lcd.backlight(); + // Display fixed text on the LCD + lcd.setCursor(2, 0); + lcd.print("CONVEYOR SPEED"); + lcd.setCursor(5, 1); + lcd.print("CONTROLLER"); + lcd.setCursor(5, 2); + lcd.print("VELOCIDADE"); + // Set up interrupts for encoder signal changes attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), ai0, CHANGE); attachInterrupt(digitalPinToInterrupt(ENCODER_DT), ai1, CHANGE); @@ -74,17 +83,17 @@ void loop() { // Reset counter for the next measurement counter = 0; - // Display RPM and speed on the LCD - lcd.clear(); - lcd.setCursor(2, 0); - lcd.print("CONVEYOR SPEED"); - lcd.setCursor(5, 1); - lcd.print("CONTROLLER"); - lcd.setCursor(5, 2); - lcd.print("VELOCIDADE"); - lcd.setCursor(5, 3); - lcd.print(speed_m_min); - lcd.print(" m/min"); + // Display speed on the LCD only if it has changed + if (speed_m_min != lastSpeed_m_min) { + lcd.setCursor(5, 3); + lcd.print(" "); // Clear previous value + lcd.setCursor(5, 3); + lcd.print(speed_m_min); + lcd.print(" m/min"); + + // Update the last speed value + lastSpeed_m_min = speed_m_min; + } // Update the last time reference lastTime = currentTime; @@ -106,3 +115,13 @@ void ai1() { counter++; } } + +} + +// Interrupt service routine for encoder channel B +void ai1() { + // If both signals are different, the encoder is rotating forward + if (digitalRead(ENCODER_CLK) != digitalRead(ENCODER_DT)) { + counter++; + } +}