Author Topic: Simple robot avoider program - could you check please?  (Read 1423 times)

0 Members and 1 Guest are viewing this topic.

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
Simple robot avoider program - could you check please?
« on: June 05, 2010, 03:53:22 AM »
Hello,

I'm going to make an arduino based robot that will avoid object in front of it.
The sensor will be a SharpIr GP2D120.
I'll use 2 HT-311 servo modified by myself.

While I'm waiting for the hardware, I start working on the code.
Could you check if there's any big mistake ( I tried keep it simple so I can easily figure what going wrong but would like your thinks).
Later I'll add more thing ( like scanning with a servo+ sharpIr so I can set direction and angle to turn)

Ty

Code: [Select]
// * Robot avoiding objects V1.0 4JUN10 * //


#include <Servo.h>

const int sharpPin=4; // set n of the pin

Servo leftservo;
Servo rightservo;
Servo scanservo;
int distance=0;
int threshold=0;
int leftspeed=0;
int rightspeed=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;


void setup() {
  pinMode(sharpPin, INPUT); //set the pin of sharpIR and servo NB: n of the pin are
  leftservo.attach(9);      // fictive - waiting for the board.
  rightservo.attach(8);
  scanservo.attach(7);
  setThreshold ;
}

void setThreshold() {  //set the threshold value doing a mean of 5 distance value
  a=analogRead(sharpPin);
  delay(40);
  b=analogRead(sharpPin);
  delay(40);
  c=analogRead(sharpPin);
  delay(40);
  d=analogRead(sharpPin);
  delay(40);
  e=analogRead(sharpPin);
  threshold=(a+b+c+d+e)/5;
}

void loop()
{
  distance=analogRead(sharpPin);

  if (distance > threshold)  {          // If no object in the way
   
    leftspeed=180;
    rightspeed=180;
    leftservo.write(leftspeed);      // go forward
    rightservo.write(rightspeed);
  }
  else {
 
    leftspeed=135;
    rightspeed=45;
    leftservo.write(leftspeed);      // else turn right
    rightservo.write(rightspeed);
  }
}

May the Achtuche be with you...

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: Simple robot avoider program - could you check please?
« Reply #1 on: June 05, 2010, 06:16:53 AM »
Looks pretty good as far as I can see, apart from one thing which I think is wrong - I'm not a C genius but,

I think
Code: [Select]
  setThreshold ;
should be
Code: [Select]
  setThreshold();
Apart from that all looks perfect.

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
Re: Simple robot avoider program - could you check please?
« Reply #2 on: June 05, 2010, 09:38:38 AM »
Great. Ty for the () advice, arduino prog didn't say anything about it when it compiled.

Just another question : am I right if I think that telling my modified servo to go 180 will make it turn faster than if I tell 135 ? And that under 90 it'll turn counterclockwise ?

Ty
May the Achtuche be with you...

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: Simple robot avoider program - could you check please?
« Reply #3 on: June 05, 2010, 02:07:00 PM »
That's completely correct.

A value further from the centre point (90) will make it spin faster.

The centre value might not be 90 though, depending on how good the servo is, it could be anything from about 88 to 92.

 -HyperNerd
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline ishkaTopic starter

  • Jr. Member
  • **
  • Posts: 38
  • Helpful? 0
Re: Simple robot avoider program - could you check please?
« Reply #4 on: June 05, 2010, 11:53:08 PM »
OK thanks. Now just have to wait till thursday so I can start working on the hardware... I hope that volcan won't smoke the entire atmosphere once more ;-)
May the Achtuche be with you...

 


Get Your Ad Here