Hey y'all,
So I've been tinkering with a seeed Motor Shield, I can get a DC motor to run with the motordriver.goForward();
. The issue I am running into is incorporating the Ultrasonic sensors, not entirely sure if it has anything to do with the motor stutters.... This is the current code, a part of the code is commented out for testing purposes.
#include "MotorDriver.h"
#include "Arduino.h"
class Ultrasonic
{
public:
Ultrasonic(int pin);
void DistanceMeasure(void);
long microsecondsToInches(void);
private:
int _pin;
long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
_pin = pin;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delayMicroseconds(2);
digitalWrite(_pin, HIGH);
delayMicroseconds(5);
digitalWrite(_pin,LOW);
pinMode(_pin,INPUT);
duration = pulseIn(_pin,HIGH);
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
return duration/74/2;
}
Ultrasonic ultrasonicleft(7); //Left
Ultrasonic ultrasonicright(8); //Right
void setup()
{
Serial.begin(9600);
motordriver.init();
motordriver.setSpeed(200,MOTORB);
motordriver.setSpeed(200,MOTORA);
}
void loop()
{
long RangeInInchesLeft, RangeInInchesRight;
ultrasonicleft.DistanceMeasure();
ultrasonicright.DistanceMeasure();// get the current signal time on right sensor
RangeInInchesLeft = ultrasonicleft.microsecondsToInches();//convert the time to inches;
RangeInInchesRight = ultrasonicright.microsecondsToInches();//RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters
/*
if(RangeInInchesRight <= 10){
motordriver.stop();
Serial.println("Going Left. Current Ranges");
Serial.println("Right Sensor: " + RangeInInchesRight);
Serial.println("Left Sensor: " + RangeInInchesLeft);
motordriver.goForward();
motordriver.goLeft();
delay(1000);
} else if(RangeInInchesLeft <= 10){
//motordriver.stop();
Serial.println("Going Right. Current Ranges");
Serial.println("Right Sensor: " + RangeInInchesRight);
Serial.println("Left Sensor: " + RangeInInchesLeft);
motordriver.goForward();
motordriver.goRight();
delay(1000);
}
else if(RangeInInchesLeft < 7 && RangeInInchesRight < 7){
// while(RangeInInchesLeft < 15 && RangeInInchesRight < 15){
motordriver.stop();
//motordriver.goBackward();
Serial.println("Going Back Left. Current Ranges");
Serial.println("Right Sensor: " + RangeInInchesRight);
Serial.println("Left Sensor: " + RangeInInchesLeft);
motordriver.backLeft();
delay(2000);
//}
}*/
motordriver.goForward();
delay(2000);
//Serial.println("Going forward. Current Range");
// Serial.println("Right Sensor: " + RangeInInchesRight);
//Serial.println("Left Sensor: " + RangeInInchesLeft);
//if(RangeInInches > 15){
// motordriver.goForward();
// }
//delay(200);
}
I would greatly appreciate any advise/comments thanks!