jeudi 29 juillet 2021

Why is my if statement being skipped and how to stop if statements conflicting each other?

I am creating a programme that uses DC motors to change gears on a bike. I have one DC motor for the rear set of gears ie 1-9 and I have a DC motor for the middle gears ie 1-3. The problem I have is that when I'm switching from gear 9 to gear 8, I press a button and the motor just keeps running forever.

This is my gear nine function:

void GearNine() {   //CHANGE FROM GEAR 9-10, or 10-9
  int target = 888;   // TARGET FOR GEAR 10

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  //  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    //if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      
      //Turn on motor A
      MotorA_Forwards();
      MotorB_Backwards();
     }
  }
  if (pos >= 838) {   //GO TO GEAR 1 ON MOTOR 1
      MotorAOff();   //TURN MOTOR OFF
  }
  if (pos2 <= 50){   //GO TO GEAR 1 ON MOTOR 2
      MotorBOff();   //TURN MOTOR OFF
      buttonPushCounter2++; //ADD TO GEAR COUNTER
  }
  
  
  
//      delay(50);
  lastButtonState = buttonState;
    Serial.print(" Motor target: ");
    Serial.print(target);
    Serial.print(" pos1: ");
    Serial.print(pos);
    Serial.print(" pos2: ");
    Serial.print(pos2);
    Serial.print(" Gear: ");
    Serial.print(buttonPushCounter2);
    Serial.println();


        buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {
    //if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      
      // if the current state is HIGH then the button went from off to on:
      //Turn on motor B
      MotorB_Backwards();

      if (pos2 <= 7050) {   // WAS IN GEAR 9 BUT GO BACK TO GEAR 8
        MotorBOff();
        buttonPushCounter2--;  //MINUS 1 FROM GEAR COUNTER: WAS 9, NOW 8
      }
    }
  }
//  if (pos2 <= 7050) {   // WAS IN GEAR 2 BUT GO BACK TO GEAR 1
//  MotorBOff();
//  buttonPushCounter2--;  //MINUS 1 FROM GEAR COUNTER: WAS 2, NOW 1
//  }
  
  lastButtonState2 = buttonState2;
  lastButtonState = buttonState;
    
    Serial.print(" Motor target: ");
    Serial.print(target);
    Serial.print(" pos1: ");
    Serial.print(pos);
    Serial.print(" pos2: ");
    Serial.print(pos2);
    Serial.print(" Gear: ");
    Serial.print(buttonPushCounter2);
    Serial.println();

}

As you can see near the bottom I have these lines of code to make the gear run back down to gear 8 after the line of code that says MotorB_Backwards();:

if (pos2 <= 7050) {   // WAS IN GEAR 9 BUT GO BACK TO GEAR 8
        MotorBOff();
        buttonPushCounter2--;  //MINUS 1 FROM GEAR COUNTER: WAS 9, NOW 8
      }

If I have these lines of code here, the motor won't stop running if the button is pressed. Where as If I have the code where I have commented it out below these lines, then the if statement conflicts with this if statement which is slightly above in the code:

  }
  if (pos >= 838) {   //GO TO GEAR 2
      MotorAOff();   //TURN MOTOR OFF
  }
  if (pos2 <= 50){
      MotorBOff();
      buttonPushCounter2++;
  }

The reason it conflicts with these if statements is that if I want to go to gear 10. Motor A will go to pos 838 but motor B wont go down to pos2 of 50, instead it will stop at pos2 of 7050, because these if statements conflict with each other. So basically I'm not sure if my if statement is correct or if I'm putting it in the correct place or even wondering if there is a better way of doing this. Much appreciated for anyone's help. Thank you.

Aucun commentaire:

Enregistrer un commentaire