Hello,
I am trying to create a program in which the arduino pulls info from a proximity sensor. When the sensor is less than 15 inches away, a servo moves.
The problem occuring is twofold:
- the serial monitor only shows 1 number
- the servo just moves back and forth
Can anyone help me with this code?
Thanks, Aaron
#include <Servo.h>
Servo myservo;
const int pingPin = 11;
unsigned int duration, inches;
void setup() {
myservo.attach(9);
myservo.write(90);
Serial.begin(9600);
}
void loop() {
pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(pingPin, LOW); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
inches = duration / 74 / 2; // Convert to inches
Serial.println(inches); // Display result
delay(200); // Short delay
if (inches<15)
{
myservo.write(180);
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.