How to trigger a motor once with a sensor in a void loop

Ok so I am triggering a motor with a sensor and I have figured out how to, but when the sensor value goes over 500 the motor moves but doesn't stop and keeps on moving the motor. How do I make it trigger once in a void loop. I want it to trigger when the value goes over 500 then run the motor then stop, but be able to trigger it again when the value goes over 500 again.

Here is my code:

#include <SoftwareSerial.h>
SoftwareSerial ArduinoUno(0,1);

// Setup pins
int standBy = 13;

// Motor A
int PWMA = 10;   // PWM Speed Control
int AIN1 = 6;   // Direction pin 1
int AIN2 = 5;   // Direction pin 2

void setup() {

  Serial.begin(115200);
    ArduinoUno.begin(9600);

  // Setup Pins as OUTPUT
  pinMode(standBy, OUTPUT);

  // Motor A
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

}

void loop() {
  while(ArduinoUno.available()>0){
    int val = ArduinoUno.parseFloat();
    if(ArduinoUno.read()== '\n')
    Serial.println(val);
  delay(300);

  while(val > 500){
  forward(255);   // Move forward
  delay(5000);    // ... for 2 seconds
  stop();         // ... Stop the motors
  delay(2000);     // Delay between motor runs.

  reverse(255);   // Move in reverse
  delay(5000);    // ... for 2 seconds
  stop();         // ... Stop the Motors;
  }
  }
}

/*
 * Functions
 * *
 */

void forward(int spd) 
{
  runMotor(0, spd, 0);
  runMotor(1, spd, 0);
}

void reverse(int spd)
{
  runMotor(0, spd, 1);
  runMotor(1, spd, 1);
}

void runMotor(int motor, int spd, int dir)
{
  digitalWrite(standBy, HIGH); // Turn on Motor

  boolean dirPin1 = LOW;
  boolean dirPin2 = HIGH;

  if(dir == 1) {
    dirPin1 = HIGH;
    dirPin2 = LOW;
  }

  if(motor == 1) {
    digitalWrite(AIN1, dirPin1);
    digitalWrite(AIN2, dirPin2);
    analogWrite(PWMA, spd);
  }

}

void stop() {
  digitalWrite(standBy, LOW);
} 

(don't worry about the software serial that is UART from ESP32S)
Please help I need this working by tomorrow sharp.

Change while to if.

The loop function already loops. The while says: if this, do another loop. You want that conditional. Merge the two concepts.

There is no way out of this while loop. Once var is > 500 it enters the loop... but it can never escape because the value of var does not change in the loop.

Ok I did that thanks for the fast reply, it works. Now I want to split that run motor and make it so if the sensor value goes over 500 then it runs the motor forwards then stops. Then, the next time it reaches over 500 it reverses the motor. Is there any way to do that? Thanks for the help.

Try it yourself. If you can't get it to work, post your code in this same thread.

Of course... you just need to keep track of what you did the previous time you were > 500.

You could possibly do that with a boolean (true of false) variable.

boolean moveForward = true;

... and then instead of all this code...

You need another "if" statement that checks moveForward. If it is true, you do the forward stuff, if false you do the reverse stuff.

Then at the end set it to the opposite value for the next time through.

moveForward = !moveforward;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.