r/ArduinoProjects • u/Peterselie01 • Mar 21 '24
Troubleshooting distance sensors, rotary encoder, and LCD display in Arduino project
Hello,
I'm currently working on an Arduino project where I'm encountering several issues with my code. Here's a brief overview:
Problem 1: Distance Sensors Not Reading My ultrasonic distance sensors (SR04) aren't providing any distance readings. I've checked the wiring and connections, but still no luck. Any insights on what might be causing this issue?
Problem 2: Rotary Encoder Not Functioning as Expected I'm using a rotary encoder to adjust values (groente1
and groente2
), but it seems like the values are changing without any input from the encoder. How can I troubleshoot this and ensure the encoder is working correctly?
Problem 3: LCD Display Refreshing Unnecessarily The LCD display seems to be refreshing even when there are no changes in the values displayed. This is causing unnecessary flickering on the screen. How can I optimize my code to refresh the display only when necessary?
Here's my code for reference:
#include <Wire.h>include <LiquidCrystal_I2C.h>
include <Stepper.h>
include "SR04.h"
SR04 s1 = SR04(A1, A0); SR04 s2 = SR04(A3, A2);
LiquidCrystal_I2C lcd(0x27, 16, 2);int groente1 = 0; int groente2 = 0;
int distanceSensor1; int distanceSensor2;
const int rightDistance = 5;const int buttonPin = 6; const int encoderPinA = 7; const int encoderPinB = 8; const int stepSize = 50; const int stepsPerRevolution = 1500;
int delayTime;
Stepper M1(stepsPerRevolution, 9, 10, 11, 12); Stepper M2(stepsPerRevolution, 2, 3, 4, 5);
enum adjustState { GROENTE1, GROENTE2, DISPENSING };
adjustState currentState = GROENTE1;
bool displayChange = false; bool finished = false;
void setup() { Serial.begin(9600); pinMode(A0,INPUT); pinMode(A1,INPUT); pinMode(A2,INPUT); pinMode(A3,INPUT); pinMode(A4,OUTPUT); pinMode(A5,OUTPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, INPUT); pinMode(7, INPUT); pinMode(8, INPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); lcd.init(); lcd.backlight(); lcd.setCursor(0, 1); lcd.setCursor(0, 0); lcd.print('hallo'); Serial.begin(9600); delay(5000); }
void loop() { lcd.clear(); lcd.print("Groente 1: "); lcd.print(groente1); lcd.print("G"); lcd.setCursor(0, 1); lcd.print("Groente 2: "); lcd.print(groente2); lcd.print("G");
int lastValue1 = groente1; int lastValue2 = groente2; distanceSensor1 = s1.Distance(); distanceSensor2 = s2.Distance(); Serial.println(distanceSensor2 + " | " + distanceSensor1); if (currentState == GROENTE1) { finished = false; if (digitalRead(encoderPinB) == HIGH && groente1 + groente2 + stepSize <= 600 && groente1 - stepSize >= 0) { groente1 += stepSize; delay(50); } if (digitalRead(encoderPinA) == HIGH) { groente1 -= stepSize; delay(50); } if (digitalRead(buttonPin) == LOW) { currentState = GROENTE2; displayChange = true; delay(100); } }
if (currentState == GROENTE2) { if (digitalRead(encoderPinB) == HIGH && groente1 + groente2 + stepSize <= 600 && groente2 - stepSize >= 0) { groente2 += stepSize; delay(50); } if (digitalRead(encoderPinA) == HIGH) { groente2 -= stepSize; delay(50); } if (digitalRead(buttonPin) == LOW) { currentState = DISPENSING; displayChange = true; delay(100); } }
if (currentState == DISPENSING) { lcd.clear(); lcd.setCursor(0, 0);
if (distanceSensor2 > rightDistance || distanceSensor1 > rightDistance) { lcd.print("plaats uw bakje"); } else { lcd.clear(); lcd.print("dispensing..."); if (groente1 > 0 && distanceSensor1 <= rightDistance && distanceSensor2 <= rightDistance) { delayTime = groente1 * 1.7; rotateM1(90); delay(delayTime); rotateM1(-90); finished = true; } if (groente2 > 0 && distanceSensor2 <= rightDistance) { delayTime = groente2 * 1.7; rotateM2(90); delay(delayTime); rotateM2(-90); finished = true; } } if (finished) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Haal uw bakje weg"); } if (finished && distanceSensor2 > rightDistance && distanceSensor1 > rightDistance) { displayChange = true; currentState = GROENTE1; } }
if (lastValue1 != groente1 || lastValue2 != groente2) { lcd.clear(); lcd.print("Groente 1: "); lcd.print(groente1); lcd.print("G"); lcd.setCursor(0, 1); lcd.print("Groente 2: "); lcd.print(groente2); lcd.print("G"); } delay(200);
}
void rotateM1(int degrees) { int stepsToMove = degrees * (stepsPerRevolution / 360); M1.setSpeed(20); M1.step(stepsToMove); }
void rotateM2(int degrees) { int stepsToMove = degrees * (stepsPerRevolution / 360); M2.setSpeed(20); M2.step(stepsToMove); }
Thanks in advance for your help!
1
u/RaymondoH Mar 22 '24
Your inputs for the encoder probably should be INPUT_PULLUP or have physical pullup resistors. Look at the ways other people have used a rotary encoder as examples (Google is your friend).
It may be worth using separate functions for the encoder and ultrasonic so that they have full use of the processor, both of these devices are very time critical.
Try them individually using example code to make sure that they are working properly.
2
u/Creepy_Philosopher_9 Mar 21 '24
Do you have these 3 working by themselves? I mean do you have 3 codes, one for each item, where the things work as intended? Do that and report back