Changing and keeping states?


With my arduino I’m driving a DC motor through a L298 chip. The speed and direction are handled by the input of two ultrasonic sensors. Altering the speed is not a problem, however altering direction is a bit tougher.

To change the direction I have to change 2 output pins on the arduino respectively from HIGH to LOW and LOW to HIGH. This isn’t a problem, but the arduino should do that when one of the sensors gets a reading below 100, when the motor get’s too close. If I use an if statement like this:

if (ultrasonic1 <= 100){
outputpin1 == LOW;
outputpin2 == HIGH;

The motor turns around in the other direction, but when the sensor gets above 100, it returns again in the first direction, because the code is looping. Since the sensor is actually measuring the distance the motor is away, the motor gets stuck at a 100 away of the sensor.

How can I keep the direction switched, even after the criteria for switching are no longer valid? Is there a different statement than the if statement that handles this?