# Help with a scholar project!

Good day to everyone!

I am wondering if you can help me in a project that a have at school. I have to build a cart that travels 10 meters straight and stops 10 cm before the wall. I challenged my teacher that I could find help on the internet and that technology can help us.

So, I bought some components:

-Arduino UNO - 365buying GY-271 HMC5883L Triple Axis Compass Magnetometer Sensor Module -SMAKN Ultrasonic Module Hc-sr04 Distance Sensor for Arduino UNO Mega R3 Mega2560 Duemilanove Nano Robot Xbee Zigbee -2 of Smart Car Tire/Wheel with Gear Motor for arduino/LPC/AVR/ARM car project - Motor shield OSEPP ---> http://www.frys.com/product/7726928#detailed

I need help with the code, I want to know how to program that the engines can gradually reduce its speed and stop at 10 cm from the wall. I don´t dont know if I have to use a PID.

If I was not very specific or if you need more information, tell me!

I need help with the code

Here's a start:

``````void setup()
{
}

void loop()
{
}
``````

All you have to do is add the stuff before setup(), the stuff in setup(), the stuff in loop(), and the stuff after loop().

I expect you should be able to do the whole project in about 20 minutes.

I want to know how to program that the engines can gradually reduce its speed

Write different speeds to the motor control pins based on whatever factors should influence the speed.

and stop

Well that would involve writing the speed that corresponds with 0 to the motor control pin.

at 10 cm from the wall.

You'll need to figure out how far you are from the wall.

I don´t dont know if I have to use a PID.

That would depend on how fast you intend to go, and how close to 10 centimeters from the wall you need to stop. 10 cm +/- 1 meter and 1 cm/day? No PID. 10 cm +/- 1 micron at 300 mph? Yep, you need PID.

Hi, what experience have you with programming and electronics?

Tom........ :)

I have to build a cart that travels 10 meters straight and stops 10 cm before the wall

Am I the only one thinking "990cm of string" ?

I challenged my teacher that I could find help on the internet and that technology can help us.

To me it sounds like you have been given this as an assignment and are looking for help.

Start with the basics. Wire the motors to the motor shield, connect the shield to the Uno, power the motors from an external power source (because the Arduino board cannot supply enough current) and write a program to turn the wheels at a fixed speed that can be varied by changing a variable in the program.

Come back and post your code when you have got that much working and we will provide help to move to the next stage. If you get stuck along the way then post the program and explain the problem. You will get plenty of help here but will be respected more if you try things for themselves even if you fail.

I wrote the following code:

``````//---------------------------------------------------------------
#include <AFMotor.h>
#include <NewPing.h>
#include <Wire.h>
#include <PID_v1.h>
//---------------------------------------------------------------
#define TRIGGER_PIN  8
#define ECHO_PIN     9
#define MAX_DISTANCE 500
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int speedLeft;
int speedRight;
int direc = 130;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int compassValue;
double Setpoint, Input, Output1, Output2;

PID myPID1(&Input, &Output1, &Setpoint, 20, .1, .1, REVERSE);
PID myPID2(&Input, &Output2, &Setpoint, 20, .1, .1, DIRECT);
//---------------------------------------------------------------
void setup() {
Serial.begin(9600);
Setpoint = 10;
motor1.run(FORWARD);
motor2.run(FORWARD);
myPID1.SetMode(AUTOMATIC);
myPID2.SetMode(AUTOMATIC);
if (!mag.begin()) {
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while (1);
}
}
//---------------------------------------------------------------
void loop() {
compassValue = CompassSensor();
DirectionWheel (compassValue);
Input = UltrasonicPing();
myPID1.Compute();
myPID2.Compute();
motor1.setSpeed(Output1);
motor2.setSpeed(Output1);
delay(30);

}
//---------------------------------------------------------------
``````

(I have a method to use a compass because I want my car to run straight, I also have the metod for the Ultrasonic)

But I dont know how to go reverse when distance is less than 10 CM. I mean, my PID should be able to stop 10 cm before the wall.

Can someone help me? :c

Moderator edit: [code][/code] tags added. (Nick Gammon)

Hi, does the sketch you posted work?
Its just that I’m surprised at your astronomical learning curve, from help! ! ! to here’s my code.

Tom…

It works, but I cant run reverse :( ANS: I have experience with electronics, but not programming, I am just starting

Thank you Tom, it is because I am investing a lot of time reading posts and examples from this forums :) (Its 1:35 AM here :p)

It works, but I cant run reverse

You need a function to measure the distance ahead and return the value. Call the function from loop() and if the distance is less than 10cm stop the motors or even reverse them and stop executing the PID.

Is your problem that you do not know how to reverse the motors ?

Hi UKHeliBob! My problem is that I have to program a PID (I can even use P, I think its easier). So, my car has to run 10 meters straight and then, it must stop 10 cm before the wall. I am using the ultrasonic to mesure the distance between the wall and my car in order to stop at the desired distance. I am also using a compass module to run straight, but I want first to program a P that works u.u

Thank you

PaulS: All you have to do is add the stuff before setup(), the stuff in setup(), the stuff in loop(), and the stuff after loop().

Sounds straightforward enough.

but I want first to program a P that works u.u

Can you please translate that into English for me.

He wants a P.

Sorry, I am better in spanish than english. I mean that I want to program a good code for PID that can help me with my issue.

So, is your current problem making the vehicle run straight or making it stop ?

Suggested steps :

Make it run straight using a PID Make it stop using the distance in front When that is working consider using the distance in front feed a PID to slow down the vehicle as it approaches the target. This does not have to be the same PID as the one controlling the steering.

My PID actually works, but my car just go straight and stop 10 cm before the wall, BUT it can not go reverse if the distance is less than 10 cm. I mean, it must stop 10 cm before the wall and stay in the position.

No, the project is with a PID, no with a line follower.
Thanks

I have a scholar project and I need help with part of the code
I have to build a car that must run 10 meters and stop 10 cm before the wall.
I am using an ultrasonic to mesure the distance between the car and the wall; I am also using a compass module in order to run straight.

With the code attached, my car can run and stop 10 cm before the wall, BUT it can not go reverse if the distance is less than 10 cm, I am asking for some help about the PID that I am using, maybe I am not using it well.

Thank you!

EstebanPIDv02.zip (1.93 KB)

@ CB +1… I had a mouth full of coffee when I read “990 cm of string”…
My monitor needed cleaning anyway…
@ OP… removing power from the motors and shorting the motor leads (Each one) will use the motors to form a dynamic brake… in effect a motor is a generator and shorting it’s terminals while the armature is in motion forms a dynamic brake…

Doc

When the wall is perhaps 20 cm away.. directly (no PID) control the motor to creep to the 10 cm distance and stop...

...