Why does adding an 'if' effect my state machine?

I've pared down my code for troubleshooting and realized that adding these two if's for motor control makes my 'walk' value stay 1. Removing them results in a successful state change at every set interval. Why is that?


void loop(void)

{
unsigned long currentMillis = millis();
Serial.println (currentMillis - previousMillisT);

if (currentMillis - previousMillisT >= intervalT) {
    previousMillisT = currentMillis; walk = !walk; }

Serial.println (walk);

if (walk = 0);{digitalWrite (motor1pin1, LOW); digitalWrite (motor1pin2, LOW);}; //turn off walk motor
if (walk = 1);{digitalWrite (motor1pin1, HIGH); digitalWrite (motor1pin2, LOW);}; //turn on walk motor
    
}

no


if (walk == 0);
. . .

if (walk == 1);

yes

Also, if statements don't usually have a semicolon.

1 Like

BTW

Don’t take shortcuts, don’t place multiple lines of code after each other.
Follow this style:
if (walk == 1)
{
digitalWrite (motor1pin1, HIGH);
digitalWrite (motor1pin2, LOW);
}

No

Actually, in this case:

if (walk == 0) {
...
}
else
{
...
}

Well I did get it right in the post after that one. (post #4)

Put it down to being a grey headed old p*sser :pleading_face:

If 'walk' is an integer type, then this is kind of sketchy:

walk = !walk

On the other hand, if it's a bool, then this is kind of sketchy:

if (walk == 1)

Yet, another example where posting full code (including variable definitions) would have been helpful.

Yes, this is the opposite of what you need. Please post the whole thing. Or else change what you posted to make it a complete, compilable sketch.

Well.. I did immediately receive a response that addressed my exact problem- so I was happy about that. I have new problems if you want to offer some more suggestions. My latest is that printing the same variable twice to serial monitor produces a different result. Weird, right?

I'll post the full code here since you asked for it. My approach was wrong from the start, but I'm getting it straigntened out now- having simplified to just one motor and carriage. This simple program is meant to transit a little cart back & forth reversing at a limit switch on either end and pausing periodically in the middle. I'm trying to code it so that it resumes its direction after the pause and willl keep waiting for the limit switch. You'll see that there's lots more in setup and commented out, but this is the basic part of the program so far:

/*
My original code which worked sent a shuttle back and forth using these two lines:

  if (BumpButtonState == HIGH){digitalWrite (motor1pin1, HIGH); digitalWrite (motor1pin2, LOW); CW = 0;};
  if (ChargeButtonState == HIGH){digitalWrite (motor1pin1, LOW); digitalWrite (motor1pin2, HIGH); CW = 1;};

  For now, I just want to add a timer to that code to pause motion and then resume in the same direction after the pause until
  the limit is hit and then it will reverse and continue forever (reversing again after the opposite switch.  There's lots more code I tried to add but I backed it all off to sort out the 1st part first.

  

*/

//Motor1: LOW/HIGH = Move toward charger (CCW from battery side). HIGH/LOW = Move away from Charger (CW from battery side)
//Motor 2:  LOW/HIGH = Lower tether.  HIGH/LOW = Raise Tether (m2p1/m2p2)
// constants won't change. They're used here to set pin numbers:
//Short internal battery loop follows:
long readVcc() {
  long result;
  // Read 1.1V reference against AVcc.  Charge threshold: 2850 (3.5v) - 3000 (3.9v)
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA, ADSC));
  result = ADCL;
  result |= ADCH << 8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}
// end of internal battery setup code
int BumpButtonPin = 3;     // the number of the pushbutton pin
int ChargeButtonPin = 4;     // the number of the pushbutton pin
int MiniWinchPin = 12;  //number of the winch pin button
int eyes = 9;
int motor1pin1 = 6;
int motor1pin2 = 7;
int motor2pin1 = 10;
int motor2pin2 = 11;

