I have a basic counting project i'm working on. It's been 5 years since I last worked with an Arduino...
When the sensor detects an object, it activates a relay to move the object and displays a count, then pauses & waits for next object. I'm still waiting for my parts to arrive to test fully, but using a simulator I have a discrepancy between the actual count (Serial) and the displayed count by 1.
eg: counted 1 / displays 0, counted 2 / displays 1... etc...
video demo: https://drive.google.com/file/d/18zAv300GGIrNI5NvT6xuWbkUnzbKej4D/view?usp=sharing
My Code:
#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(13,12,11,10,9,8);
const long Limit = 10;
int count=0;
int ttlcount=0;
int relay = 3;
int sensor = 5;
const long HoldTime = 1500;
const long Wait1 = 600;
const long Wait2 = 800;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(sensor, INPUT);
pinMode(relay, OUTPUT);
lcd.setCursor(0, 0);
lcd.print(" Counted:");
}
void loop() {
if (digitalRead(sensor) == HIGH) {
delay(Wait1);
digitalWrite(relay, HIGH);
delay(HoldTime);
lcd.setCursor(13,0);
lcd.print(count++);
Serial.println(count);
digitalWrite(relay, LOW);
delay(Wait2);
}
if(count > Limit){
digitalWrite(relay, LOW);
lcd.setCursor(0,1);
lcd.print("Empty & Reset");
while(count > Limit);
}
}
Any Ideas?
Thanks in advanced!
The displayed results are correct, and differ by one, exactly as coded and expected.
lcd.print(count++);
Serial.println(count);
LarryD
October 11, 2021, 10:22pm
3
lcd.print(count);
Serial.println(count++);
Thank you @LarryD !
That worked along with changing:
int count=0;
to:
int count=1;
Cheers!
CliffyPid:
lcd.print(++count);
@lastchancename Yup! that worked with the posted code. Only need to change one line as opposed to 3 lines. Odd I couldn't find this solution elsewhere... I've been searching for hours and within 20-30 mins of posting on the forum. Voila!!
Thanks again!
@lastchancename I spoke too soon.
I did have to make some more code changes for the "Limit" function to work with the displayed counts.
if(count > Limit -1){
digitalWrite(relay, LOW);
lcd.setCursor(0,1);
lcd.print("Empty & Reset");
while(count > Limit -1);
Final woking code in case someone else is having the same issue:
#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(13,12,11,10,9,8);
const long Limit = 5;
int count=0;
int relay = 3;
int sensor = 5;
const long HoldTime = 1800;
const long Wait1 = 600;
const long Wait2 = 800;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(sensor, INPUT);
pinMode(relay, OUTPUT);
lcd.setCursor(0, 0);
lcd.print(" Counted: ");
lcd.setCursor(0, 2);
lcd.print(" ");
delay(500);
}
void loop() {
if (digitalRead(sensor) == HIGH) {
delay(Wait1);
digitalWrite(relay, HIGH);
delay(HoldTime);
lcd.setCursor(13,0);
lcd.print(++count);
Serial.println(count);
digitalWrite(relay, LOW);
delay(Wait2);
}
if(count > Limit -1){
digitalWrite(relay, LOW);
lcd.setCursor(0,1);
lcd.print("Empty & Reset");
while(count > Limit -1);
}
}
fixed the count limit issue with this:
if(count >= Limit){
digitalWrite(relay, LOW);
lcd.setCursor(0,1);
lcd.print("Empty & Reset");
while(count >= Limit);
system
Closed
April 13, 2022, 1:02am
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.