I'm trying program my PNG and servos to properly operate in sync. My servos are not modified so I'm simply trying to move them forward and backward while the PNG provides the distance measured in inches. When distance < 8 a function (or is it called a method?) is called to turn on the RED LED.
The code works successfully when separated...i.e. PNG will work without servos and servos will work without PNG...but when integrating the code, I get some strange output:
Running the code as you see it pretty much locks up the robot with the red light coming on and nothing else except some slight servo movement at startup.
In trying to troubleshoot where the problem might, lie, I disabled all the code associated with the right servo. That gives better results with the left servo going through its correct range of motion for one iteration only. The PNG does not appear to be operating through its normal cycle because the LED is not flashing like it should be.
Oddly, when I open the serial monitor, the left servo will go through its range of motion and will do so each time I open the monitor after closing it.
Running with USB power. Does my code below suggest any conflict?
#include <Servo.h>
Servo leftservo; // create servo object to control a servo
Servo rightservo;
// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 7;
const int avoidPin = 3;
const int avoidInch = 8;
int right=0;
int left=90;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
pinMode (avoidPin, OUTPUT); //establishes LED pin for indication of within the set number of inches
leftservo.attach(9); // attaches the left servo on pin 9 to the servo object
delay (1000);
rightservo.attach(8); // ditto for right servo on pin 8
}
void loop()
{
// 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.println(inches);
delay (50);
digitalWrite(avoidPin,LOW);
//drive servos
rightservo.write(right); // sets the servo position according to the scaled value
leftservo.write(left);
right=right++;
left=left--; //motors mounted in opposite configuration, thus values need to be inverse of each other
if (right==90) right=0;
if (left==0) left=90;
// test to see if object is within 'avoidinch' distance
// if (inches < avoidInch)
// {
// avoidance(); //call avoidance method
// }
}
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;
}
void avoidance()
{
//stop wheels from turning, flash red light
digitalWrite (avoidPin, HIGH);
// consider here storing the the last value, panning the robot to the right, taking new value
// if new value is less than old value, pan in opposite direction
// delay(10);
}