// variables will change:
boolean  walk = true; //variable to pause the walk routine
boolean CW = false; //variable for toggling CW 1= charge side, 0= bump side
int BumpButtonState = 0;         // variable for reading the bump side button status
int ChargeButtonState = 0;      //variable for reading the charge side button status
int MiniWinchState = 0;      //variable for reading the winch button status
int Motor1; //variable for toggling CW
int Motor2; //variable for winch motor
int eyebright = 0; //how bright the eyes are
int eyefade = 5;
int battVolts;   // made global for wider avaliblity throughout a sketch if needed, example for a low voltage alarm, etc
// value is volts X 100, 5 vdc = 500
unsigned long previousMillisT = 0; //stores last time time interval was updated.
unsigned long previousMillisD = 0; //stores last time drop interval was updated.
unsigned long previousMillisW = 0; //stores last time bottom dwell interval was updated.
unsigned long previousMillisTW = 0; //stores last time  top dwell interval was updated.


long intervalT = 8000; //interval for walk stop & climbdown routine init
long intervalD = 3000; // period for climbing down
long dwellD = 2000; // dwell to hang out at bottom
long dwellT = 1500; //hang out at top before proceeding
void setup(void)
{
  Serial.begin(9600);
  // initialize the pushbutton pins as inputs:
  pinMode(BumpButtonPin, INPUT);
  pinMode(ChargeButtonPin, INPUT);
  pinMode (MiniWinchPin, INPUT);
  pinMode (motor1pin1, OUTPUT);
  pinMode (motor1pin2, OUTPUT);
  pinMode (motor2pin1, OUTPUT);
  pinMode (motor2pin2, OUTPUT);
  Serial.print("volts X 100");
  Serial.println( "\r\n\r\n" );

  while (digitalRead (BumpButtonPin) == LOW) {
    digitalWrite (motor1pin1, LOW);
    digitalWrite (motor1pin2, LOW);
    digitalWrite (motor2pin1, LOW);
    digitalWrite (motor2pin2, LOW);
  };
  //pause everything and wait for bump button to initialize sequence.
  digitalWrite (motor1pin1, HIGH); //initiate movement away from charger
  walk = 1;
}

void loop(void)

{
  unsigned long currentMillis = millis();
  Serial.println (currentMillis - previousMillisT);
  if (currentMillis - previousMillisT >= intervalT) {
    previousMillisT = currentMillis; walk = !walk;
  }
  Serial.println(); Serial.print ("walk?:"); Serial.print (walk);
  Serial.println (); Serial.print ("Clockwise?:"); Serial.print (CW);
  Serial.println(); Serial.print (currentMillis - previousMillisT);
  Serial.println(); Serial.print ("Charge Button Pin:"); Serial.print (digitalRead (ChargeButtonPin));
  Serial.println(); Serial.print ("Bump Button Pin:"); Serial.print (digitalRead (BumpButtonPin));
  Serial.println(); Serial.print ("Winch Button Pin:"); Serial.print (digitalRead (MiniWinchPin));
  Serial.println(); Serial.print ("WTF Button Pin:"); Serial.print (digitalRead (ChargeButtonPin));  //I added this because while printing the same variable 3 lines above, the result is different.

  if ((walk == true) && (CW == 1))    //Resume CW
  { digitalWrite (motor1pin1, HIGH);
    digitalWrite (motor1pin2, LOW);

    if ((walk == true) && (CW == 0))    //Resume CCW because CW = false
    { digitalWrite (motor1pin1, LOW);
      digitalWrite (motor1pin2, HIGH);
    }

    if ((digitalRead(ChargeButtonPin) == HIGH)
        && (walk == true)
        && (CW == 1))    //when charegebutton hit and walk is on rotate CCW and set CW to 1(CCW).
    { digitalWrite (motor1pin1, HIGH);
      digitalWrite (motor1pin2, LOW);
    }

    if ((digitalRead(BumpButtonPin) == HIGH)
        && (walk == true))     //when bumpbutton hit and walk is on rotate CW and set CW to 0(CW).
    { digitalWrite (motor1pin1, LOW);
      digitalWrite (motor1pin2, HIGH);
    }

    if (walk == false) {
      digitalWrite (motor1pin1, LOW); //stop the motor
      digitalWrite (motor1pin2, LOW);
    }
  }
}





