Need help with servo controll

Hi you all,

I’m new to programming and I need help with my little project.
I’m trying to controle a servo with a ultra sonic sensor but I have no luck in getting it right.
I’ve tryed the “if” statement…the “if-else” one…the “for” one to. But or I get errors on compiling or the servo goes crazy and is turning byond 0 degrees.

I’m getting errors on these things “{” and “;” that are inside my loop after my “for” statement.
I just want the servo to get in a fixed position and stops there (and it must stay there) if a value is at a set distance and it must return to the first position and stay there if the value is not like the set distance.

I’ve seen a lot examples with different code but all use a potentialmeter in te setup…I don’t want that. It’s just a UNO, servo and the HC-SR04.

Could someone please help me out here? I don’t know what else to do!
Here’s the code so far:

#include <NewPing.h>
#include <Servo.h>

#define TRIGGER_PIN  5
#define ECHO_PIN     7
#define MAX_DISTANCE 200

Servo servo01;                           

int dist; 
int pos = 0;                              // Don't know if this is still usefull in this code. Last modification was to ad the integer DIST to make the "for" the "if" or the "else" statement easyer.
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup()
{
Serial.begin(115200);
servo01.attach(9);
}

void loop() 
{
  delay(100);
  unsigned int uS = sonar.ping();
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");
dist = uS / US_ROUNDTRIP_CM;

 for(dist != 0 && dist > 50)
     {
     servo01.write(15);
     delay(15);
     }
 for(dist != 0 && dist < 50)
     {
     servo01.write(80);
     delay(15);
     }
}
for(dist != 0 && dist > 50)

Did you mean if(dist != 0 && dist > 50), 'cos that don't look like no for loop.

Hi

for(dist != 0 && dist > 50)
{
servo01.write(15);
delay(15);
}
for(dist != 0 && dist < 50)
{
servo01.write(80);
delay(15);
}

I think you mean "if" not "for".
Just replace "for" with "if" and it will compile.

Hope this helps.

Tom........ :slight_smile:

I get errors on compiling or the servo goes crazy and is turning byond 0 degrees.

And can you post (in plain language, not code) what you want the program to do in response to the values you get from the ultrasonic sensor? That will help us to comment on your code, if it is still not doing what you expect it to do.

All the best

Ray

To AWOL and Tom: No I did try and use the "FOR" statement but I already see it's not the correct way.
http://arduino.cc/en/Reference/For

To Ray: I'm sorry..I'll try and explain it in words. I would like to set a servo's to position A when the measurement from the sensor is below 50cm. And it must stay there...even if it take 5 days before the distance changes. If the distance finally changes and goes over 50cm the servo must go to position B....and again it must stay there untill the distance changes to under 50 cm.
The ultra sonic sensor is constant reading the distance (with the ping action) and when it's going out off range (about byond 4 meters) it returns a 0 (zero). That's under 50 (0<50) so that would bring the servo to position A....but this action is not allowed!

I have tryed different ways to get it to work...but all with no luck.
I can some what read the code because of all the misstakes and research (cutting and pasting pieces of code) I've made but when it's not working I lack the knowledge to alter the code to make it work. So I'm hoping you guys./girls here would be so kind to help me out here. Not by actualy providing me with the working code but to give me a push in the right direction.
Because I do want to learn this...I just don't like reading! I'm more of a hands on kind of guy. I learn more from doing it then from reading about it.

I would like to set a servo's to position A when the measurement from the sensor is below 50cm. And it must stay there...even if it take 5 days before the distance changes. If the distance finally changes and goes over 50cm the servo must go to position B....and again it must stay there untill the distance changes to under 50 cm.
The ultra sonic sensor is constant reading the distance (with the ping action) and when it's going out off range (about byond 4 meters) it returns a 0 (zero). That's under 50 (0<50) so that would bring the servo to position A....but this action is not allowed!

OK, thanks for clarifying. Less than 50 => move to position A. Greater than (or equal to?) 50 => move to position B. No echo received (so reading is 0) => leave the servo at its current position until you get an echo?

If you change back to "if" statements, your code is heading in the right direction. Could you try that, and then post what specific compiler errors you get, or what problems you get if it runs?

All the best

Ray

I have tryed different ways to get it to work...but all with no luck.

Yeah, sorry about that, all AVR processors are immunised against luck before they leave the factory.

Don't trust luck, trust your debug prints.

Okay...I uploaded the program to the Uno. When there is an object close (less then 50 cm) to the sensor the servo doesn't move.
But when I move the object further away (byond 50 cm) or even completely away the servo rocks back and forewards....constandly.
Untill I bring the object back to within the 50 cm.

Also strange is that when it rocks the readings on the seriel monitor is giving strange readings:
48
48
49
50
52
"I removed the object (my hand) here"
136
33
19
135
34
13
135
36
12
135
35
14
136
33
13
"Pulled the ground from the breadboard here so disconnecting the servo"
136
136
135
135
136
136

It's like the UNO is getting feedback from the servo or are the strange reading from the servo drawing power?
Anyhow...How do I stop the rocking motion of the servo?

AWOL:

I have tryed different ways to get it to work...but all with no luck.

Yeah, sorry about that, all AVR processors are immunised against luck before they leave the factory.

Don't trust luck, trust your debug prints.

Hahahahaha :grin: Thanks for the tip!

Are you powering the servo from the Arduino? If so, try powering it from a different power supply from the Arduin (but with the GND connected to the Arduino GND).

Okay...but have to do that when I'm home. I'm at the office in the moment :wink: and don't have a Lipo with me.
But it's the goal to power it from a 11.1v lipo via a buck converter (from 11.v down to 5v) and not to use the UNO but a Micro to controll the servo's. Yes...servo's XD...as in 2 servo's. And servo 2 needs to rotate mirrored to servo 1.
It's for the landing gear from my Quadcopter. On take off when the quad is higher then 50 cm the gear goes up (servo 1 to 180 degree and servo 2 to 0 degree). On landings (below 50 cm) the gear must go done again (servo 1 to 100 degree and servo 2 to 80 degree).

But I don't want to incorporate servo 2 just yet.....first make the code work for one servo! Step by step.

Hi Hackscribble,

I connected a 11.1V; 1000mAh; 3-cell Li-Po to the buck converter and then to the + and - of a breadboard. The + to the Vin and the - to the GND on the UNO. I also connected the red and black wires from the servo and the sonic sensor to the breadboard and the signal wires to the UNO. The LED on the UNO is lit but the servo isn't doing anything. I can't check if the sonic sensor gets power like I can check the servo. If the servo was powered I couln'd move it (if I'm right?).

Am I doing something wrong?

Hi

As a test, try disconnecting the Vin supply from the Arduino, and power the Arduino instead from USB. Then run the program again and see what readings you get on the serial monitor from the ultrasonic sensor.

How do they compare with the readings you posted earlier?

EDIT

As a separate test, try the "sweep" example program from the servo library. Does the servo work OK with this program?