I am building a controller for filling liquid containers to user specific amounts, 0-450 Liters. The target amounts are set by depressing various buttons to sum up or down to a target quantity. For example depressing "Up 100" will add 100 L to the target. Depressing the button again will sum it to 200 L and so on. I have buttons for up and down 100L, up and down 10L etc.
The system seems to work so far, the switches are adding the sum totals as they should.
The issue is a delay when I depress the same switch several times in a row. I have to wait about 1 second before the system registers that I depressed the switch again. The target updates correctly but there is an unwanted delay.
If I hit a different switch, the target is updated almost immediately. The issue only occurs when hitting the same switch repeatedly.
I tried changing the numbers on the debounce timer from 50 to 1 to 10 with no effect. I also tried disabling serial.Print to no effect.
Any ideas what is causing this?
Using an Arduino Uno from the starter kit with the starter Kit LCD screen.
I attached parts of the sketch below it is too large to allow me to attach all of it.
[code]
/*
Tote Filler Arduino Sketch Chuck Baresich
Updated February 13th 2018
_____________________________________
Parts of Base Sketch taken from:
-- Arduino Starter Kit example - Project 11 - Crystal Ball by Scott Fitzgerald
-- Sketch scorecard from Arduino community by user Scarybeard posted January 2016
-- Debounce Sketch created by David Mellis and modified thereafter
_____________________________________
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;
// set up constants for the input switches from control, and variables to hold the values of the switch pins
// Reset target switch
const int resetPin = 7;
int resetState;
int lastResetState = LOW;
// switches for increase and decrease by 100 count
const int up100Pin = 8;
const int up100 = 100;
int up100State;
int lastUp100State = LOW;
const int down100Pin = 10;
int down100State;
int lastDown100State = LOW;
const int down100 = -100;
// switches for increase and decrease by 10 count
const int up10Pin = A4;
int up10State;
int lastUp10State = LOW;
const int up10 = 10;
const int down10Pin = A3;
const int down10 = -10;
int down10State;
int lastDown10State = LOW;
// 450L switch
const int max450Pin = 6;
const int max450L = 450;
int max450State;
int lastMax450State = LOW;
// variable to hold the target quantity
int target = 000;
int actual = 000;
int ptarget = 000;
const int minimumL = 0;
void setup() {
// set up the number of columns and rows on the LCD
lcd.begin(16, 2);
// set up the switch pin as an input
pinMode(startPin, INPUT);
digitalWrite(startPin, startState);
pinMode(flow, INPUT);
pinMode(stopPin, INPUT);
digitalWrite(stopPin, stopState);
pinMode(resetPin, INPUT);
digitalWrite(resetPin, resetState);
pinMode(up100Pin, INPUT);
digitalWrite(up100Pin, up100State);
pinMode(down100Pin, INPUT);
digitalWrite(down100Pin, down100State);
pinMode(up10Pin, INPUT);
digitalWrite(up10Pin, up10State);
pinMode(down10Pin, INPUT);
digitalWrite(down10Pin, down10State);
pinMode(max450Pin, INPUT);
digitalWrite(max450Pin, max450State);
lcd.setCursor(0, 0);
lcd.print("Target:");
lcd.setCursor(0, 1);
lcd.print("Actual:");
}
void loop() {
target = constrain(target, minimumL, max450L);
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(target);
lcd.setCursor(12, 0);
lcd.print(" L");
lcd.setCursor(8, 1);
lcd.print(actual);
lcd.setCursor(12, 1);
lcd.print(" L");
// check the status of the switches
// user wants to increase target by 100 L at a time
int readUp100 = digitalRead(up100Pin);
if (readUp100 != lastUp100State) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readUp100 != up100State) {
up100State = readUp100;
if (up100State == HIGH) {
updateup100();
}
}
}
lastUp100State = readUp100;
// user wants to decrease target by 100 L at a time
int readDown100 = digitalRead(down100Pin);
if (readDown100 != lastDown100State) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readDown100 != down100State) {
down100State = readDown100;
if (down100State == HIGH) {
updatedown100();
}
}
}
lastDown100State = readDown100;
// user wants 450L pumped, reset button is not pushed and stop button is not pushed
int readMax450 = digitalRead(max450Pin);
if (readMax450 != lastMax450State) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readMax450 != max450State) {
max450State = readMax450;
if (max450State == HIGH) {
updatemax450();
}
}
}
lastMax450State = readMax450;
// user wants to reset target to zero
int readReset = digitalRead(resetPin);
// Serial.print(" Reset? ");
// Serial.print(readReset);
if (readReset != lastResetState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readReset != resetState) {
resetState = readReset;
if (resetState == HIGH) {
// Serial.print("reset has been pressed");
updatereset();
}
}
}
lastResetState = readReset;
// user wants to stop pumping liquid
int readStop = digitalRead(stopPin);
//Serial.print(" Stop? ");
//Serial.println(readStop);
if (readStop != lastStopState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readStop != stopState) {
stopState = readStop;
if (stopState == HIGH) {
// Serial.print("stop has been pressed");
updatestopvalve();
}
}
}
lastStopState = readStop;
// user wants to increase target by 10 L at a time
int readUp10 = digitalRead(up10Pin);
//Serial.print(" Up 10? ");
// Serial.println(readUp10);
if (readUp10 != lastUp10State) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readUp10 != up10State) {
up10State = readUp10;
if (up10State == HIGH) {
// Serial.print("Up 10 has been pressed");
updateup10();
}
}
}
lastUp10State = readUp10;
// user wants to decrease target by 10 L at a time
int readDown10 = digitalRead(down10Pin);
// Serial.print(" Down 10? ");
// Serial.println(readDown10);
if (readDown10 != lastDown10State) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (readDown10 != down10State) {
down10State = readDown10;
if (down10State == HIGH) {
// Serial.print("down 10 has been pressed");
updatedown10();
}
}
}
lastDown10State = readDown10;
}
void updatemax450()
{
target = max450L;
lcd.setCursor(8, 0);
lcd.print(target);
// Serial.print(target);
}
void updatereset()
{
target = 0;
lcd.setCursor(8, 0);
lcd.print(target);
lcd.setCursor(8, 0);
lcd.print(" ");
}
void updateup100()
{
target = target + up100;
lcd.setCursor(8, 0);
lcd.print(target);
}
void updatedown100()
{
if (target >= minimumL) {
ptarget = target;
target = ptarget + down100;
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(target);
}
else if (target <= minimumL) {
target = minimumL;
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.print(target);
}
}
void updatestopvalve()
{
// do something
}
void updateup10()
{
target = target + up10;
lcd.setCursor(8, 0);
lcd.print(target);
}
void updatedown10()
{
ptarget = target;
target = ptarget + down10;
lcd.setCursor(8, 0);
lcd.print(target);
}
[/code]