Thursday, July 3, 2014

My First Arduino Robot

This is my first successful robot using an Arduino and a ping sensor. The forward and reverse circuit is the same as the forward and reverse circuit I have posted on this blog and YouTube. The motor is a set of motors i bought from Radio Shack. The big silver transistor is a NPN 2N3055 that controls the stearig motor. The code is at the bottom.








And here is the code.

// by BirdshoBrad @ YouTube & Blogger
const int pingPin = 7;
int pin1 = 10;
int pin2 = 11;
int pin3 = 12;



void setup() {

  Serial.begin(9600);  // initialize serial communication:
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
}

void loop()
{
  long duration;

  // The PING is triggered by a HIGH pulse of 5 microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
Serial.println(duration);
delay(100);

if (duration > 2500) // the distance....about 8-10 inches
{digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH); //forward
}

else {
  digitalWrite(pin3, LOW); // forward
  delay(1500); // rest
  digitalWrite(pin1, HIGH); // turnig transistor
digitalWrite(pin2, HIGH); // reverse
delay(3000);
digitalWrite(pin3, LOW);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
delay(2000); //rest
}
 
}









No comments:

Post a Comment