Hello good people of Arduino Land!
This question relates to my ongoing development of my water drop controller code.
The most recent sketch was posted here yesterday:
(In two halves - 9000 char limit)
LINK:
https://forum.arduino.cc/index.php?topic=689635.15
The code works very well - I've had lots of really helpful advice along the way, but since the most recent improvements I have seen some puzzling results that I can't nail down.
I should point out that when I began this journey, I was advised to stop using delay(), so I did just that, and now there are absolutely no delay() functions anywhere in my sketch...which I'm rather proud of...c'mon, I'm a beginner!
Hardware:
- Arduino Nano clone (Elegoo, with Uno BL)
- I²C 1602 LCD (generic)
- Keyes KY-040 rotary encoder
- Seven tactile switches - six for value select, one for start button
Summary of operation:
- Six time values (milliseconds) are entered via a single rotary encoder on the fly
- These are the water drop sizes and pauses between the drops.
Order:
-
Pause One
-
Drop One
-
Pause Two
-
Drop Two
-
Pause Three
-
Drop Three
(Arbitrary names) -
Single push button to start the drop sequence - blinking an LED in this case (standing in for a 12VDC solenoid valve)
-
Change values (or not)
-
repeat with changed data or same if unchanged
The Problem.
If a zero value is entered, then I would expect to see no activity for that particular part of the sequence.
However, this is not what happens.
I'm still seeing a sizeable blink, even when there is a zero value entered into a drop size field.
Furthermore, if I set all time values to zero, I still see three rapid blinks, with a tiny pause in between them.
This has me very puzzled!
An earlier version of the code (admittedly rather poorly written, with some fairly sizeable programming errors due to lack of knowledge) seemed to work fine, ie. no blinks for zero time values.
The older (incorrectly coded, but working - 'old wonky') version of the code can be found here:
https://forum.arduino.cc/index.php?topic=689635.1
(It starts at post #1, and ends on post #3)
It seemed that when I implemented the 'switch-case' function, rather than the clumsy and just plain wrong way I did it before, it has introduced some kind of delays into the timing data.
I decided to pare down my code to it's bare minimum, to see if that is still working correctly, and it seems fine.
Here is the test code:
const byte ledPin = 4;
const byte buttonPin = 2;
bool buttonState = HIGH;
bool buttonStateLast = HIGH;
bool ledReady = false;
//unsigned long value[6] = {0, 0, 0, 0, 0, 0}; // test for zero time
unsigned long value[6] = {500, 100, 1000, 500, 1500, 2000};
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != buttonStateLast) {
if (buttonState == LOW) {
ledReady = true;
previousMillis = millis();
}
buttonStateLast = buttonState;
}
if (ledReady) {
static int ndx = 0;
if (ndx < 6) {
if (millis() - previousMillis >= value[ndx]) {
previousMillis = millis();
digitalWrite(ledPin, ! digitalRead(ledPin));
ndx++;
}
}
else {
ndx = 0;
ledReady = false;
}
}
}
I decided to buy a proper, 'grown-up' oscilloscope too (well...a better one than the 'starter' scope I have), so I attach an image, which clearly shows that for zero data entered (except for the last value - 100ms), there is clearly two pulses (and two pauses) that shouldn't be there.
It's not the best image, but it's just visible!
My scope is set to 100ms/div, so those 'pulses' seem to be about ~25ms long.
My Shako 12VDC valves have a response time of ~20ms, so these pulses are going to be a problem.
And also the 100ms pulse at the end of the sequence, seems to be too long aswell...though I'm sure this is related to the overall timing error somewhere.
I wonder if some kind expert wouldn't mind casting an eye over this conundrum, and perhaps nudging me in the right direction...I'm not sure what else to try, other than to go back to the (incorrect) code I was using before...which would really be a shame since I'd much prefer to get this code right!
Many thanks folks.