I need a bit of help. I have this simple little autonomous robot that seems to be working pretty well except I am having some timing issues in the code. I am using the timedaction library to have an 'interrupt' for the ultrasonic sensor sweep with a servo motor that seems to not have enough time to function properly. The code is really short so I am just pasting it all on here. I am doing this because sometimes fresh eyes can look at something and pinpoint my error. Basically I have a servo that is sweeping from 45 to 90 to 135 degrees and every time it stops, I ping the ultrasonic sensor and store the value in an array. The motors move with respect to the longest distance available. If anyone sees anything that would cause this to have issues please let me know. Everything seems to be working, but the servo is not sweeping all the way to the right and the robot is slow to respond. It will sometimes work great but sometimes hit the wall before turning. I figure that it works sometimes because the signal is received faster allowing enough time within the interrupt.
Thanks for the help in advance.
#include <TimedAction.h>
#include <Servo.h>
int enable = 53;
int dir1 = 49;
int pwm1 = 2;
int dir2 = 51;
int pwm2 = 3;
int servo = 4;
int i = 0;
int z = 0;
Servo myservo;
const int pingPin = 5;
long Ranging[3];
//int x = 0;
TimedAction repeatScan = TimedAction(1000, Scan);
void setup()
{
Serial.begin(9600);
pinMode(enable, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(pwm1, OUTPUT);
pinMode(dir2, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(servo, OUTPUT);
myservo.attach(servo); // attaches the servo on pin 9 to the servo object
}
void loop()
{
repeatScan.check();
}
long duration;
long cm;
long ping(){ // ping ultrasonic sensor
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
//delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
//delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
delay(300);
return cm;
// r m l
}
void Scan(){ // scan function for servo motor
int pos;
myservo.write(45);
Ranging[0] = ping();
myservo.write(90);
//delay(10);
Ranging[1] = ping();
myservo.write(135);
//delay(10);
Ranging[2] = ping();
if((Ranging[2] >= 70)){
digitalWrite(enable, HIGH);
digitalWrite(dir1, LOW);
digitalWrite(dir2, LOW);
forward();
delay(25);
}
else if (Ranging[0] >= 70) {
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
forward();
delay(25);
}
else if (Ranging[1] >= 70) {
digitalWrite(dir1, LOW);
digitalWrite(dir2, HIGH);
forward();
delay(25);
}
else{
mstop();
}
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void forward(){
while(i <= 30){
analogWrite(pwm1, i); // PWM at 20% is 51
analogWrite(pwm2, i); // PWM at 20% is 51
i ++;
delay(25);
}
if(i == 30){
analogWrite(pwm1, 51); // PWM at 20% is 51
analogWrite(pwm2, 51); // PWM at 20% is 51
}
}
void mstop(){
i=51;
bool stop = false;
if(stop == false){
while(i >= 51){
analogWrite(pwm1, i); // PWM at 20% is 51
analogWrite(pwm2, i); // PWM at 20% is 51
i = i - 1;
delay(10);
digitalWrite(enable, LOW);
stop = true;
}
}
if(stop == true){
i = 0;
}
}