ArduinoUNO/4WDROVER5/PING/SparkfunMotorDrive/Satrtup

Hi guys,

im totally new in arduino and u got the starters pack from arduino. That i enjoyed fully .
Now that im true the projects i wish to start building my first autonomous project.
As i didn’t know what to order i have some Hardware but don’t know how to start. Anyone wants to suggests the first steps ?

My setup is :

Raspberry PiB
Arduino Uno
PING 3connections
Rover5 4DCmotors
SparkFun 4 channel Motorboard.

Can any one suggest the wiring to the raspberry pi or to the arduino for starting.
And What code should i use . I really can’t find any support from Sparkfun Website.

Regards.

#include <Servo.h>

/* This is the Third version of the obstacle avoidance robot.
Improvements over version 2:
1)Scans while moving and makes small corrections vs. stoping to scan and making large corrections.
Issues:

*/
//Robot speed is 8.875 in/s
//rotation spedd is 120°/s

//define Motors

#define Motor1 3
#define Motor2 5
#define Motor3 6
#define Motor4 9 //These are PWM Choose A Number between 0 and 255

#define ChBack1 2
#define ChBack2 4
#define ChBack3 7
#define ChBack4 8 //Thes are just I/O LOW/HIGH)

//Declare Servos
//Servo leftservo; //Left wheel servo
//Servo rightservo; //Right wheel servo
Servo scanservo; //Ping Sensor Servo
const int pingPin = 12; //Pin that the Ping sensor is attached to.
//const int leftservopin = 9; //Pin number for left servo
//const int rightservopin = 10; // Pin number for right servo
const int scanservopin = 10; // Pin number for scan servo
const int distancelimit = 20; //If something gets this many inched from
// the robot it stops and looks for where fo go.

int scantime = 0;
int lastscantime = 0;
char sensorpos = ‘L’;
long oldtime = 0;
long timesinceturnedleft = 0;
long timesinceturnedright = 0;

//Setup function. Runs once when Arduino is turned on or restarted
void setup()
{
//leftservo.attach(leftservopin); //Attach left servo to its pin.
//rightservo.attach(rightservopin); // Attch the right servo
scanservo.attach(scanservopin); // Attach the scan servo
Serial.begin(9600);
delay(2000); // wait two seconds
}

void loop(){
int leftdistance = 90;
int rightdistance = 90;
go(); // if nothing is wrong the go forward using go() function below.
if(millis()>oldtime+300){
if(sensorpos == ‘L’){
leftdistance = ping();
sensorpos = ‘R’;
}
else{
rightdistance = ping();
sensorpos = ‘L’;
}
oldtime = millis();
}
switch (sensorpos){
case ‘L’:
scanservo.write(70);
break;
case ‘R’:
scanservo.write(110);
break;
}
if(leftdistance<distancelimit){
if (millis()<timesinceturnedleft + 500){
// Drive Backwords
analogWrite(Motor1, 250);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, HIGH);
digitalWrite(ChBack2, HIGH);
digitalWrite(ChBack3, HIGH);
digitalWrite(ChBack4, HIGH);

delay(500);
//Turn Right
analogWrite(Motor1, 250);
analogWrite(Motor2, 0);
analogWrite(Motor3, 0);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);

delay(120);

}
else{
analogWrite(Motor1, 250);
analogWrite(Motor2, 0);
analogWrite(Motor3, 0);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);

delay(25);

}
timesinceturnedright = millis();
}
if(rightdistance<distancelimit){
if(millis()<timesinceturnedright + 500){
// Drive Backwords
analogWrite(Motor1, 250);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, HIGH);
digitalWrite(ChBack2, HIGH);
digitalWrite(ChBack3, HIGH);
digitalWrite(ChBack4, HIGH);
delay(500);

//Turn Left

analogWrite(Motor1, 0);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 0);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);

delay(120);
}
else{
//Turn Left

analogWrite(Motor1, 0);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 0);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);

delay(25);
}
timesinceturnedleft = millis();
}
}

int ping(){
long duration, inches;
//Send Pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//Read Echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
Serial.print("Ping: ");
Serial.println(inches);
return round(inches);
}

void go(){
analogWrite(Motor1, 250);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);
}

void turnleft(float t){
analogWrite(Motor1, 0);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 0);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);
t=(t/120)*1000;

Serial.print("Turning left for this many seconds: ");
Serial.println(t);
delay(t);
}

void turnright(float t){
analogWrite(Motor1, 250);
analogWrite(Motor2, 0);
analogWrite(Motor3, 0);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);
t=(t/120)*1000;

Serial.print("Turning right for this many seconds: ");
Serial.println(t);
delay(t);
}

void goforward(int t){
analogWrite(Motor1, 250);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, LOW);
digitalWrite(ChBack2, LOW);
digitalWrite(ChBack3, LOW);
digitalWrite(ChBack4, LOW);
delay(t);
}

void goback(int t){
analogWrite(Motor1, 250);
analogWrite(Motor2, 250);
analogWrite(Motor3, 250);
analogWrite(Motor4, 250);

digitalWrite(ChBack1, HIGH);
digitalWrite(ChBack2, HIGH);
digitalWrite(ChBack3, HIGH);
digitalWrite(ChBack4, HIGH);
delay(t);
}

void stopmotors(){
analogWrite(Motor1, 0);
analogWrite(Motor2, 0);
analogWrite(Motor3, 0);
analogWrite(Motor4, 0);

digitalWrite(ChBack1, HIGH);
digitalWrite(ChBack2, HIGH);
digitalWrite(ChBack3, HIGH);
digitalWrite(ChBack4, HIGH);
}

char scan(){
int lval, lcval, cval, rcval, rval;
int maximum = 0;
int choice;

scanservo.write(0); //Look left
delay(300);
lval = ping();
if(lval>maximum){
maximum = lval;
choice = 1;
}

scanservo.write(0); //Look left center
delay(300);
lcval = ping();
if(lcval>maximum){
maximum = lcval;
choice = 2;
}

scanservo.write(0); //Look center
delay(300);
cval = ping();
if(cval>maximum){
maximum = cval;
choice = 3;
}

scanservo.write(0); //Look right center
delay(300);
rcval = ping();
if(rcval>maximum){
maximum = rcval;
choice = 4;
}

scanservo.write(0); //Look right
delay(300);
rval = ping();
if(rval>maximum){
maximum = rval;
choice = 5;
}

if(maximum<=distancelimit){choice = 6;}

scanservo.write(0); //center scan servo
delay(300);

Serial.print("Choice: ");
Serial.println(choice);
return choice;
}

long microsecondsToInches(long microseconds){
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds){
return microseconds / 29 / 2;
}

Can anybody look at my above code ? Its finally almost , but my servo is doing weird and my left wheel dons turn HEL HELP HEL¨:°)