i am trying to get my servo to sweep and stop in incremental stages say 0, 45, 90, 135, and 180. i can get the code to stop the servo to do what i want when it is seprate. and the same for the ping code works fine there as well. when i try to get them to work together my servo starts at 90 like i want then goes to 0 and pings and then just sits there even though i have the code for it to go to 180 in the loop. here is a copy of the code pls help me out i just switched from basic micro and havent gotten the chance to get familiar with arduino yet. the bad thing is this is a class project and i have 2 weeks to get it done, i cant figure out what im doing wrong and why the servo just sits at 0 and pings.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int pingPin = 8; //ping sensor pin number
int pos = 90; // variable to store the servo position
void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(7); // attaches the servo on pin 9 to the servo object
myservo.write(90); //center servo
delay(1000); //delay 1000ms
}
void loop(){
myservo.write(0); //move servo to pos 0
delay(1000);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.println();
delay(100);
}
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: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
delay (100);
myservo.write(180); //move servo to pos 0
delay(1000);
}
// myservo.write(90); //move servo to pos 0
// delay(1000);
it seems pretty simple to me i just cant figure out why it wont work what am i missing