/*

   Serial.println ("Resume walk, avoid drop routine.");
   walk = 0;
   Serial.println (previousMillisT);
   Serial.println (walk);
  } //at intervalT, stop walking
  if (walk = 0);{digitalWrite (motor1pin1, LOW); digitalWrite (motor1pin2, LOW);}; //turn off walk motor

  if (currentMillis - previousMillisT <= intervalT) {
   previousMillisT = currentMillis;
  }
  Serial.println ("Begin drop, avoid walk routine."); walk = 0;
  Serial.println (walk); Serial.println (currentMillis - previousMillisT);
  //When time is less than interval, walk. THIS IS WHAT IT ALWAYS SHOWS!

  if ((CW = 0) && (walk = 1)) {
   digitalWrite (motor1pin1, LOW); //resume charge CW
   digitalWrite(motor1pin2, HIGH);
   Serial.println ("Resuming Roll toward Charge side"); Serial.println(digitalRead(ChargeButtonPin));
  };


  // if ((CW = 1) && (walk = 1)) {digitalWrite (motor1pin1, HIGH); //resume bump CW (problem!)
  //  digitalWrite(motor1pin2, LOW);
  //  Serial.println ("Resuming Roll toward Bump side"); Serial.println(digitalRead(BumpButtonPin));};

  if (digitalRead(ChargeButtonPin) == HIGH && (walk = 1)) {
   digitalWrite (motor1pin1, LOW);
   digitalWrite (motor1pin2, HIGH); delay (500); (CW = 1);
  }

  if (digitalRead(BumpButtonPin) == HIGH && (walk = 1)) {
   digitalWrite (motor1pin1, HIGH);
   digitalWrite (motor1pin2, LOW); delay (500); CW = 0;
  }

  //END WALK ROUTINE, BEGIN DROP ROUTINE

  if (currentMillis - previousMillisT >= intervalT) {
   previousMillisT = currentMillis;
   Serial.println ("Stop walk, begin drop routine."); walk = 0;


   if (currentMillis - previousMillisD >= intervalD) {
     previousMillisD = currentMillis;
   };   // begin timed climbdown
   digitalWrite (motor2pin1, LOW);
   digitalWrite(motor2pin2, HIGH);
   Serial.println("Climbdown for intervalD seconds");

   if (currentMillis - previousMillisD >= dwellD) {
     previousMillisW = currentMillis;
   };   // pause at the bottom
   digitalWrite (motor2pin1, LOW);
   digitalWrite(motor2pin2, LOW); //Pause
   Serial.println("Pause dwellD seconds");

   while (digitalRead (MiniWinchPin) == LOW) {
     digitalWrite (motor2pin1, HIGH);
     digitalWrite (motor2pin2, LOW);
     Serial.println ("Climb until switch");
   };
  }

  if (digitalRead (MiniWinchPin) == HIGH) {
   digitalWrite (motor2pin1, LOW);
   digitalWrite (motor2pin2, LOW);
   Serial.println ("Climb Limit!");


   if (currentMillis - previousMillisD >= dwellT) {
     previousMillisTW = currentMillis;
   };   // pause at the top
   digitalWrite (motor2pin1, LOW);
   digitalWrite(motor2pin2, LOW); //Pause
   Serial.println("Pause dwellT seconds");

   walk = 1; //resume walk routine
  }
  }

*/


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

  1. if a defined time-intervall is over stop driving
  2. start driving winch to lower object
  3. if a defined time-interval is over stop lowering
  4. wait a defined time-interval with both motors off
  5. if the defined time-intervall is over start winch-motor to lever object
  6. drive winch-motor until winch-limit-switch is switching
  7. stop winch-motor
  8. 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

Hi,
Thanks again for the reply, Stefan! Yes, your understanding of intended behavior is exactly correct. I appreciate and will conisider your advice whlie working thorugh this. I particularly like having less convoluted steps for my brain to manage. The whole arduino experience so far has me realizing how complicated steps are that I prevoiusly thought would/should be simple. I've been tempted to try out a more visual interpreter or use tinkercad or whatever to get my code but I think once I can reliably understand coding directly it will be worth the struggle in the end. It's more that this is one of 3 ongoing arduino/halloween projects (but for some reason the hardest) and it's already Oct 10th! :slight_smile:

Two aspects to your latest problem - if the button is floating, you may see a change between readings - do you have a pulldown resistor?

