Hello!
I've to develop a program in arduino for a automatic machine which should move on u- turns based on ultrasonic sensor readings, but not at same direction. each time it have to change the direction. The way to travel is given in an image. please help me to develop the algorithm along with logic and code.
Thanks in advance.
each time it have to change the direction.
Each time what happens?
How far have you got with your assignment?
PaulS:
Each time what happens?
Each time of turning the object has to change its direction. for example, if in first turn, it takes a right turn, then at 2nd turn, it must turn to the left. the image attached here shows the direction of movement.
the turns are 180 degree of the forward direction. If it goes forward, then at first turn, turning by left side it comes back and again goes forward by taking a U-turn in right.
So, even numbered turns are to one direction, and odd numbered turns are to the opposite direction.
AWOL:
So, even numbered turns are to one direction, and odd numbered turns are to the opposite direction.
yes. its also a point. thanks for getting me. please help.
Samiul_Basir:
the turns are 180 degree of the forward direction. If it goes forward, then at first turn, turning by left side it comes back and again goes forward by taking a U-turn in right.
Unless you are able to pick your machine up and turn it 180 degrees, you might think of doing two 90 degree turns to change direction.
Paul
const unsigned int TRIG_PIN=11;
const unsigned int ECHO_PIN=10;
const unsigned int BAUD_RATE=9600;
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(BAUD_RATE);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
int distance= duration/29/2;
float a= distance;
a= a*(-1);
if(duration==0){
Serial.println("Warning: no pulse from sensor");
}
else{
Serial.print("distance to nearest object:");
Serial.println(distance);
Serial.println(" cm");
}
delay(100);
if (a<=10){
//This code will turn Motor A clockwise for 2 sec.
analogWrite(motorPin1, 180);
analogWrite(motorPin2, 0);
delay(3000);
}
else if (a<=-10){
//This code will turn Motor A clockwise for 2 sec.
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 180);
delay(3000);
}
else
analogWrite(motorPin2, 180);
analogWrite(motorPin1, 180);
}
i've worked on it till this. but not sure if its the right way. a three wheeler, two motor will be used to run. calculating forword speed and dia, it takes 3 sec to take a full turn. Is the logic workable?
float a= distance;
a= a*(-1);
if (a<=10){
}
else if (a<=-10){
}
else
There was no point in storing the integral value in a float. EVERY value of a, since a is now negative, will be less than 10, so the else if and else clauses are useless.
PaulS
yes i'm concerned about it. what can i do in this case? as there any way to store the previous value or something else that could be efficient?
as there any way to store the previous value
Do you know the previous value to store?
Or, do you want to store the current value on this pass through loop() so that, on the next pass, you have the previous value available? THAT is certainly possible. The state change detection example does just that.