Introduction: My Robot

YOU NEED :

 - 1x ARDUINO UNO
 - 1x TAMIYA UNIVERSAL PLATE SET
 - 1x TAMIYA TRACK & WHEEL SET
 - 1x TAMIYA MOTOR GEARBOX
 - 1x L298 Dual H-Bridge Motor Driver
 - 1x 9g Servo
 - 1x Ir Sharp Sensor
 - Plexiglass
 - 1x SPDT Switch
 - Some Wires
 - Hot Glue Gun
 - 3x 9 v Battery
 - 1x Test PCB
 -1x L0875
 -1x 1uf capacitor
 -1x 10uf capacitor



Arduino Code:

 #include

Servo MS;
int a = 4;
int b = 5;
int c = 6;
int d = 7;
int x;
int pos = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(a,OUTPUT);
  pinMode(b,OUTPUT);
  pinMode(c,OUTPUT);
  pinMode(d,OUTPUT);
  MS.attach(3);
}

void loop()
{
  int S;
  int F;
  int D;
 
  int sensorValue = analogRead(A2);
  float vol = sensorValue * (5.0 / 1023.0);
  Serial.println(vol);
 
 
 
    
    
 
 
 
  if(vol >= 0.7)
     {
        MS.write(0);
        pos = 0;
        delay(500);
        Serial.println(pos);
       
        MS.write(90);
        pos = 90;
        delay(500);
        Serial.println(pos);
       
        MS.write(180);
        pos = 180;
        delay(500);
        Serial.println(pos);
       
        MS.write(90);
        pos = 90;
        delay(500);
        Serial.println(pos);
       
        if(pos == 0 && vol >= 0.7)
        {
          S = 1;
        }
       
        if(pos == 0 && vol <= 0.7)
        {
          S = 0;
        }
       
        if(pos == 90 && vol >= 0.7)
        {
          F = 1;
        }
       
        if(pos == 90 && vol <= 0.7)
        {
          F = 0;
        }
       
        if(pos == 180 && vol >= 0.7)
        {
          D = 1;
        }
       
        if(pos == 180 && vol <= 0.7)
        {
          D = 0;
        }
       
        if(S == 0 && F == 1 && D == 1)
        {
           digitalWrite(a, HIGH);
           digitalWrite(b, LOW);
           digitalWrite(c, LOW);
           digitalWrite(d, HIGH);
        }
       
        if(S == 1 && F == 0 && D == 1)
        {
           digitalWrite(a, LOW);
           digitalWrite(b, LOW);
           digitalWrite(c, LOW);
           digitalWrite(d, LOW);
        }
       
        if(S == 1 && F == 1 && D == 0)
        {
           digitalWrite(a, LOW);
           digitalWrite(b, HIGH);
           digitalWrite(c, HIGH);
           digitalWrite(d, LOW);
        }
       
        if(S == 1 && F == 1 && D == 1)
        {
           digitalWrite(a, HIGH);
           digitalWrite(b, LOW);
           digitalWrite(c, HIGH);
           digitalWrite(d, LOW);
        }
       
         Serial.println(S);
         Serial.println(F);
         Serial.println(D);
     }
    
     if(vol <= 0.7 )
     {
       MS.write(90);
     
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
     
   }
 

 
 
}



Pin conecttion :

L298N :

 -pin 1, 8, 15 to gnd arduino and to 9v battery anode;
 -pin 2, 3 to motor 1;
 -pin 13, 14 to motor 2;
 -pin 4, 6, 9, 11 to battery cathode
 -pin 5, 7, 10, 12 to arduino digital pin (digital pin 4, 5, 6, 7 );

Servo :
  
-gnd to arduino gnd;
-red wire to 5v arduino;
-white wire (signal) to digital pin 3 arduino;

IR Sharp Sensor :

-gnd to arduino gnd;
-red wire to 5v arduino;
-white wire (signal) to analog pin A2 arduino








Comments

author
erickeene (author)2015-02-24

From a wiring point of view - it looks good. You got everything connected correctly. Without building it - I can't be 100% certain but it does look like you covered your bases.

From the code point of view - I am having trouble following you. (I did see that the #include <Servo.h> was not right at the top - minor oversight when you moved the code to the web site). I am following the logic and see the following where you get the initial vol from the IR Sensor:

int sensorValue = analogRead(A2);

float vol = sensorValue * (5.0 / 1023.0);

Serial.println(vol);

Makes since.

if vol >.7, you rotate the servo. (0, 90, 180, and back to 90). You don't resample the voltage so vol will still be the value from the last reading. Next, as you go through all the different positions of the servo, you set the pos = that degree. at the end of the servo, you set it back to 90. So the big section of if statements only match on the POS ==90, and vol >.7. So only F ever gets set..

Try this code (I did not test it - didn't have all the hardware to build for testing but it should work better);

#include <Servo.h>

Servo MS;

int a = 4;

int b = 5;

int c = 6;

int d = 7;

int pos = 0;

//declare these 3 variable as global so they don't have to be passed in and out of the subroutine

int S = 0; //should always set a value when you declare a variable

int F = 0;

int D = 0;

void setup()

{

Serial.begin(9600);

pinMode(a,OUTPUT);

pinMode(b,OUTPUT);

pinMode(c,OUTPUT);

pinMode(d,OUTPUT);

MS.attach(3);

}

void loop()

{

int sensorValue = analogRead(A2);

float vol = sensorValue * (5.0 / 1023.0);

Serial.println(vol);

if(vol >= 0.7)

{

MS.write(0);

pos = 0;

delay(500);

which_way(pos);

Serial.println(pos);

MS.write(90);

pos = 90;

delay(500);

which_way(pos);

Serial.println(pos);

MS.write(180);

pos = 180;

delay(500);

which_way(pos);

Serial.println(pos);

MS.write(90);

pos = 90;

delay(500);

Serial.println(pos);

}

}

void which_way(int pos)

{

//pos is declared inside the subrouting and is independ of the other pos

//remeasure distance at the new servo position

int sensorValue = analogRead(A2);

float vol = sensorValue * (5.0 / 1023.0);

Serial.println(vol);

if(pos == 0 && vol >= 0.7)

{

S = 1;

}

if(pos == 0 && vol <= 0.7)

{

S = 0;

}

if(pos == 90 && vol >= 0.7)

{

F = 1;

}

if(pos == 90 && vol <= 0.7)

{

F = 0;

}

if(pos == 180 && vol >= 0.7)

{

D = 1;

}

if(pos == 180 && vol <= 0.7)

{

D = 0;

}

if(S == 0 && F == 1 && D == 1)

{

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

}

if(S == 1 && F == 0 && D == 1)

{

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

}

if(S == 1 && F == 1 && D == 0)

{

digitalWrite(a, LOW);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

}

if(S == 1 && F == 1 && D == 1)

{

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

}

Serial.println(S);

Serial.println(F);

Serial.println(D);

}