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!