Hello All,
Thank you so much for such a great forums, hope this is the right place to post it.
I am in need of some help and advice to improve my project.
It should be basically a robot with wheels which uses ultra sonic sensor HC-SR04, on Arduino UNO, using 2 motors SM-S4303R, it should rotate, find the abstract and then move forward to it as fast as possible.
The main concept which I am going to follow is similar to the attached photo. basic code to start is here too and will be updated.
Any feedback is welcome about the code, how to make the code better and how to build the physical parts more efficient too .
Thanks.
#include <Servo.h>
Servo myservoR;
Servo myservoL;
const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 = 13;
const int echoPin2 = 12;
int irsens = A0;
int irsens2 = A5;
void setup() {
// put your setup code here, to run once:
pinMode (A0, INPUT);
pinMode (A5, INPUT);
Serial.begin (9600);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode (trigPin2, OUTPUT);
pinMode (echoPin2, INPUT);
myservoR.attach(9);
myservoL.attach(11);
Serial.begin (9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (analogRead(A0) <900)
{
myservoR.write(180);
myservoL.write(0);
delay(5000);
}
else
{
}
if (analogRead(A5) <900)
{
myservoR.write(0);
myservoL.write(180);
delay(5000);
}
else
{
}
long duration, cm;
digitalWrite (trigPin, LOW);
delayMicroseconds (2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin, LOW);
pinMode (echoPin, OUTPUT);
digitalWrite (echoPin, LOW);
pinMode (echoPin, INPUT);
duration = pulseIn (echoPin, HIGH);
cm = (duration/2)/29;
long duration2, cm2;
digitalWrite (trigPin2, LOW);
delayMicroseconds (2);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin2, LOW);
pinMode (echoPin2, OUTPUT);
digitalWrite (echoPin2, LOW);
pinMode (echoPin2, INPUT);
duration2 = pulseIn (echoPin2, HIGH);
cm2 = (duration2/2)/29;
Serial.print ("trackSensF = ");
Serial.print (analogRead(A0));
//delay (100);
Serial.print ("\t trackSensB = ");
Serial.print (analogRead(A5));
//delay (100);
Serial.print ("\t cm= ");
Serial.print (cm);
//delay(100);
Serial.print ("\t cm2= ");
Serial.println (cm2);
//delay(100);
//--------------------
if (cm > 5) {
myservoR.write(0);
// myservoL.write(50);
delay(15);
}
else {
myservoR.write(180);
// myservoL.write(180);
}
if (cm2 > 5) {
// myservoR.write(30);
myservoL.write(0);
delay(15);
}
else {
// myservoR.write(180);
myservoL.write(180);
}
}
sketch_nov13b.ino (810 Bytes)