Also, you're printing a lot. That will block code execution, especially at 9600 baud and give the floating pin a little time to change state.

Yes, all the inputs have pulldown resistors so I don't know where this random value is coming from. It's very strange. I know there's lots of problems with my code in general, but I tend to post here when something particularly weird comes up like this problem. The line:
"Serial.print (digitalRead (ChargeButtonPin)" should yield either a 0 or a 1, and it does on the first line, and then, 3 lines later, printing the exact thing to serial monitor yields "08096" or similar random interger. The comments are just for my reference and I'll probably take them out once its working. I could up the baud rate too if I got too worried about it, it's just default because I haven't changed it.

Try println instead - it's concatenating the print at the top of loop.

Oh geez. Yes, I see what happened there now. It still puts that 0 or 1 as intended and then sticks the timing value right after it. Silly oversight that I'm embarassed to have posted. Darn!

Hi,

what is your opinion to this suggestion:

I could rewriting your code keeping the logic as it is now but

I take your variable-names as a base but modifiy them all to be really self-explaining like in the examples above.
I add constants that have self-explaining names too like in the example above
and replace the hardcpded numbers like "1" or "0" with these constants.
This would make your code much more readable and it comes closer to "normal language"

what do you think? Would you like me to do that?
if yes this could be a base for more support in transforming your code into using a switch-case state-machine

best regards Stefan

I hate to put you out for that, and it feels like cheating on my homework, but under the circumstances and the impending holiday I'd take you up on the offer if you're so inclined. I do think it would help me out a lot to understand how to do this properly. Also, my 4yr old had a really good idea that I foolishly added to the (non-coding) worklist so I'm going to be missing some sleep shortly while fabricating all this stuff. Of course I'll post all this with STL's free for public consumption once I get things working. I surely appreciate your help!

OK I started re-writing this will take maybe 20 minutes.
In the meantime you could read this example about how switch-case state-machines work

I'm pretty sure that some detail-questions will arise while you are reading it.
I'm interested in improving the explanation more and more based on the questions beginners have.

best regards Stefan

OK so here is a modified version of the code you have posted above.

The logic is exact the same. I only added some functions with self-eplaining names and some constants with self-explaining names.

You confirmed that the steps described above are right.
I came to the conclusion that the next step should be the transformation of the logic to a switch-case state-machine.

read through this code-version to see the difference that it makes if the names tell the story directly

/*  My original code which worked sent a shuttle back and forth using these two lines:

  if (BumpButtonState == HIGH){digitalWrite (driveMotorpin1, HIGH); digitalWrite (driveMotorpin2, LOW); CW = 0;};
  if (ChargeButtonState == HIGH){digitalWrite (driveMotorpin1, LOW); digitalWrite (driveMotorpin2, HIGH); CW = 1;};

  For now, I just want to add a timer to that code to pause motion and then resume in the same direction after the pause until
  the limit is hit and then it will reverse and continue forever (reversing again after the opposite switch.  There's lots more 
  code I tried to add but I backed it all off to sort out the 1st part first.
*/

//Motor1: LOW/HIGH = Move toward charger (CCW from battery side). HIGH/LOW = Move away from Charger (CW from battery side)
//Motor 2:  LOW/HIGH = Lower tether.  HIGH/LOW = Raise Tether (m2p1/m2p2)
// constants won't change. They're used here to set pin numbers:
//Short internal battery loop follows:
long readVcc() {
  long result;
  // Read 1.1V reference against AVcc.  Charge threshold: 2850 (3.5v) - 3000 (3.9v)
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA, ADSC));
  result = ADCL;
  result |= ADCH << 8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}
// end of internal battery setup code
int BumpButtonPin = 3;     // the number of the pushbutton pin
int ChargeButtonPin = 4;     // the number of the pushbutton pin
int MiniWinchPin = 12;  //number of the winch pin button
int eyes = 9;
int driveMotorpin1 = 6;
int driveMotorpin2 = 7;
int winchMotorpin1 = 10;
int winchMotorpin2 = 11;

byte CWdriveDirection;
const byte towardsBumper  = 0;
const byte towardsCharger = 1;


