Go Down

Topic: Student First Project - Help and Advice needed (Read 1 time) previous topic - next topic

ShayUK

Nov 14, 2017, 12:35 pm Last Edit: Nov 28, 2017, 01:51 pm by ShayUK
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.

Code: [Select]



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








}
 



PaulS

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.
The art of getting good answers lies in asking good questions.

groundFungus

Quote
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?

Code: [Select]
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.

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.. :)
Everything runs on smoke, let the smoke out, it stops running....

ShayUK

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?

Code: [Select]
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.

ShayUK

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.. :)
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.

ShayUK

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.

ShayUK

#7
Nov 20, 2017, 04:04 pm Last Edit: Nov 20, 2017, 04:19 pm by ShayUK
Here is the equipment we have so far.
Concept coming soon.
Thanks for your attention.


vinceherman

#8
Nov 20, 2017, 04:48 pm Last Edit: Nov 20, 2017, 04:48 pm by vinceherman
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.


TomGeorge

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

Tom.. :o
Everything runs on smoke, let the smoke out, it stops running....

ShayUK

#10
Nov 28, 2017, 01:43 pm Last Edit: Nov 28, 2017, 01:49 pm by ShayUK
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?

Code: [Select]

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








}





ShayUK

Anyone interested to take a look at the code maybe?

vinceherman

#12
Dec 01, 2017, 06:27 pm Last Edit: Dec 01, 2017, 06:33 pm by vinceherman
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.

Quote
Which battery you suggest?
I often see people use power banks for portable supplies.



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.

slipstick

And you really don't need to use else with every if. E.g.
Code: [Select]

  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


TomGeorge

#14
Dec 02, 2017, 07:06 am Last Edit: Dec 02, 2017, 07:08 am by TomGeorge
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..

https://www.arduino.cc/reference/en/language/structure/control-structure/if/

This may help to..

https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up