Hello. Working with a clone HC_SR04, I got it to work with out having the echoPin stuck on HIGH. Now implementing it into a new sketch. I am not sure as to why my obstacle avoiding bot will run full speed Forward and not turn or go Backwards. The code here is a little chopped up at the end Cause I have been Moving things around and working on bracket placement. Any constructive input would be swell Thanks.
//keyes l298 shield 1 hc_sr04
//defne pins
#include <NewPing.h>
#define trigPin 12
#define echoPin 11
#define MAX_DISTANCE 200
//Motor A
int enableA = 10;
int pinA1 = 3;
int pinA2 = 2;
//Motor B
int enableB = 9;
int pinB1 = 5;
int pinB2 = 4;
//speed
int pwm_speed = 255;
void setup() {
Serial.begin (9600);
//configure pin modes
pinMode (enableA, OUTPUT);
pinMode (pinA1, OUTPUT);
pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
pinMode (pinB1, OUTPUT);
pinMode (pinB2, OUTPUT);
}
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
unsigned int uS;
int triggerDistance = 15;
int distance;
int scan() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if (uS == 0 && digitalRead(echoPin) == HIGH) {
pinMode(echoPin, OUTPUT);
digitalWrite(echoPin, LOW);
delay(100);
pinMode(echoPin, INPUT);
}
}
//motor function
void Forward() {
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW);
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
analogWrite (enableA, pwm_speed);
analogWrite (enableB, pwm_speed);
}
void Backwards() {
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, HIGH);
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, HIGH);
analogWrite (enableA, pwm_speed);
analogWrite (enableB, pwm_speed);
}
void Left() {
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW);
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, HIGH);
analogWrite (enableA, pwm_speed);
analogWrite (enableB, pwm_speed);
}
void Right() {
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, HIGH);
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
analogWrite (enableA, pwm_speed);
analogWrite (enableB, pwm_speed);
}
void Stop() {
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, LOW);
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, LOW);
analogWrite (enableA, pwm_speed);
analogWrite (enableB, pwm_speed);
}
void loop() {
// test
/*
Forward();
Serial.println ("Forward");
delay(1000);
Backwards();
Serial.println("Backwards");
delay(1000);
Right();
Serial.println ("Right");
delay(1000);
/////////////////////////////////
*/
distance = scan();
if (distance >= triggerDistance) {
Backwards();
delay(700);
Serial.println("Backwards");
Right();
delay(300);
Serial.println("Right");
Stop();
Serial.println("Stopping");
distance = scan();
if ( distance > triggerDistance) {
Forward();
Serial.println("Forward");
}
else {
Forward();
Serial.println("Forward");
}
}
}