// variables will change:
boolean  walk = true; //variable to pause the walk routine
boolean CW = false; //variable for toggling CW 1= charge side, 0= bump side
int BumpButtonState = 0;         // variable for reading the bump side button status
int ChargeButtonState = 0;      //variable for reading the charge side button status
int MiniWinchState = 0;      //variable for reading the winch button status
int Motor1; //variable for toggling CW
int Motor2; //variable for winch motor
int eyebright = 0; //how bright the eyes are
int eyefade = 5;
int battVolts;   // made global for wider avaliblity throughout a sketch if needed, example for a low voltage alarm, etc
// value is volts X 100, 5 vdc = 500
unsigned long previousMillisT = 0; //stores last time time interval was updated.
unsigned long previousMillisD = 0; //stores last time drop interval was updated.
unsigned long previousMillisW = 0; //stores last time bottom dwell interval was updated.
unsigned long previousMillisTW = 0; //stores last time  top dwell interval was updated.


long intervalT = 8000; //interval for walk stop & climbdown routine init
long intervalD = 3000; // period for climbing down
long dwellD = 2000; // dwell to hang out at bottom
long dwellT = 1500; //hang out at top before proceeding

void stopDriverMotor() {
  digitalWrite (driveMotorpin1, LOW); 
  digitalWrite (driveMotorpin2, LOW);     
}

void driveTowardsBumper() {
  digitalWrite (driveMotorpin1, HIGH); 
  digitalWrite (driveMotorpin2, LOW);   
}  

void driveTowardsCharger() {
  digitalWrite (driveMotorpin1, LOW); 
  digitalWrite (driveMotorpin2, HIGH);   
}  

void stopWinchMotor() {
  digitalWrite (winchMotorpin1, LOW);
  digitalWrite (winchMotorpin2, HIGH);  
}
  
void climbDown() {
  digitalWrite (winchMotorpin1, LOW);
  digitalWrite (winchMotorpin2, HIGH);  
}

void climbUp() {
  digitalWrite (winchMotorpin1, HIGH);
  digitalWrite (winchMotorpin2, LOW);  
}


void setup(void) {
  Serial.begin(115200);
  // initialize the pushbutton pins as inputs:
  pinMode (BumpButtonPin, INPUT);
  pinMode (ChargeButtonPin, INPUT);
  pinMode (MiniWinchPin, INPUT);
  pinMode (driveMotorpin1, OUTPUT);
  pinMode (driveMotorpin2, OUTPUT);
  pinMode (winchMotorpin1, OUTPUT);
  pinMode (winchMotorpin2, OUTPUT);
  Serial.print("volts X 100");
  Serial.println( "\r\n\r\n" );

  while (digitalRead (BumpButtonPin) == LOW) {
    stopDriverMotor();     
    stopWinchMotor();   
  };
  //pause everything and wait for bump button to initialize sequence.
  driveTowardsBumper(); //initiate movement away from charger
  walk = true;
}


void loop(void) {
  unsigned long currentMillis = millis();
  Serial.println (currentMillis - previousMillisT);
  if (currentMillis - previousMillisT >= intervalT) {
    previousMillisT = currentMillis; 
    walk = !walk;
  }
  Serial.println(); 
  Serial.print ("walk?:"); 
  Serial.print (walk);
  Serial.println (); 
  Serial.print ("Clockwise?:"); 
  Serial.print (CW);
  Serial.println(); 
  Serial.print (currentMillis - previousMillisT);
  Serial.println(); 
  Serial.print ("Charge Button Pin:"); 
  Serial.print (digitalRead (ChargeButtonPin));
  Serial.println(); 
  Serial.print ("Bump Button Pin:"); 
  Serial.print (digitalRead (BumpButtonPin));
  Serial.println(); 
  Serial.print ("Winch Button Pin:"); 
  Serial.print (digitalRead (MiniWinchPin));
  Serial.println(); 
  Serial.print ("WTF Button Pin:"); 
  Serial.print (digitalRead (ChargeButtonPin));  //I added this because while printing the same variable 3 lines above, the result is different.

  if ((walk) && (CWdriveDirection == towardsBumper)) {    //Resume CW
    driveTowardsBumper();
  }
    
  if ((walk) && (CWdriveDirection == towardsCharger)) {    //Resume CCW because CW = false
    driveTowardsCharger();
  }


  if ((digitalRead(ChargeButtonPin) == HIGH)
      && (walk) && (CWdriveDirection == towardsCharger)) {    //when chargebutton hit and walk is on rotate CCW and set CW to 1(CCW).
    driveTowardsBumper();
  }

  if ((digitalRead(BumpButtonPin) == HIGH)
      && (walk)) 
  {    //when bumpbutton hit and walk is on rotate CW and set CW to 0(CW).
    driveTowardsCharger();  
  }

  if (!walk) {  // the attention-sin is the not-operator !true = false   !false = true
    stopDriverMotor(); 
  }
}


