Need help to fix the code to make Linear Actuator move specific distance

I am using a PA-18 Linear Actuator, Arduino uno, MD10C R3, and an ultrasonic sensor. Currently the code works when moving the linear actuator forward and backwards but it does not stop at the distance I want it to. Can anyone help fix this to only move forward 30 inches and backward 30 inches when the ultrasonic sensor is triggered?

const int motorPowerPin = 3;  // power pin of MD
const int motorDirectionPin = 4;  // direction pin of MD
const int trigPin = 10;  // trigger pin of ultrasonic sensor
const int echoPin = 11;  // echo pin of ultrasonic sensor

const int maxDistance = 100;  // Max distance for the ultrasonic sensor 
int triggerCount = 0;

void setup() {
  pinMode(motorPowerPin, OUTPUT);
  pinMode(motorDirectionPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Stop the motor initially
  digitalWrite(motorPowerPin, LOW);
}

void loop() {
  // Measure distance with the ultrasonic sensor
  int distance = getDistance();

  // Check if the distance is less than a threshold (detecting an object)
  if (distance < maxDistance) {
    if (triggerCount == 0) {
      // First trigger - Move forward 30 inches
      moveLinearActuator(30);
      triggerCount++;
    } else if (triggerCount == 1) {
      // Second trigger - Move backward 30 inches
      moveLinearActuator(-30);
      triggerCount++;
    }
  }
}

int getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  return pulseIn(echoPin, HIGH) * 0.034 / 2;
}

void moveLinearActuator(int distance) {
  if (distance > 0) {
    // Set the motor direction for forward
    digitalWrite(motorDirectionPin, HIGH);
  } else {
    // Set the motor direction for backward
    digitalWrite(motorDirectionPin, LOW);
    distance = -distance;  // Make distance positive for the motor control
  }

  // Turn on the motor
  digitalWrite(motorPowerPin, HIGH);
  
  // Move the linear actuator for the specified distance
  delay(distance * 100); 

  // Turn off the motor
  digitalWrite(motorPowerPin, LOW);
  analogWrite(powerPin, 255); 
}

Welcome to the forum

What happens if you change the parameters in the calls to the moveLinearActuator() function ?

Try 20 and -20. Does the actuator move more or less distance ?

the data sheet says ‘feedback’ is optional.
See what you have, and move on from that.

Since you seem to be measuring something, you may be able to calculate your movements,
Remember to keep track of the ‘current’ position, and let your code move to the desired ‘target’ position by itself. Easy if you do it right.

@cassierae please describe the mechanical part.

Is it like you have a car X feet away from the wall, and you want to move closer by 30 inches then back up 30 inches when? when what, and is the sensor on the "car"?

What you have now just goes for a time out and a time back every time the ultrasonic distance from (what to what?) is below 100. But it only does it once


// global constant
const int resetDistance = 250;  // reset detection mechanism outbound 


// then the last line of the loop
  if (distance > resetDistance) triggerCount = 0;
}

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