pcbbc:
And you want to keep on doing buttonPress1() function, even once the button is released? Am I correct?
Then you need to remove that call from the button check, and write some code...
if (state == ADJUSTING_VARIABLE) {
switch(variable)
{
case 1:
buttonPress1();
break;
case 2:
buttonPress2();
break;
.....
}
}
See how pressing the button causes the state to change, and it’s the new state that causes the correct function to be called to do the work, and the work keeps getting called (regardless of if the button is still pressed or not, or if a different button is pressed) until something changes state back to WAITING_FOR_BUTTON.
Thank you pcbbc, I really appreciate the time taken to address my rookie problem!
I am not very familiar with switch/case statements yet, so I will start reading and practising!
ToddL1962:
Here is a code refactor that reduces the size of your code using a button data structure. It compiles but I haven't tested it and also I have not made any changes to it to address your original issue. When I have time I will look at it some more.
#include <digitalIOPerformance.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte encPinA = 2;
const byte encPinB = 12;
const byte encSw = 4;
const byte ledPin = 5;
const byte startButton = 3; // 100nF cap to GND for crude debounce
bool startButtonState = HIGH;
bool startButtonStateLast = HIGH;
bool ledReady = false;
volatile bool countUpFlag = false;
volatile bool countDnFlag = false;
unsigned long previousMillis = 0;
struct ButtonData_s
{
const byte buttonPin;
bool buttonState;
bool buttonStateLast;
unsigned long value;
unsigned long valueLast;
unsigned long buttonDebounceLast;
bool buttonFlag;
int cursorCol;
int cursorRow;
};
ButtonData_s buttonData[6] = { {6, HIGH, HIGH, 0, 0, 0, false, 1, 0},
{7, HIGH, HIGH, 0, 0, 0, false, 5, 0},
{8, HIGH, HIGH, 0, 0, 0, false, 9, 0},
{9, HIGH, HIGH, 0, 0, 0, false, 1, 1},
{10, HIGH, HIGH, 0, 0, 0, false, 5, 1},
{11, HIGH, HIGH, 0, 0, 0, false, 9, 1} };
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
lcd.clear();
pinMode(encPinA, INPUT_PULLUP);
pinMode(encPinB, INPUT_PULLUP);
pinMode(encSw, INPUT_PULLUP);
for (int i=0; i<6; i++)
{
pinMode(buttonData[i].buttonPin, INPUT_PULLUP);
}
pinMode(startButton, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
attachInterrupt(digitalPinToInterrupt(encPinA), encISR, LOW);
///////////////////////////////////
//// PRINT VALUES TO LCD ////
///////////////////////////////////
lcd.setCursor(0, 0);
lcd.print("Drop Controller");
lcd.setCursor(0, 1);
lcd.print("*** START ***");
delay(1000);
lcd.clear();
for (int i=0; i<6; i++)
{
lcd.setCursor(buttonData[i].cursorCol, buttonData[i].cursorRow);
lcd.print(buttonData[i].value);
lcd.setCursor(buttonData[i].cursorCol, buttonData[i].cursorRow);
}
}
void encISR () {
///////////////////////////////////
//// ENCODER INTERRUPT ////
///////////////////////////////////
static unsigned long debounceLast = 0;
unsigned long debounce = millis();
if (debounce - debounceLast > 5) {
if (digitalRead(encPinB) == LOW) {
countUpFlag = true;
} else {
countDnFlag = true;
}
}
debounceLast = debounce;
}
void loop() {
///////////////////////////////////
//// READ BUTTON STATES ////
///////////////////////////////////
for (int i=0; i<6; i++)
buttonData[i].buttonState = digitalRead(buttonData[i].buttonPin);
///////////////////////////////////
//// START LED SEQUENCE ////
///////////////////////////////////
startButtonState = digitalRead(startButton);
if (startButtonState != startButtonStateLast) {
if (startButtonState == LOW) {
ledReady = true;
previousMillis = millis();
}
startButtonStateLast = startButtonState;
}
if (ledReady) {
static int ndx = 0;
if (ndx < 6) {
if (millis() - previousMillis >= buttonData[ndx].value) {
previousMillis = millis();
digitalWrite(ledPin, ! digitalRead(ledPin));
ndx ++;
}
}
else {
ndx = 0;
ledReady = false;
}
}
//////////////////////////////////////
//// BUTTON SELECT FUNCTION CALLS ////
//////////////////////////////////////
for (int i=0; i<6; i++)
{
if (millis() - buttonData[i].buttonDebounceLast > 5) {
if (buttonData[i].buttonState == LOW) {
buttonPress(i);
}
buttonData[i].buttonDebounceLast = millis();
}
}
}
///////////////////////////////////
//// BUTTON SELECT FUNCTIONS ////
///////////////////////////////////
void buttonPress(int index) {
lcd.setCursor(1, 0);
lcd.print(buttonData[index].value);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(" ");
lcd.setCursor(buttonData[index].cursorCol-1, buttonData[index].cursorRow);
lcd.print(" ");
if (!digitalRead(encSw)) {
buttonData[index].value = 0;
lcd.setCursor(buttonData[index].cursorCol, buttonData[index].cursorRow);
lcd.print(buttonData[index].value);
lcd.setCursor(buttonData[index].cursorCol, buttonData[index].cursorRow);
lcd.print(" ");
while (!digitalRead(encSw))
delay(1);
}
if (countUpFlag) {
buttonData[index].value++;
}
if (countDnFlag) {
buttonData[index].value--;
}
countUpFlag = false;
countDnFlag = false;
if (buttonData[index].value != buttonData[index].valueLast) {
lcd.setCursor(buttonData[index].cursorCol, buttonData[index].cursorRow);
lcd.print(buttonData[index].value);
lcd.setCursor(buttonData[index].cursorCol, buttonData[index].cursorRow);
lcd.print(" ");
buttonData[index].valueLast = buttonData[index].value;
}
buttonData[index].value = min(300, max(0, buttonData[index].value));
}
Wow Todd!
I don't know what to say...thank you for going to such trouble, I really appreciate it.
I'm not sure how any of it works yet, but I'll try to follow it line-by-line and see if I can figure it out.
Thank you both pcbbc and Todd, you're both very kind and are indeed a credit to these forums!
I'm just sorry that I'm quite a slow learner!! 