Go Down

Topic: sweep and ping (Read 1 time) previous topic - next topic


May 22, 2011, 04:54 pm Last Edit: May 22, 2011, 05:05 pm by drab Reason: 1
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.

Code: [Select]
#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:
  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
// 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);
  digitalWrite(pingPin, HIGH);
  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("in, ");

  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



//  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


and then just sits there even though i have the code for it to go to 180 in the loop.

No, you don't.  At least not in the code you provided.  The code has it go to 90 in setup() and 0 at the top of loop() so it is doing exactly what this code tells it to do.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up