First proper test – all worked nicely

So, a couple of days ago my jumper leads arrived from Mindkits. I managed to quickly hook up both motor drivers (4 motors in total) and do a basic test.

IMG_0465

Then yesterday I managed to write some basic code. I wanted to keep things really simple so I have a function called Go() and another called Stop(). I’ll leave it as an exercise for the reader to deem what they do.

Finally, I have a simple function to read from the only distance sensor I have attached so far – the one on the front.

I had to do quite a bit of tweaking of how fast to set the motors for it to work on carpet. In the end I just went with full speed as anything less can make it fail to start moving.

Here is the video of its first test.

Here is my current code. Pretty simple stuff!

//
// Right tracks of Rover 5.
// E2/M2 are the front motor
// E1/M1 are the rear motor
//

int E1 = 2; //PWM
int M1 = 14;
int E2 = 3; //PWM
int M2 = 15;   

//
// Left tracks of Rover 5.
// E4/M4 are the front motor
// E3/M3 are the rear motor
//

int E3 = 4; //PWM
int M3 = 16;
int E4 = 5; //PWM
int M4 = 17;   

// Front Distance Sensor
int Dist1 = 6;

void setup() 
{ 
   pinMode(M1,OUTPUT);   
   pinMode(M2,OUTPUT);
   pinMode(M3,OUTPUT);   
   pinMode(M4,OUTPUT);   

   pinMode(Dist1,OUTPUT);
   Serial.begin(9600);
} 

void loop() 
{ 
  Serial.println(getDist());
  int dist=getDist();
  if(dist>30) 
  {
    Go(255);
  }
  else
  {
    Stop();
  }
  delay(250);
}

int getDist()
{
  int i=analogRead(Dist1);
  int val=(6762/(i-9))-4;
  return val;
}

void Go(int speed)
{
  digitalWrite(M1,HIGH);
  digitalWrite(M2,HIGH);
  digitalWrite(M3,LOW);
  digitalWrite(M4,LOW);  
  analogWrite(E1,speed);   //PWM Speed Control
  analogWrite(E2,speed);   //PWM Speed Control
  analogWrite(E3,speed);   //PWM Speed Control
  analogWrite(E4,speed);   //PWM Speed Control    
}

void Stop()
{
  analogWrite(E1,0);   //PWM Speed Control
  analogWrite(E2,0);   //PWM Speed Control
  analogWrite(E3,0);   //PWM Speed Control
  analogWrite(E4,0);   //PWM Speed Control   
}
Share on Facebook

Leave a Comment