I have a standard servo and the PING))) sensor connected to pin 9 and 7 respectively. When running the demo code for both, they work as needed, but when I want to have the servo move and have the PING check the distance, the light on the ping blinks very dimly and the servo moves poorly.
I am running of USB power.
#include <Servo.h>
Servo myservo;
const int pingPin = 7;
int val=0;
void setup() {
// initialize serial communication:
myservo.attach(9);
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
val=val+1;
if (val==180)
{
val=0;
}
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
Serial.print(duration);
Serial.print( val);
Serial.println();
delay(10);
servo();
}
void servo()
{
myservo.write(val);
delay(15);
}