Student First Project - Help and Advice needed

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)

I suspect that your robot will be faster if you rotate it 180 degrees about any axis.

Your robot shows two servo motors. Your code only deals with one.

Your code for measuring distance from a ping sensor, that does not appear in your picture, is in loop(). You really should learn to write functions, and put the code in a function. Validate that the function works, and then you can just call getDistance(), and KNOW that the code works. You can then concentrate on why your robot runs into things, instead of steering around them, without worry about whether getDistance() is the problem.

What do you think?

I think that you should post your code as described in the "how to use the forum" stickies so people don't have to download it.

Does the code do what you want? If not, what does it do and how is that different from what you want?

pinMode (echoPin, OUTPUT);
digitalWrite (echoPin, LOW);
pinMode (echoPin, INPUT);

What does that do? In all my years of using HCSR04 sensors I have never seen that.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

groundFungus:
I think that you should post your code as described in the "how to use the forum" stickies so people don't have to download it.

Does the code do what you want? If not, what does it do and how is that different from what you want?

pinMode (echoPin, OUTPUT);

digitalWrite (echoPin, LOW);
pinMode (echoPin, INPUT);




What does that do? In all my years of using HCSR04 sensors I have never seen that.

We are still working on the codes, but suggestions to improve it are welcome.
What is does is when one sensor detects an object within a range specified, the servo motor runs on one direction and same for the other sensor, as we use two of them.

Those three codes are for ultrasound, we used them based on the help sheet we have been given.

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Thanks a lot for your reply, I am now using the right method for codes now.

Regarding the circuit we do not have it now. But I will keep this post updated for each step we took.

Basic code is there.

Basically it is a sumo robot which shall stay in the field and those black-white detectors sensors are there to detect the doyo color and the ultra sound sensors measure the distance of the opposite robot, I hope that ot looks clear for now, soon I will add a simple concept too.

Here is the equipment we have so far.
Concept coming soon.
Thanks for your attention.

This pains me.
These 9v batteries work in a smoke detector where the current demands are very low.
Once you try to drive a motor with them, they quickly fail. The internal resistance drops the voltage and systems fail.

You might consider a battery that can deliver more current.

painsme.png

Hi,
Those servos for a start will need much more than that smoke detector battery to operate.

Tom.. :o

Hello All

@vinceherman : yes you were right! we used 2 but they failed again.

Here is the code and a pic of the setup which we are trying to make the robot based on.

Which battery you suggest?
What do you think about the code?
How to improve it all in your professional opinion?

#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);
}








}

Anyone interested to take a look at the code maybe?

Some questions about your hardware.
You show 2 servos. It looks like you will be using them as wheels. Did you purchase continuous rotation servos? Or modify your standard servos to be continuous rotation?

For the rest of the post, I will assume your servos are continuous rotation, and that the black rings in your pic are the wheels that will be attached to the servos.

Which battery you suggest?

I often see people use power banks for portable supplies.
pb.png

Looking at your code, I see you setting one servo to 0 and the other to 180, then using a delay for 5 seconds.
Why 5 seconds? That seams to be a long time to go without reading any sensors.

And you really don't need to use else with every if. E.g.

  if[(analogRead(A0) < 900)
  {
    myservoR.write(180);
    myservoL.write(0);
    delay(5000);
  }
  else   // just take these lines out
  {       // they aren't 
  }       // doing anything

Other than that can you explain what actually happens when you write(0) or write(180) to one of the servos? Some comments might perhaps help. There are many places where you only write to one of the servos and that seems odd.

Are the results from all your Serial.prints showing what you expect?

And of course the big question. What does it actually do now? And what do you want it to do that's different?

Steve

Hi,
What parts of your code do you have working?
I hope you are coding in stages.

  • Write and get working code just for first SR04.
  • Write and get working code just for second SR04.
  • Combine the two SR04 codes and get working, making sure that there is no acoustic cross talk.
  • Write and get working code just for the first IR sensor.
  • Write and get working code just for the second IR sensor.
  • Combine the two IR codes and get working.
  • Write and get working code just for one continuous servo.
  • Write and get working code just for the other continuous servo.
  • Combine the two servo codes and get working.
  • Then in stages combine each of the codes, one at a time , each time getting them working before adding new code.

This will make debugging so much easier.

You should be declaring ALL your I/O pinMode in void setup().
Your code appears to be missing sections, can you post a complete code in a new post please.

Any delay function STOPS ALL program processing, so delay(5000) makes your robot deaf and blind for 5Seconds while it is belting around at full speed.

Please look in Examples in the IDE, for "Blink without Delay" to show you how to delay with out stopping your program.

Also..

This may help to..

Tom... :slight_smile:

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 = 13;
const int echoPin2 = 12;

int irsens = A0;
int irsens2 = A5;

This suggests to me that you are going to change the pins that the IR sensors are connected to, during execution of your sketch.

Also, do really need a datatype with a range -32768 to 32767, to describe a pin number on a very small microcontroller?

Thanks to all for great feedbacks and suggestions/Questions :

@ vinceherman : Yes they are continuous rotation servos and yes you are right about the wheels. For the battery, because the design is aready done, we decided to use 3 x 9v batteries to see what will happen, we are still thinking about that, just found out in a pdf file of servos which showing using 4 x 1.5V batteries for servos and one 9v for sensors . do you know the current of those power banks by the way?
5 seconds could be changed later, it is for servo motors to not lead the robot out of the ring when the white edge is detected ( it's a 2 colored ring, black and white), which we are going to place it.

@slipstick : I explained it in the first post, 0 and 180 rotates the robot to detect another robot and go forward to it to hit it and push it outside the ring.

@ TomGeorge : What is acoustic cross talk between the 2 servos?

A question, What happens is when everything is connected with all the sensors, the 2 servos are not rotating at the same speed, do you know what is the problem?
We will use your commentsa and check the links and hopefully will post a new code soon.
Thank you very much.

@Thanks, when we use INT command it is -32768 to 32767 storage by default. There is no need to change it anyway .

Appreciate to all again!

Thanks, when we use INT command it is -32768 to 32767 storage by default. There is no need to change it anyway .

"int" (note case) is a datatype, not a command, and by default, when used to describe pin numbers, wastes memory.
Besides, the concept of negative pin numbers is plain silly.

ShayUK:
Thanks to all for great feedbacks and suggestions/Questions :

@ TomGeorge : What is acoustic cross talk between the 2 servos?

I am referring to the two SR04 ultrasonic units.
When they transmit their ping, the pings are on the same frequency.
So if you ping too close together you can get reflections in one SR04 that come from the other SR04. the fact that they are facing away from each other will help prevent this.
Tom... :slight_smile:

Thanks, AWOL, I checked with our guys and tutor, they said we have enough free memory and really no need to change that at this stage, but I personally agree with you want to know how it can be changed?

Dear Tom, I discussed it with my teammates now, the sensors are opposite direction, we still have a bit of that, not too much, we are going to test the robot, I will keep you posted about what happened in reality.

I also attached some pics of the progress, we used Fusion 360 to see and measure the parts and laser printed chassis etc... before building it.

The code is still the same, the very first post contains the code, as well as one of the posts which included it. after testing the physical robot, we will see if the code worked well enough or not.

Thanks for all your support!

Best Regards
Shay