/*
Serial.println ("Resume walk, avoid drop routine.");
walk = 0;
Serial.println (previousMillisT);
Serial.println (walk);
} //at intervalT, stop walking
if (walk = 0); {
  digitalWrite (driveMotorpin1, LOW);
  digitalWrite (driveMotorpin2, LOW);
}; //turn off walk motor

if (currentMillis - previousMillisT <= intervalT) {
  previousMillisT = currentMillis;
}
Serial.println ("Begin drop, avoid walk routine."); walk = 0;
Serial.println (walk); Serial.println (currentMillis - previousMillisT);
//When time is less than interval, walk. THIS IS WHAT IT ALWAYS SHOWS!

if ((CW = 0) && (walk = 1)) {
  digitalWrite (driveMotorpin1, LOW); //resume charge CW
  digitalWrite(driveMotorpin2, HIGH);
  Serial.println ("Resuming Roll toward Charge side"); Serial.println(digitalRead(ChargeButtonPin));
};


// if ((CW = 1) && (walk = 1)) {digitalWrite (driveMotorpin1, HIGH); //resume bump CW (problem!)
//  digitalWrite(driveMotorpin2, LOW);
//  Serial.println ("Resuming Roll toward Bump side"); Serial.println(digitalRead(BumpButtonPin));};

if (digitalRead(ChargeButtonPin) == HIGH && (walk = 1)) {
  digitalWrite (driveMotorpin1, LOW);
  digitalWrite (driveMotorpin2, HIGH); delay (500); (CW = 1);
}

if (digitalRead(BumpButtonPin) == HIGH && (walk = 1)) {
  digitalWrite (driveMotorpin1, HIGH);
  digitalWrite (driveMotorpin2, LOW); delay (500); CW = 0;
}

//END WALK ROUTINE, BEGIN DROP ROUTINE

if (currentMillis - previousMillisT >= intervalT) {
  previousMillisT = currentMillis;
  Serial.println ("Stop walk, begin drop routine."); walk = 0;


  if (currentMillis - previousMillisD >= intervalD) {
    previousMillisD = currentMillis;
  };   // begin timed climbdown
  digitalWrite (winchMotorpin1, LOW);
  digitalWrite(winchMotorpin2, HIGH);
  Serial.println("Climbdown for intervalD seconds");

  if (currentMillis - previousMillisD >= dwellD) {
    previousMillisW = currentMillis;
  };   // pause at the bottom
  digitalWrite (winchMotorpin1, LOW);
  digitalWrite(winchMotorpin2, LOW); //Pause
  Serial.println("Pause dwellD seconds");

  while (digitalRead (MiniWinchPin) == LOW) {
    digitalWrite (winchMotorpin1, HIGH);
    digitalWrite (winchMotorpin2, LOW);
    Serial.println ("Climb until switch");
  };
}

if (digitalRead (MiniWinchPin) == HIGH) {
  digitalWrite (winchMotorpin1, LOW);
  digitalWrite (winchMotorpin2, LOW);
  Serial.println ("Climb Limit!");


  if (currentMillis - previousMillisD >= dwellT) {
    previousMillisTW = currentMillis;
  };   // pause at the top
  digitalWrite (winchMotorpin1, LOW);
  digitalWrite (winchMotorpin2, LOW); //Pause
  Serial.println("Pause dwellT seconds");

  walk = 1; //resume walk routine
}
}

*/

best regards Stefan

1 Like