Sensor + Servo Machine (NEED HELP)

I'm trying to make a machine using sensors and a servo. Everything's perfectly fine but my servo isn't moving. Can anyone tell me why?

#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
Servo myservo;
const int led = 0;
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;
int noteduration = 500;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(echoPin, INPUT);
myservo.attach(11);
myservo.write(0);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Put Hand Under "); // Prints string "Distance" on the LCD
delay(10);
lcd.setCursor(0,1);
lcd.print("Arrow ");
delay(10);
if (distanceInch<5)
{
  delay(500);
  digitalWrite(led, HIGH);
  delay(1000);
  myservo.write(200);
}
else {
    digitalWrite(led, LOW);
    myservo.write(0);
}
}

The delay in your loop is too short to see the servo working.

1. You need to declare the following global variables as floats:

float distanceCm, distanceInch;

2. Try the above quoted code with the following parameter and see if Servo moves.

myservo.write(150);

3. If the Servo still does not move, try by putting the following code above the if()...

distnaceInch = 4.15;    //test purpose
if (distanceInch<5.0)
{
  delay(500);
  digitalWrite(led, HIGH);
  delay(1000);
  myservo.write(150);
}

That does not make sense. Angle values are limited to 0...180.
Please provide a link to the servo you are using. Especially digital servos ignore pulse values out of their limits.

Hi,
What are your sensors?
If you are using the ultrasonic sensor, try using NewPing library to do your ultrasonic sensor work/calcs.
Also in the IDE press CTRL and T keys to auto format your code to make it easier to read.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

apparently my servos are just broken. Thanks for the help though!