Hi Salukikev,
you have written about a state-machine and your code is - somehow a state-machine but with a lot of if-conditions. There is a second way to code state-machines using a switch-case-structure.
switch-case is an if-condition which can have mutliple conditions which are executed mutually exclusive.
First of all I want to describe in my own words if I have understood the wanted functionality right.
You have a shuttle that can move in only two directions back and forth because it is mechanically guided in some way. Similar to a railroad.
On end is the "charger" the other end is the "bumper"
You have limit-switches on both ends and if the shuttle reaches the bumper-end shuttle will bump onto the switch "closing" the switch.
If closing the bumper-switch has been detected change drive-direction to drive to the opposite end towards charger-switch.
Your device has two motors
motor1 drive back and forth
motor2 driving a winch which makes an object climb up or climb down
motor2 (the winch) has one limit-switch at the upper end
What is not yet clear to me is what the behaviour shall be I guess
shuttle starts driving away from charger-end
- if a defined time-intervall is over stop driving
- start driving winch to lower object
- if a defined time-interval is over stop lowering
- wait a defined time-interval with both motors off
- if the defined time-intervall is over start winch-motor to lever object
- drive winch-motor until winch-limit-switch is switching
- stop winch-motor
- go on driving the shuttle in the same direction as before
if charger or bumber limit-switch is hit change drive-direction
repeat this cycle infinitely
I'm not really sure if this description is correct in all details.
Anyway even if some detail deviate but it is correct that you have a well defined sequence of steps that are repeated this is a very classical case for a state-machine that is based on a switch-case structure.
Even if you would like to have some deviations in the sequence of the steps this is possible to do with a "real" switch-case-state-machine.
Because a switch-case structure can execute parts of the code mutually exclusive you don't have to check always for all conditions or have to think a lot about how and where to nest conditions or how to logically add or's / and's to the conditions.
This means once you have understood the concept of the switch-case it becomes much easier to code sequencies. You can concentrate on that conditions that initialise the transition to the next step. (which here is called the next "state")
So please confirm if my decription is right or post corrections if my description deviates from your wanted behaviour
As an additional hint you should give all variables and constants name that explain themselves.
examples
"motor1" motor1 is the that motor that makes your device drive so the name should say exactly that
"drivemotor" or "motor_drive" or something like that
motor2 is that motor that turns the winch
"winchmotor" or "motor_winch"
me personally I would prefer
"driveMotor" and "winchMotor"
and in this way the name comments itself
instead of
int Motor1; //variable for toggling CW
int Motor2; //variable for winch motor
write
int driveMotorDir; // no comment needed the name explains ITSELF
int winchMotorDir;
const byte towardsBump = 1;
const byte towardsCharger = 0;
const byte up = 1;
const byte down = 0;
const byte toCharger = 1;
const byte toBumper = 0;
const byte closed = HIGH;
const byte opend = LOW;
in this way the code becomes much more readable and explains itself just by reading
if (ChargeButtonPin == closed) {
driveMotorDir = toBumper;
}
coding this way means some more work to write constants and longer names. Buuuut if you came back to this code in six month to change something you read it once and you what th code is doing.
additional when working with the code your brain has less work to do for translating things like "motor1" is the drivermotor-direction and value "1" means drive towards charger etc.
best regards Stefan