Let's Make Robots!

Arduino code help for Sharp IR, servo and motor controller

Hi, i am new here and new to robotics! I am building my first robot, its a little crude and scruffy, but is intended as a learning base that i can tinker with so that i can venture into the world of robotics, electronics and programming.

Spec:

Self designed, lasercut chassis (mk3)

Tamiya tracks and wheels

Tamiya double motor gearbox (low gear)

Arduino Duemilanove

Sharp GP2D12 IR sensor

Micro servo (generic ebay one)

Prototype shield v5 with breadboard

L298N Motor control module

4xAA 1.2v rechargeables (to power Arduino)

1x9v battery (to power motors, being replaced with something better later on)

I am struggling with the code side of things. I am using the arduino software and would like for the servo to move side to side whilst the sharp sensor detects obstacles. When an obstacle is detected, the robot will turn 90 degrees and scoot off until it finds another object to avoid and so on.

I am a little bit confused as to what i should be telling it to do, as i am quite new to writing code! So far i have managed to get a reading out of the Sharp IR, get the servo to move side to side, and get the motors to drive in each direction. I can't seem to work out how to get these all to work togther, only as individual codes! I have admitadely found other peoples codes online, and taken bits from them and altered them to suit my robot, so this may be a key reason as to where i am going wrong...

 

Servo:

int servoPin = 0; //servo connected to digital pin 0
int myAngle; //angle of the servo roughly 0-180
int pulseWidth; //servoPulse function variable

void setup ()
{
  pinMode(servoPin, OUTPUT); //sets pin 0 as output
}

void servoPulse(int servoPin, int myAngle)
{
  pulseWidth =(myAngle * 10) +500; //determines delay
  digitalWrite (servoPin, HIGH); //set servo high
  delayMicroseconds (pulseWidth); //microsecond pause
  digitalWrite(servoPin, LOW); //set servo low
}

void loop()
{
  // servo starts at 10 deg and rotates to 170 deg
  for (myAngle=10; myAngle<=170; myAngle++)
  {
    servoPulse(servoPin, myAngle); //send pin and angle
    delay (20); //refresh cycle
  }
  //servo starts at 170 deg and rotates to 10 deg
  for (myAngle=170; myAngle>=10; myAngle--)
  {
    servoPulse (servoPin, myAngle); //send pin and angle
    delay(20); //refresh cycle
  }
}


Sharp IR (worth noting that this does not seem to be giving me a proper reading on the serial monitor):

int sensorPin = 0; //analog pin 0

void setup(){
  Serial.begin(9600);
}

void loop(){
  int val = analogRead(sensorPin);
  Serial.println(val);

  //just to slow down the output - remove if trying to catch an object passing by
  delay(100);

}

 

Motor drive (just a series of movements for the robot to perform until it can use the IR)

// motor A
int dir1PinA = 13;
int dir2PinA = 12;
int speedPinA = 10;
// motor B
// motor A
int dir1PinB = 11;
int dir2PinB = 8;
int speedPinB = 9;
unsigned long time;
int speed;
int dir;
void setup() {
pinMode(dir1PinA, OUTPUT);
pinMode(dir2PinA, OUTPUT);
pinMode(speedPinA, OUTPUT);
pinMode(dir1PinB, OUTPUT);
pinMode(dir2PinB, OUTPUT);
pinMode(speedPinB, OUTPUT);
time = millis();
speed = 0;
dir = 1;
}
void loop() {
analogWrite(speedPinA, speed);
analogWrite(speedPinB, 255 - speed);
// set direction
if (1 == dir) {
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
digitalWrite(dir1PinB, HIGH);
digitalWrite(dir2PinB, LOW);
} else {
digitalWrite(dir1PinA, HIGH);
digitalWrite(dir2PinA, LOW);
digitalWrite(dir1PinB, LOW);
digitalWrite(dir2PinB, HIGH);
}
if (millis() - time > 5000) {
time = millis();
speed += 20;
if (speed > 255) {
speed = 0;

}
if (1 == dir) {
dir = 0;
} else {
dir =1;
}
}
}

 

Thanks in advance for any help!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Not to worry, i don't think anybody would have been interested in the photos anyway as the projects not complete yet.

As for the 9v battery, i mentioned in my first post that its being replaced with something else. The new battery pack will replace the 9v battery and the 4 AA batteries altogether as i am aware that the things i had kicking about were not entirely up to the job. The 9v Asda brand poverty battery has been very capable and the robot has plenty enough power to get accross a thick pile carpet for 25minutes so far, as well as powering the arduino and servo for ages with no signs of draining yet either.

a single 9v has very little current, and won't do anything for your motors. If you don't have any other external power source, you could either:

A) power your arduino and motors off the 4AA's (though non-rechargable would give you more voltage)

B) power the arduino with the 9v and everything else from the 4AA's.

...But the photos were not of any robots. They were huge 3000-pixel-wide bandwidth-limit notifications from photobucket. It looked like some kinda weird spam until I scrolled down.

Cheers for the link! That's exactly what i have been looking for it seems. The code, as mentioned, is just stuff i have found and tried to modify to suit my needs but i know is not ideal.

I have deleted the photos now, i just figured people might have some interest in what i have built.

Did you mean to include those giant images? ugg.

To be really honest, I can't make heads or tails of your code there. I happen to have some super simple autonomous code that you may want to try. https://sites.google.com/a/rocketbrandstudios.com/rocket-brand-studios/tutorials-arduino

There are downloadable files at the bottom, including one called "Wicked easy autonomous". You should be able to modify the pins etc to work with your set-up.