Hi, what we are asking here is for the line following car to follow the line and approach the parking systems. After approaching here, it is required to stop in front of the parking system and enter the parking lot as long as the parking lot is empty. We have written the code of the line tracking machine here and it is ready. It tracks the line, but we can't program the parking system. How can we do this? We plan to use 1 hc-sr04 sensor, we want to place it on the left side of the car so that when it stops in front of the parking space, it can check whether the parking lot is empty through this sensor placed on the left side. If it is empty, let him enter, if not, let him continue on his way. We have used 2 line tracking sensors in this machine. These sensors follow the black line. If you want, we can install the 3rd sensor, but we can't understand the logic of the program. We would appreciate it if you could at least tell us the Psevo code. We only need the pseudo code for the parking system, the code for the lane follower is ready. Here is park system's picture.
Thanks Regards Farhad
Assuming you have shown us a picture of the "line", I would program it to detect the "T" branch using the line sensor, then look for the opening to the parking area, also using the line sensor.
The line sensor must have at least 3 detectors for this to work.
I'm not so sure what is complex about your stated requirement above.
Do you already have the capability for the car to stop in front of each parking space? If so the rest is easy. Here is sample code for the HC-SR04 I found using a simple Google search:
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}
You will need to determine the maximum distance that defines what "occupied" means, given where your car is stopped and where another car might be in the parking space. For example, if that distance were 50cm then if your sensor detects an object closer that (<50cm) then the space is occupied and you should stop at the next available space and repeat.
Thanks for your reply but we know already this code. The most difficult part is stop in front of each parking space. We cant do this
For help with your code, first read the "How to get the best out of this forum" post, and follow the directions.
Post the code in line, using code tags, and explain what you expect to happen, and what happens instead.
Is this for some kind of competition? Isn't the point of it, to demonstrate your level of competence, or ability to research and solve a novel problem? How will that happen if someone feeds you the answer?
Thanks for your reply. We consider that. We declare before, we need just parking system pseove code. When i share the code, you will need explanation. But now you should say use the 3 sensor, detecth the "T" branch. Or you share the pseove code. It is enough for us. I dont wanna tire you to share my code
Is it that bad?
This is for my improvement. I wanna improve my skills, but I don't find the algorithm. I need your help
I wonder what that is.
Then, at least, please follow the forum guidelines to learn how to ask an effective question.
/*
keyestudio 4wd BT Car V2.0
lesson 10
Line Tracking Robot
*/
#define ML_Ctrl 4 //define direction control pin of B motor
#define ML_PWM 5 //define PWM control pin of B motor
#define MR_Ctrl 2 //define direction control pin of A motor
#define MR_PWM 9 //define PWM control pin of A motor
const int sensor_l = 3;//define the pin of left line tracking sensor
const int sensor_c = 7;//define the pin of middle line tracking sensor
const int sensor_r = 11;//define the pin of right line tracking sensor
int l_val,c_val,r_val;//define these variables
void setup() {
Serial.begin(9600);//start serial monitor and set baud rate to 9600
pinMode(ML_Ctrl, OUTPUT);//set direction control pin of B motor
pinMode(ML_PWM, OUTPUT);//set PWM control pin of B motor to OUTPUT
pinMode(MR_Ctrl, OUTPUT);//set direction control pin of A motor to OUTPUT
pinMode(MR_PWM, OUTPUT);//set PWM control pin of A motor to OUTPUT
pinMode(sensor_l,INPUT);//set the pins of left line tracking sensor to INPUT
pinMode(sensor_c,INPUT);//set the pins of middle line tracking sensor to INPUT
pinMode(sensor_r,INPUT);//set the pins of right line tracking sensor to INPUT
}
void loop()
{
tracking(); //run main program
}
void tracking()
{
//l_val = digitalRead(sensor_l);//read the value of left line tracking sensor
// c_val = digitalRead(sensor_c);//read the value of middle line tracking sensor
//r_val = digitalRead(sensor_r);//read the value of right line tracking sensor
if(digitalRead(sensor_l)==0 && digitalRead(sensor_r)==0)//if the state of middle one is 1, which means detecting black line
{
front();//car goes forward
}
else if(digitalRead(sensor_l)==0 && !analogRead(sensor_r)==0)
{
left();
}
else if(!digitalRead(sensor_l)==0 && digitalRead(sensor_r)==0)
{
right();
}
else if(!digitalRead(sensor_l)==0 && !digitalRead(sensor_r)==0)
{
back();
}
}
void front()//define the status of going forward
{
digitalWrite(ML_Ctrl,HIGH);//set direction control pin of B motor to HIGH
analogWrite(ML_PWM,100);//set PWM control speed of B motor to 70
digitalWrite(MR_Ctrl,HIGH);//set direction control pin of A motor to HIGH
analogWrite(MR_PWM,100);//set PWM control speed of A motor to 70
}
void back()//define the state of going back
{
digitalWrite(ML_Ctrl,LOW);//set direction control pin of B motor to LOW
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,LOW);//set direction control pin of A motor to LOW
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void left()//car turns left
{
digitalWrite(ML_Ctrl,LOW);//set direction control pin of B motor to LOW
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,HIGH);//set direction control pin of A motor to HIGH level
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void right()//define the right-turning state
{
digitalWrite(ML_Ctrl,HIGH);//set direction control pin of B motor to HIGH level
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,LOW);//set direction control pin of A motor to LOW
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void Stop()//define the state of stop
{
analogWrite(ML_PWM,0);//set PWM control speed of B motor to 0
analogWrite(MR_PWM,0);//set PWM control speed of A motor to 0
}//********************************************************* /*
keyestudio 4wd BT Car V2.0
lesson 10
Line Tracking Robot
*/
#define ML_Ctrl 4 //define direction control pin of B motor
#define ML_PWM 5 //define PWM control pin of B motor
#define MR_Ctrl 2 //define direction control pin of A motor
#define MR_PWM 9 //define PWM control pin of A motor
const int sensor_l = 3;//define the pin of left line tracking sensor
const int sensor_c = 7;//define the pin of middle line tracking sensor
const int sensor_r = 11;//define the pin of right line tracking sensor
int l_val,c_val,r_val;//define these variables
void setup() {
Serial.begin(9600);//start serial monitor and set baud rate to 9600
pinMode(ML_Ctrl, OUTPUT);//set direction control pin of B motor
pinMode(ML_PWM, OUTPUT);//set PWM control pin of B motor to OUTPUT
pinMode(MR_Ctrl, OUTPUT);//set direction control pin of A motor to OUTPUT
pinMode(MR_PWM, OUTPUT);//set PWM control pin of A motor to OUTPUT
pinMode(sensor_l,INPUT);//set the pins of left line tracking sensor to INPUT
pinMode(sensor_c,INPUT);//set the pins of middle line tracking sensor to INPUT
pinMode(sensor_r,INPUT);//set the pins of right line tracking sensor to INPUT
}
void loop()
{
tracking(); //run main program
}
void tracking()
{
//l_val = digitalRead(sensor_l);//read the value of left line tracking sensor
// c_val = digitalRead(sensor_c);//read the value of middle line tracking sensor
//r_val = digitalRead(sensor_r);//read the value of right line tracking sensor
if(digitalRead(sensor_l)==0 && digitalRead(sensor_r)==0)//if the state of middle one is 1, which means detecting black line
{
front();//car goes forward
}
else if(digitalRead(sensor_l)==0 && !analogRead(sensor_r)==0)
{
left();
}
else if(!digitalRead(sensor_l)==0 && digitalRead(sensor_r)==0)
{
right();
}
else if(!digitalRead(sensor_l)==0 && !digitalRead(sensor_r)==0)
{
back();
}
}
void front()//define the status of going forward
{
digitalWrite(ML_Ctrl,HIGH);//set direction control pin of B motor to HIGH
analogWrite(ML_PWM,100);//set PWM control speed of B motor to 70
digitalWrite(MR_Ctrl,HIGH);//set direction control pin of A motor to HIGH
analogWrite(MR_PWM,100);//set PWM control speed of A motor to 70
}
void back()//define the state of going back
{
digitalWrite(ML_Ctrl,LOW);//set direction control pin of B motor to LOW
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,LOW);//set direction control pin of A motor to LOW
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void left()//car turns left
{
digitalWrite(ML_Ctrl,LOW);//set direction control pin of B motor to LOW
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,HIGH);//set direction control pin of A motor to HIGH level
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void right()//define the right-turning state
{
digitalWrite(ML_Ctrl,HIGH);//set direction control pin of B motor to HIGH level
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,LOW);//set direction control pin of A motor to LOW
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void Stop()//define the state of stop
{
analogWrite(ML_PWM,0);//set PWM control speed of B motor to 0
analogWrite(MR_PWM,0);//set PWM control speed of A motor to 0
}//*********************************************************
this is my code but i dont add 3rd sensor and ultrasonic sensor yet. I wanna find algorithm then start to program
You obviously did not spend much time reading the forum instructions. Please go back and edit your post to place the code in code tags (explained in the forum instructions).
By the way, your code would be much easier to read if you auto-formatted it using ctrl-T in the IDE.
Excellent! Finally, someone who designs first, then codes! That is the recommended method of professionals.
So now, please also post the algorithms that you used to write the code you posted.
Or, is it just "Lesson 10" code that was provide with the kit?
In either case, it would be an excellent idea to post pseudocode of what you are currently considering. Then we can together, consider how the modification you are asking for can be accomplished.
Another thing, a very direct and thoughtful answer was provided in reply #2. Yet, you have not so far, respected it with any relevant response. Not even a question.
Please edit your post to add code tags.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
