Jump to content
ninjaplatypus42

MSP430 F5529 ultrasonic sensor for servo control

Recommended Posts

I have a code from an arduino to control servo position based on the distance output of the ultrasonic sensor. The servo just turns back and forth and there's no serial output at all. Everything is attached to the appropriate pins. I'm literally brand new at this so i really have no idea how to trouble shoot. Any help would be appreciated. This is the code i'm using.

#include <Servo.h>


//Setup the variables for the HC-SR04

const int trigPin = 3;

const int echoPin = 4;

// create servo object to control a servo

// a maximum of eight servo objects can be created

Servo myservo;



// variable to store the servo position

int pos = 0; 

void setup() {

// initialize serial communication:

Serial.begin(9600);



// attaches the servo on pin 9 to the servo object

myservo.attach(2); 



}

void loop()

{

// establish variables for duration of the ping,

// and the distance result in inches and centimeters:

long duration, inches, cm;



// The sensor is triggered by a HIGH pulse of 10 or 
// more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);



// Read the signal from the sensor: a HIGH pulse whose

// duration is the time (in microseconds) from the sending

// of the ping to the reception of its echo off of an object.

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);



// convert the time into a distance

inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);



//Tell the Arduino to print the measurement in the serial console

Serial.print(inches);

Serial.print("in, ");

Serial.print(cm);

Serial.print("cm");

Serial.println();



// This if-else statement tells the Arduino at what distance

// it should trigger the servo, what the servo should do,

// and what it should do if the distance is too far away.

if (inches <= 24) {sweep(3);



}

else if (inches >= 24) {myservo.write(pos);

}



// Tell the Arduino to wait 0.10 seconds before pinging the

// Ultrasonic Sensor again.

delay(100);

}

// Converts the microseconds reading to Inches

long microsecondsToInches(long microseconds)

{

// According to Parallax's datasheet for the PING))), there are

// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

// second). This gives the distance travelled by the ping, outbound

// and return, so we divide by 2 to get the distance of the obstacle.

// See: www.parallax.com/.../28015-PING-v1.3.pdf

return microseconds / 74 / 2;

}

//Converts the Microseconds Reading to Centimeters

long microsecondsToCentimeters(long microseconds)

{

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

//This sub-routein is what dictates the movement of the servo

void sweep(int NUM_OF_CYCLES)



{

// Tells the Arduino to start this loop at 0 and incriment it by 1

// each time the loop completes. This is how the Arduino knows to

// stop the loop when a specific number of the 
// Sweep routein has been ran.

for (int j=0; j<NUM_OF_CYCLES; j++)
// goes from 0 degrees to 180 degrees
for(pos = 0; pos < 180; pos += 1) 
{ // in steps of 1 degree
// tell servo to go to position in variable 'pos'
myservo.write(pos); 
// waits 15ms for the servo to reach the position delay(10); } 
// goes from 180 degrees to 0 degrees 
for(pos = 180; pos>=1; pos-=1) { 
// tell servo to go to position in variable 'pos' 
myservo.write(pos); 
// waits 15ms for the servo to reach the position 
delay(10); } }}

Edited by bluehash
[ADMIN] Please use code tags next time.

Share this post


Link to post
Share on other sites

@@ninjaplatypus42

 

I suggest trying to getting the separate parts working and then combining. Try getting serial wpoking first by putting a print statement right after begin. Then perhaps get the ultrasonic sensor. Etc... Break it into pieces and isolate the problems.

Share this post


Link to post
Share on other sites
Welcome!
 
First thing you should do is learn to use the code tag for wrapping code into a more readable block of text. The code tag can e accessed by clicking the button above marked "<>", then paste your code in.... like:
#include <Servo.h>

//Setup the variables for the HC-SR04
const int trigPin = 3;
const int echoPin = 4;

// create servo object to control a servo
// a maximum of eight servo objects can be created
Servo myservo;

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

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // attaches the servo on pin 9 to the servo object
  myservo.attach(2); 
}

void loop(){
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The sensor is triggered by a HIGH pulse of 10 or 
  // more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  //Tell the Arduino to print the measurement in the serial console
  Serial.print(inches);
  Serial.print(" in, ");
  Serial.print(cm);
  Serial.println(" cm");

  // This if-else statement tells the Arduino at what distance
  // it should trigger the servo, what the servo should do,
  // and what it should do if the distance is too far away.
  if (inches <= 24) {
    sweep(3);
  }
  else if (inches >= 24) {
    myservo.write(pos);
  }
  // Tell the Arduino to wait 0.10 seconds before pinging the
  // Ultrasonic Sensor again.
  delay(100);
}

// Converts the microseconds reading to Inches
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second). This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: www.parallax.com/.../28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

//Converts the Microseconds Reading to Centimeters
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

//This sub-routein is what dictates the movement of the servo
void sweep(int NUM_OF_CYCLES){
  // Tells the Arduino to start this loop at 0 and incriment it by 1
  // each time the loop completes. This is how the Arduino knows to
  // stop the loop when a specific number of the 
  // Sweep routein has been ran.
  for (int j=0; j<NUM_OF_CYCLES; j++) {
    // goes from 0 degrees to 180 degrees
    for(pos = 0; pos < 180; pos += 1){
      // in steps of 1 degree
      // tell servo to go to position in variable 'pos'
      myservo.write(pos); 
      // waits 15ms for the servo to reach the position
      delay(10);
    } 
    // goes from 180 degrees to 0 degrees 
    for(pos = 180; pos>=1; pos-=1) { 
      // tell servo to go to position in variable 'pos' 
      myservo.write(pos); 
      // waits 15ms for the servo to reach the position 
      delay(10); 
    } 
  }
}

Next, I'd suggest moving some of the variables out of "loop()" and defining pin functions in "setup()".

 

You define pos = 0 at the top. 

In sweep(), you use pos to go from 0 to 180 in one loop. In the next loop you start pos at 180, with a conditional of "any number greater than or equal to 1", and decrement by 1. So, every time you enter sweep(), the servo will move to 180, then move maybe 1 degree backwards... and that is it.

And in loop(), since pos will be either 180 or 179 depending on things, it won't move since it was left at that position in sweep().

 

Or at least that's how I'm interpreting things based on

for(pos = 180; pos>=1; pos-=1) {
      // tell servo to go to position in variable 'pos'
      myservo.write(pos);
      // waits 15ms for the servo to reach the position
      delay(10);
    }

which is the final loop executed before exiting the function sweep().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×