mercredi 17 mars 2021

If statement conditional in a while loop never met

I have ran into this problem multiple times in different situations and still have not found a solution. In the code below, I am attempting to shut off my motors (analogWrite(RPWMpin, 0duty); analogWrite(LPWMpin, 0duty);) and restrict any other code to run unless the start button is pressed (start == 1). This all works well until it enters the while loop. Inside the loop, the if statement conditional is never met, despite having pressed the button. The while loop never exits and my code is stalled with no exit. GamePad.isStartPressed() is a function that returns a Boolean value corresponding to the press of the start button. The variable tenFeet is a float with a value >0.

I have tried manually breaking the loop inside the second if statement (if (start == 1)) without reassigning the variables eracount and elacount, thinking changing the variable the condition depends on may be causing the problem. This also failed and never exits the while loop. I have also tried eliminating the first if statement (if (elacount >= tenFeet || eracount >= tenFeet)) and just started with the while loop, this also failed. I am very unsure why the if statement inside the while loop will never have its condition met. Please help.

if (elacount >= tenFeet || eracount >= tenFeet){
    analogWrite(RPWMpin, 0*duty);
    analogWrite(LPWMpin, 0*duty);
    while(elacount >= tenFeet || eracount >= tenFeet){
        start = GamePad.isStartPressed();
        if (start == 1){
            eracount = 0;
            elacount = 0;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire