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

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