Go Down

Topic: Problems incrementing servos while polling PING sensor (Read 354 times) previous topic - next topic

Hello,
     I am working on a project in which I want to control two servo motors that I want to use to rotate a PING sensor and A camera on each servo. I want the servos to stop when the ping sensors read an object withing a given distance. The overall objective is to create different "Zones" of distance with different responses for each zone. I am having trouble controlling the servos and can use any help.

here is what i have so far:
Code: [Select]
//References
#include <NewPing.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "MediaPlayer.h"
#include <ServoTimer2.h>

//Variables
int ledPin = 13;
int servo1pos = 1000;
int servo2pos = 0;
#define PING_PIN  11 
#define MAX_DISTANCE 100

//Objects
ServoTimer2 servo1;
ServoTimer2 servo2;
MediaPlayer mediaPlayer;
NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE);

//Setup
void setup() {
  pinMode (ledPin, OUTPUT);
  servo1.attach(7);
  servo2.attach(6);
  Serial.begin(9600);
}


//Loop
void loop() {
  int ping1dist;
  ping1dist = poll0();
  Serial.print("distance in centimeters = ");
  Serial.print(ping1dist);
  Serial.println();
if(ping1dist > 40)
{ int i = i;
  servo1.write(i);
  delay(150);
i =+ 50;}
  ping1dist = poll0();
    Serial.print(" Distance is greater than 100");
    Serial.println();

}
//Ping1 Subroutine
int poll0() {
  delay(30);                     
  unsigned int uS = sonar.ping();

int dist = uS / US_ROUNDTRIP_CM;
return dist;
}



Go Up