There is a very important rule you should always follow.
Each time you are posting code. Post the most actual and complete sketch.
Now you left your helpers alone with a single line of code
without showing the rest of the code.
Now we - your helpers - have to speculate what the rest of your actual code is
Is it
- the code gcjr has posted?
- the code you have posted in # 24?
- did you change anything else in the code of post # 24?
- is it the code from post # 1?
and this is the reason why you should always each and every time post your complete sketch as a code-section.
scrolling over half a screen is 0,5 seconds.
Asking back or start speculating, sorting together what the rest of the code will be
takes 5 to 50 minutes and requires asking back.
A microcontroller can do 10 case-stages in less than 0,01 seconds
So if everything takes too long it can be corrected in the code if the complete sketch can be seen.
The switch statement is a variation of an if-condition that uses one variable to check a simplified condition
In the example-code this variable has the name "myStateVar"
Depending on the value of variable "myStateVar" only that part of the code that is below this particular value will be executed.
the break statement will cause skipping the other cases.
I add to your your description
a start button
check if start-button is pressed
- wait for pressing startbutton
- conveyor runs
- bucket breaks light beam
- conveyor stops
- water dispenses for 5 sec
- conveyor starts again
Now a state machine has similarities to a machine that has different modes of operation
mode "wait for pressing startbutton"
mode "conveyor runs"
mode "water dispenses for 5 sec"
to make it easier to see the pattern = for didactical reasons I created some more modes
mode "waitForPressingStart"
mode "moveConveyor"
mode "checkForBucketInPosition"
mode "openWaterValve"
mode "wait5Seconds"
mode "closeWaterValve"
and here is the sketchy sketch that is not yet compilable but shows the principle
const byte waitForPressingStart = 0;
const byte moveConveyor = 1;
const byte checkForBucketInPosition = 2;
const byte openWaterValve = 3;
const byte wait5Seconds = 4;
const byte closeWaterValve = 5;
state = waitForPressingStart;
switch (myStateVar)
case waitForPressingStart: // only in case myStateVar == waitForPressingStart
if (digitalRead(startButtonPin == pressed) {
myStateVar = moveConveyor;
}
break; // skip other cases and IMMIDIATELY jump down to END-OF-SWITCH
case moveConveyor: // only in case myStateVar == moveConveyor
digitalWrite(drive, HIGH);
myStateVar = checkForBucketInPosition;
}
break; // skip other cases and IMMIDIATELY jump down to END-OF-SWITCH
case checkForBucketInPosition: only in case myStateVar == checkForBucketInPosition
if ( (digitalRead(sensewaterstn) == beamBroken));
digitalWrite(drive, LOW); // stop conveyor
myStateVar = openWaterValve;
}
break; // skip other cases and IMMIDIATELY jump down to END-OF-SWITCH
case openWaterValve: // only in case myStateVar == openWaterValve
digitalWrite(waterfill, HIGH);
WaterFillTimer = millis();
myStateVar = wait5Seconds;
break; // skip other cases and IMMIDIATELY jump down to END-OF-SWITCH
case wait5Seconds: // only in case myStateVar == wait5Seconds
if ( TimePeriodIsOver(WaterFillTimer, 5000) {
myStateVar = closeWaterValve;
}
break; // skip other cases and IMMIDIATELY jump down to END-OF-SWITCH
case closeWaterValve: // only in case myStateVar == closeWaterValve
digitalWrite(waterfill, LOW);
myStateVar = waitForPressingStart;
break; // skip other cases and IMMIDIATELY jump down to END-OF-SWITCH
} // END-OF-SWITCH
Here is a tutorial how non-blocking timing works
and here is a tutorial with a compiling demo-code that shows how state-machines work
side-comment at @gcjr
You made a change that I appreciate.
You stayed somehow close on the code of the TO.
Still it is more or less your "standard-code" that has a huge lack of explaining things.
My opinion: you should add this standard comment to your code-suggestions.
Here is my version of a demo-code.
If you have questions you can ask as many questions as you like
If the my code is "above your head" skip my code-suggestion.