Problem using a variable to switch off DC motor, using H-Bridge

 while (FloatRad < 25.225)    //while sylvacRad is not at its maximum setting, drive up
  {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    analogWrite(enA, 255);
  }                 //when FloatRad reaches its condition, jump onto next line

You have got rid of the endless loop in one place by removing the trailing semicolon but now it has moved on to another place as I mentioned previously. FloatRad is still not updated in the while loop so once the code enters the loop it will never exit and there is a second while loop with the same problem

Change the while to an if and the loop() function will keep running, ReadSerial() will be run.

However, in ReadSerial() you decalare a new and different variable named FloatRad that will go out of scope, ie be unavailable, outside the the if statement code block in which it is declared. It will not even be available in the else clause of that if/else

Remove the float from

      float FloatRad = sylvacRad.toFloat();

and the value will be assigned to the global FloatRad variable and that in turn will be accessible in the DriveMotor() function

As you are discovering, computer programs do exactly what you tell them to and everything needs to be laid out simply for them

1 Like