@myggle you are driving me nuts
Everytime I go back to check your code I find that you modified something of what I gave you and that modification is causing your code to work faulty
Part 1
Iāll start of with your previous code since I think itās more relevant to your end product
int pumpPin[8] = { 2, 3, 4, 5, 6, 7, 8, 9 }; //Digital pins used
int pumpNumber[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; //V0 (1 - 8)
pumpNumber is a new array that you added that is not needed,
first of its an array with the size of 9 for tracking 8 pumps so one is unused, further it does nothing for you other than denote the amount of pumps you have. The only variable you need to track which pump you are accessing is the āindexā value passed by your step widget
int pumpIndex = 0;
BLYNK_WRITE(V0) { // Step Widget
pumpIndex = param.asInt() - 1;
}
Every time you press this widget, with min max it 1-8 it will send a number in that range. Using 1-8 is purely a user interface adaptation of your array range 0-7 because it makes more sense.
By subtracting 1, your variable pumpIndex will be set with theese values
per stepper widget āindexā
1 2 3 4 5 6 7 8 <-- stepper widget var
0 1 2 3 4 5 6 7 <-- pump index var.
This is needed because your arrays always start with index 0, beeing 8 places long, the range is 0-7.
So perhaps now you see why āint pumpNumber[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; //V0 (1 - 8)ā is redundant
Part 2
As I found the same error in your new code ill bring it up again. You want to set pinModes for all your pumps as OUTPUT, and you are going about the wrong way with your for loop.
for(int p = 2; p <= 9; p++)
{
pinMode(pumpPin[p], OUTPUT);
}
Your pins are in the range of 2-9, this is declared in the array pumpPin. But! They are declared at the arrays positions 0-7.
Always, when accessing your arrays values you need to call to correct indexes.
With your array
int pumpPin[8] = { 2, 3, 4, 5, 6, 7, 8, 9 }; //Digital pins used
matching the values to index its like this
2 3 4 5 6 7 8 9 <- Values in array
0 1 2 3 4 5 6 7 <- at corresponding index
So your for loop starts at p = 2 the first element you get
is at position 3 in the array and contains your digital pin 4.
Effectively skipping position 1-2 and never setting their pin modes.
pumpPin[2] = 4
pumpPin[3] = 5
ā¦
ā¦
pumpPin[7] = 9
pumpPin[8] = N/A <- once your for loop hits this it will crash your code, or rather it should with an index out of bounds error, there is no such position in your arrays above the index value of 7.
Part 3
I would guess that, why your checkPump was crashing/not working properly boils down to your previous sneaky style implementation of the redundant array called pumpNumber.
if (pumpNumber[x] == 1 && pumpRunning[x] != 1) // Pump should run but isn't running = Start pump
for every x in the for loop you were checking the index of pumpNumber and this is what you got
x pumpNumber value
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
And checking if this was equal to 1, which only happend at x = 1
because it accessed pumpNumber[x = 1] which contained 1
thus allowing the code to proceed.
This was the original code I gave you
if (pumpState[x] == 1 && pumpRunning[x] != 1) // Pump should run but isn't running = Start pump
It was made so that it checks the state of each pump, to determine wether or not they were to be turned on or not.
so if pumpState[x] for a given pump would return 1, it would denote that this pump should run.
Then it checked if it was already running by checking the pumpRunning[x] as to prevent redundant code, because if it is already running we need not do anything with this pump.
Doing so would mess up our pumpStartTime and the pump would never stop working.
Conclusion
Always work your code top to bottom, follow the logic, speak outloud if need be. What is happening in my program at this moment, what do these variables contain prior and after my modification. etc etc.
When given a snippet of code, do not modify it untill it is working and all errors are solved, as to prevent creating new ones, case in point
About new code
Iāll take a look at your new code a bit later when I get the time
But at a first glance your new solution will bring you error should you ever change the pump index at runtime, you will loose controll of the running pump.
Also you are doing something weird with your index handling.
At declaration you do not instantiate pumpNumber, I think it defaults to 0.
So pumpNumber = 0
then x = 0 - 1
Your subtraction needs to be handled at BLYNK_WRITE(V0)
because at the moment you are indexing your value as is
within the range of 1-8 and will never get pump at index 0 of your array should you use the pump selector widget.