Linear Actuator (servo) with ultrasonic sensor

Hi all,

I have a project to complete. It involves a servo extending a linear actuator when the ultrasonic sensor returns a certain height.

For example, when the ultrasonic sensor returns less than 100, it extends the linear actuator, (at different speeds it will vary how long will take and would be useful to adjust it). Then once the the ultrasonic sensor returns above 100 it will retract the linear actuator.

I can get the ultrasonic sensor to run, and the linear actuator to run. I can get them to complete one task together, i.e when the sensor returns above 100 it does nothing. I can then get the servo to extend the linear actuator when the sensor returns below 100, however, it either gets stuck in a loop, and keeps infinitely extending the actuator, or stops running the sensor, which means that the when the sensor should return more than 100 and retract the actuator it doesn’t.

I have attached the code I have been fiddling with and the pseudocode, I would really appreciate any help with this.

Thanks so much!

/* Sweep

by BARRAGAN <http://barraganstudio.com>

This example code is in the public domain.



modified 8 Nov 2013

by Scott Fitzgerald

http://www.arduino.cc/en/Tutorial/Sweep

*/



#include <Servo.h>



const int trigPin = 9;

const int echoPin = 10;

Servo myservo;  // create servo object to control a servo

              // twelve servo objects can be created on most boards



// defines variables

long duration;

long distance;

int pos = 0;    // variable to store the servo position



void setup() {

myservo.attach(8);  // attaches the servo on pin 9 to the servo object

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

}



void loop() {
int path; 

//  myservo.write(85);

//  delay(2000);

//  myservo.write(100);

//  delay(2000);



// CODE FOR ULTRASONIC SENSOR - TO NOT CHANGE

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.34/2; // Output in mm





// CONTROLS SERVO

//if (distance > 100) {

//myservo.write(85);

//}

//if (distance < 100) {

//myservo.write(100);

//}

//

//if (distance == 3) {

//  myservo.write(92);

//}
// Testing the distance in flight mode, if higher than 10cm do nothing
if (distance > 100) {
path = 1;

}
// If the distance falls below 10cm extend leg and keep it there
else if (distance <100){
path = 2;
}
switch (path){
  case 1:
    myservo.write(90);
    break;
  case 2:
    myservo.write(80);
    delay(2000);
   do {
    myservo.write(90);
     
   
     
   
    }while (path =2);
     
}

// Prints the distance on the Serial Monitor

Serial.print("Distance: ");

Serial.print(distance, DEC);

Serial.print("\n");





delay(100);


}

I have never had much luck controlling a servo with the smiley face pin. If you post your code as shown in the how to use this forum-please read sticky, the forum software won't mess your code up like that.

Once the code gets into the case 2 do loop, how will it ever exit the loop?

Apologies. I have fixed that post, hopefully now easier to read.

And yes the loop is the problem, if I try attempt to put in something that will break the loop, e.g.

do nothing until the height is no longer below 100

It doesn't run the sensor, so it can never know if it's broken out.