Society of Robots - Robot Forum

Software => Software => Topic started by: arispal on September 05, 2010, 01:10:11 PM

Title: Newbie: Something wrong with my poor programming..... (DFRobotshop Rover)
Post by: arispal on September 05, 2010, 01:10:11 PM
Hello experts!! I'm new to this hobby and have little experience in C programming. I bought a DFRobotshop Rover w/c is Arduino compatible basic kit (with tracks) and purchase the Sharp IR GP2D12 with servo. I am impressed with the obstacle avoidance robots. I already test the programming of the forward/backward/left/right of the motors but I could not operate it with the IR and servo. My goal is to detect obstacles in the three angles which is left/center/right. I tried the following code:

int irReader = 1;    // the analog input pin for the ir reader

int motoPin1 = 8;
int motoPin2 = 7;
int ena1 = 6;
int ena2 = 5;

void setup()

{
Serial.begin(9600);
pinMode(motoPin1, OUTPUT);
pinMode(motoPin2, OUTPUT);
pinMode(ena1, OUTPUT);
pinMode(ena2, OUTPUT);
int analogRead(irReader);
}

void loop()
{

if (analogRead(irReader) > 60)

{
  motor_stop();
  delay(500);
  backward();
  motor_stop();
  delay(500);
  right_turn();
  motor_stop();
  delay(500);
 
}

else if (analogRead(irReader) < 60)

{
  forward();
}
}

void backward()
{
 
  digitalWrite(motoPin1, HIGH);
  digitalWrite(motoPin2, HIGH);
  digitalWrite(ena1, HIGH);
  digitalWrite(ena2, HIGH);

}

void forward()
{
  digitalWrite(motoPin1, LOW);
  digitalWrite(motoPin2, LOW);
  digitalWrite(ena1, HIGH);
  digitalWrite(ena2, HIGH);

}

void right_turn()
{
  digitalWrite(motoPin1, HIGH);
  digitalWrite(motoPin2, LOW);
  digitalWrite(ena1, HIGH);
  digitalWrite(ena2, HIGH);

void left_turn()
{
  digitalWrite(motoPin1, LOW);
  digitalWrite(motoPin2, HIGH);
  digitalWrite(ena1, HIGH);
  digitalWrite(ena2, HIGH);
}


void motor_stop()
{
  digitalWrite(ena1, LOW);
  digitalWrite(ena2, LOW);
}


I hope somebody can help with this.


Thanks!!!
Title: Re: Newbie: Something wrong with my poor programming..... (DFRobotshop Rover)
Post by: Ro-Bot-X on September 05, 2010, 02:27:41 PM
I see 3 problems. First one is just waste of memory by using "int" variable for pin assignment instead of "#define". So this:
Code: [Select]
int motoPin1 = 8;
int motoPin2 = 7;
int ena1 = 6;
int ena2 = 5;

should be this:
Code: [Select]
#define motoPin1 8
#define motoPin2 7
#define ena1 6
#define ena2 5

The second problem is missing writing he pins state after defining them outputs.

The third problem is missing delays between backward() and motor_stop(), then between right_turn() and motor_stop().
Title: Re: Newbie: Something wrong with my poor programming..... (DFRobotshop Rover)
Post by: arispal on September 05, 2010, 03:21:19 PM
Thank you for your reply...I think it's the same with the link below post...but I don't know how to apply it..he is using the MegaServo.h...is the code can be useful for my robot?

http://www.societyofrobots.com/robotforum/index.php?topic=8640.0 (http://www.societyofrobots.com/robotforum/index.php?topic=8640.0)

what do you mean by missing pins state?