Help on improving my bot

Hello everyone Started with arduino ,and the forum has been helpful.
I read a few posts on Obstacle avoidance using HCSR04 and it's done :slight_smile: (image attached)!

Works fine with basic obstacle avoidance but i have a few doubts :-

  1. Battery dies out fast (used two 9V batteries,also code attached). Is there any low power mode(like msp430) which i can utilise ?

  2. Any algorithm which is a bit more efficient ? or any suggestions with improvements for the code is highly appreciated.

Thank you :slight_smile:

#include <NewPing.h>
#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
const int Motor2Pin1 = 6;
const int Motor2Pin2 = 9;
const int Motor1Pin1 = 10;
const int Motor1Pin2 = 11;
int a;
int b;

void setup()
{

  pinMode(Motor1Pin1, OUTPUT);   
  pinMode(Motor1Pin2, OUTPUT);   
  pinMode(Motor2Pin1, OUTPUT);   
  pinMode(Motor2Pin2, OUTPUT);  
  Serial.begin(9600);

}



void loop()
{
  a=sensor(b);
  motor(a);
  delay(100);
}




int sensor(int b)
{                   
  unsigned int uS = sonar.ping(); 
  b = (uS / US_ROUNDTRIP_CM);
  return b;
}



int motor(int a)
{
  int val=a;
  if(val==0 || val>20)
    GoForward();

  else if(val<19&&val>14)
  {
    GoRight();

  }

  else if(val <14&&val>5)
  {
    GoLeft();

  }

  else if (val>0&&val<5)
  {
    GoBackward();

  }
}




void GoForward()
{
  digitalWrite(Motor1Pin2, HIGH);
  digitalWrite(Motor1Pin1, LOW);
  digitalWrite(Motor2Pin2, LOW);
  digitalWrite(Motor2Pin1, HIGH);
  delay(100);
}

void GoBackward(){

  digitalWrite(Motor1Pin2, LOW);
  digitalWrite(Motor1Pin1, HIGH);
  digitalWrite(Motor2Pin2, HIGH);
  digitalWrite(Motor2Pin1, LOW);
  delay(100);
}
void GoRight(){
  analogWrite(Motor1Pin2, 255);
  analogWrite(Motor1Pin1, 0);
  analogWrite(Motor2Pin2, 100);
  analogWrite(Motor2Pin1, 0);
  delay(100);
}
void GoLeft(){
  analogWrite(Motor1Pin2, 0);
  analogWrite(Motor1Pin1, 100);
  analogWrite(Motor2Pin2, 0);
  analogWrite(Motor2Pin1, 255);
  delay(100);
}

final.zip (812 Bytes)

  1. Battery dies out fast (used two 9V batteries,also code attached). Is there any low power mode(like msp430) which i can utilise ?

No. You can use something other than pathetic little batteries, though.

  1. Any algorithm which is a bit more efficient ?

More efficient than what? Pancake batter?

Thank you and i was referring to my gibberish code.

Thank you and i was referring to my gibberish code.

Which you have not shared. So, more efficient than your code is an undefined unknown.

Use an AC/DC converter or re chargeable power sources , The weird chemistries Nicd Nimh Lipo Li ion whatever .
Upgrade it with using little motors and better drivers .

PaulS:
Which you have not shared. So, more efficient than your code is an undefined unknown.

I'm sorry , it said code uploaded i didnt know it was a blank one. Here is the code. Please let me know if its uploaded now. Thank you

finex.zip (779 Bytes)

void loop()
{
sensor(b);
delay(100);
a=sensor(b);
delay(100);
motor(a);
delay(100);
}

Well, you were right. It is pathetic.

Start with some names that follow the Arduino (and every other intelligent programming paradigm) convention of function names that consist of a noun and a verb (like digitalRead(), analogWrite(), etc.).

One can only guess at what sensor() is supposed to do. Clearly, calling it with and without storing the return value is contradictory. If the function returns a value, discarding it is silly.

The robot() will do NOTHING during the stickYourThumbUpYourAssAndDoNothing() (aka delay()) call.

