I apologize as I know many will laugh, but I am the true definition of a newbie, noob, whatever you want to call me, but I do appreciate the help in advance!
I got my cheap flea market car, one dc motor driving both rear wheels, one spring loaded to return to center dc motor to turn right or left motor. One ping (parallax) sensor. I just want to start with basics. I did try to search all over, but could not find a basic idea for what I am trying to do, although I think everyone else on the planet already knows how to. I just want the car to go forward until it "sees" something and then turn the front wheels, back up for more or less 90 degrees and continue forward. Again, I really appreciate someone pointing out the obvious to me. Thanks!! - Dwayne
int motorPin = 12;
const int pingPin = 7;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(motorPin, HIGH);
digitalWrite(9, LOW);
analogWrite(3, 95);
}
void loop() {
long duration, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
/*Serial.print(inches);
Serial.print("in, ");
Serial.println();
*/
digitalWrite(motorPin, HIGH);
analogWrite(11,0);
if (inches <= 12) {
digitalWrite(9, HIGH);
//delay(2000);
//int x = x + 1;
//do
//{
digitalWrite(motorPin, LOW);
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 100);
digitalWrite(8, LOW);
digitalWrite(13, LOW);
analogWrite(11, 255);
//} while (x < 10);
//for(int x = 0; x < 2000; x++);
//delay(2000);
//analogWrite(3,100);
//delay(3000);
}
//delay(250);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}