int sensor(int b)
{ delay(50);                    
  unsigned int uS = sonar.ping(); 
  b = (uS / US_ROUNDTRIP_CM);
  return b;
}

Assigning a value to the input argument does not make sense.

int motor(int a)
{
 int val=a;
  if(val==0 || val>20)
   GoForward();

Copying the input value, and then ignoring the input value, while never changing the copy, does not make sense.

Posting code that is so poorly indented does not make sense. Especially when Tools + Auto Format is so easy to use.

While I'm at it, zipping a single ino file, whose size is well under the cut-and-paste in a code box limit, and attaching it does not make sense.

Thanks for the extremely valuable suggestions (oh wait i have to search for it in that heap of criticism).
Good way to encourage a beginner, Says a lot about you sir. Thank you.

The bot works well (or maybe its Lokis illusion from asgard) and the 'stickYourThumbUpYourAssAndDoNothing() ' was intended to make the motor stay in that position for a while- was experimenting with delays and the additional two delay statements was to see if battery life improves (read it from some website).

nice way to flame up a thread, Good job.

Thank You :slight_smile:

Here's the code for those who can't be bothered to download and unzip it.

#include <NewPing.h>
#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

const int Motor2Pin1 = 6;
const int Motor2Pin2 = 9;

const int Motor1Pin1 = 10;
const int Motor1Pin2 = 11;
int a;
int b;

void setup()
{
  pinMode(Motor1Pin1, OUTPUT);   
  pinMode(Motor1Pin2, OUTPUT);   
  pinMode(Motor2Pin1, OUTPUT);   
  pinMode(Motor2Pin2, OUTPUT);  
  Serial.begin(9600);

}



void loop()
{
  sensor(b);
  delay(100);
  a=sensor(b);
  delay(100);
  motor(a);
  delay(100);
}




int sensor(int b)
{ 
  delay(50);                    
  unsigned int uS = sonar.ping(); 
  b = (uS / US_ROUNDTRIP_CM);
  return b;
}



int motor(int a)
{
  int val=a;
  if(val==0 || val>20)
    GoForward();

  else if(val<19&&val>14)
  {
    GoRight();

  }

  else if(val <14&&val>5)
  {
    GoLeft();

  }

  else if (val>0&&val<5)
  {
    GoBackward();

  }
}




void GoForward()
{
  analogWrite(Motor1Pin2, 100);
  analogWrite(Motor1Pin1, 255);
  analogWrite(Motor2Pin2, 100);
  analogWrite(Motor2Pin1, 255);
  delay(100);
}

void GoBackward(){
  analogWrite(Motor1Pin1, 100);
  analogWrite(Motor1Pin2, 255);
  analogWrite(Motor2Pin1, 100);
  analogWrite(Motor2Pin2, 255);
  delay(100);
}

void GoLeft(){
  analogWrite(Motor1Pin1, 100);
  analogWrite(Motor1Pin2, 255);
  analogWrite(Motor2Pin2, 100);
  analogWrite(Motor2Pin1, 255);
  delay(100);
}

void GoRight(){
  analogWrite(Motor1Pin2, 100);
  analogWrite(Motor1Pin1, 255);
  analogWrite(Motor2Pin1, 100);
  analogWrite(Motor2Pin2, 255);
  delay(100);
}

void Stop(){
  digitalWrite(Motor1Pin2, LOW);
  digitalWrite(Motor1Pin1, LOW);
  digitalWrite(Motor2Pin1, LOW);
  digitalWrite(Motor2Pin2, LOW);
  delay(100);
}

Hint: A really good way of improving your code is by getting it to tell you what it is doing.

Thank you sir

You can use non-gearboxed HIGH RPM (or geared for HIGHER RPM) motors , so the bot would move fast . Then make a fast-reaction code so you'd have something totally fast-reaction .
To make your arduino work faster , there are two things you can do :
1- Overclock
2- cool the system down
And as a blog post demonstrated , using liquid nitrogen can make the uno to go up to 64 Mhz which is 4 times faster .
You can safely (I AM NOT RESPONSIBLE FOR ANYTHING) overclock a uno to 20Mhz as the Atmega328 can go up to 20 Mhz .

Hi Pranav_Srivatsa,
We really need more info from you! Like what motors, batteries!! which could be your problem. If you can show us how you have everything wired up.

Regards

Mel.

Paul S does have a point though. (And it's a consistent one!) It can be hell to have to wade past a page of inane posts to find anything useful or consider offering any help with. The Forum really does need to be interesting technically not just amusing.(Though a little levity is a always a plus). The moderator should reject the following type of posts:
1 Help Me. With whatever I think I don't know but didn't think about.
2 Any mention of college assignments. That should go into a "I will pay handsomely for someone to cheat for me because my brain hurts too much" topic.
3 Any code with a delay() to be banned from the Forum!

if asking for specific help then post code with first post. But before you hit that post button do imagine you are someone else reading it and criticise severely. You will then definitely have a better experience on the forum. (Paul is sometimes quite forthcoming).
Anyway, getting shot down in flames is a great opportunity to get over your limitations. The worst you'll suffer is gravel rash. Learn from it.

Hi,
Yes I agree to a point! Not sure about the delay() as it's easy for beginners to use.
Yes I have noticed, some that don't just want a guide or advice but want it all, we're not a code writting service, well I could be if the price was right (I'm not really that good).

What really gets me is when we all help offer our advice and sugesstions! Only to never get a reply from the poster who has now got the answer they wanted or has just gave up because their head is spinning..

And cross-posters what a waste!! Or the one who follows his post 2 hours later with another, because he's had no reply yet.

Perhaps some forum rules need looking at!!

Regards

Mel.

Arman5592:
You can use non-gearboxed HIGH RPM (or geared for HIGHER RPM) motors , so the bot would move fast . Then make a fast-reaction code so you'd have something totally fast-reaction .
To make your arduino work faster , there are two things you can do :
1- Overclock
2- cool the system down
And as a blog post demonstrated , using liquid nitrogen can make the uno to go up to 64 Mhz which is 4 times faster .
You can safely (I AM NOT RESPONSIBLE FOR ANYTHING) overclock a uno to 20Mhz as the Atmega328 can go up to 20 Mhz .

Seems intresting, thanks will try it out :slight_smile:

Cactusface:
Hi Pranav_Srivatsa,
We really need more info from you! Like what motors, batteries!! which could be your problem. If you can show us how you have everything wired up.

Regards

Mel.

I used a basic 12V geared dc motor and general purpose 9v batteries. Paul sir was right , it's pathetic(batteries).

leolfs:
Paul S does have a point though. (And it's a consistent one!) It can be hell to have to wade past a page of inane posts to find anything useful or consider offering any help with. The Forum really does need to be interesting technically not just amusing.(Though a little levity is a always a plus). The moderator should reject the following type of posts:
1 Help Me. With whatever I think I don't know but didn't think about.
2 Any mention of college assignments. That should go into a "I will pay handsomely for someone to cheat for me because my brain hurts too much" topic.
3 Any code with a delay() to be banned from the Forum!

if asking for specific help then post code with first post. But before you hit that post button do imagine you are someone else reading it and criticise severely. You will then definitely have a better experience on the forum. (Paul is sometimes quite forthcoming).
Anyway, getting shot down in flames is a great opportunity to get over your limitations. The worst you'll suffer is gravel rash. Learn from it.

I do agree with your statement. Everyone hates the annoying noob and i am trying not to be one . I didnt mean any disrespect to any one . Sorry if i have.
I didnt ask for the code , my code is not an efficient one and my programming skills arent that good, just want to improve the code.

I would use AA batteries, they last a lot longer than 9Vs. Get a 10 AA battery box wired up instead of 9Vs for the motors, but keep a 9V for the Arduino (seperate batteries is better in my experience) and make SURE you have a common ground. That should improve run time. Use rechargeable AAs. Hope this helps.

While I can see the points people are making on both sides of this (arguement?) thread, I will point out that as a beginner, you WILL make mistakes. I've been there. So just just chill. There is such a thing as too much constructive criticism! :slight_smile:

I like your robot. :slight_smile:

Will keep that in mind :slight_smile: thanks :smiley: