Skip to content

Instantly share code, notes, and snippets.

@whyisjake
Created April 10, 2012 20:35
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save whyisjake/2354277 to your computer and use it in GitHub Desktop.
Save whyisjake/2354277 to your computer and use it in GitHub Desktop.
Arduino Robot Code
/*
Original code by Nick Brenn
Modified by Marc de Vinck
Make Projects Arduino-based 4WD robot
http://makeprojects.com/Project/Build-your-own-Arduino-Controlled-Robot-/577/1
*/
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
const int pingPin = 19;
// ------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
motor1.setSpeed(200); // Sets the speed for all the motors 1 - 4 (255 is the
maximum speed)
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
}
// ------------------------------------------------------------------------------
float ping () // This is the code that runs the PING ))) Sensor
{
long duration, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
long microsecondsToInches(long microseconds);
inches = microsecondsToInches(duration);
return inches;
} // ------------------------------------------------------------------------------
void forward() // This function moves all the wheels forward
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
// ------------------------------------------------------------------------------
void backward() // This function moves all the wheels backwards
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
// ------------------------------------------------------------------------------
void haltMotors() // This function stops all the motors (It is better to stop
the motors before changing direction)
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
// ------------------------------------------------------------------------------
void turnRight() // This function turns the robot right
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
// ------------------------------------------------------------------------------
void loop() { // This is the main program that will run over and over
long duration, inches; // Declare the variables "duration" and "inches"duration = pulseIn(pingPin, HIGH);
inches = ping(); // Set the inches variable to the float returned by the
ping() function.
Serial.print(inches);
Serial.print("in, ");
Serial.println();
while (inches >8){ // While the robot is 8 inches away from an object.
inches = ping();
forward(); // Move the robot forward.
}
haltMotors(); // Stop the motors for 2 seconds, before changing
direction.
delay(1000);
while (inches < 10){ // Until the robot is 10 inches away from the object, go
backward.
inches = ping();
backward(); // Move the robot backward.
}
turnRight(); // Once the robot is done moving backward, turn the robot
right.
delay(1500); // Note! Change this value (greater or smaller) to adjust
how much the robot turns right
haltMotors();
delay(1000);
}
long microsecondsToInches(long microseconds){
return microseconds / 74 / 2;
}
@rene9900
Copy link

rene9900 commented Feb 8, 2018

for the lazy ones @2018

/*
Original code by Nick Brenn
Modified by Marc de Vinck
Make Projects Arduino-based 4WD robot
http://makeprojects.com/Project/Build-your-own-Arduino-Controlled-Robot-/577/1
*/
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
const int pingPin = 19;
// ------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
motor1.setSpeed(200); // Sets the speed for all the motors 1 - 4 (255 is the maximum speed)
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
}
// ------------------------------------------------------------------------------
float ping() // This is the code that runs the PING ))) Sensor
{
long duration, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
long microsecondsToInches(long microseconds);
inches = microsecondsToInches(duration);
return inches;
} // ------------------------------------------------------------------------------
void forward() // This function moves all the wheels forward
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
// ------------------------------------------------------------------------------
void backward() // This function moves all the wheels backwards
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
// ------------------------------------------------------------------------------
void haltMotors() // This function stops all the motors (It is better to stop the motors before changing direction)
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
// ------------------------------------------------------------------------------
void turnRight() // This function turns the robot right
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
// ------------------------------------------------------------------------------
void loop() { // This is the main program that will run over and over
long duration, inches; // Declare the variables "duration" and "inches"duration = pulseIn(pingPin, HIGH);
inches = ping(); // Set the inches variable to the float returned by the ping() function.
Serial.print(inches);
Serial.print("in, ");
Serial.println();

while (inches >8){ // While the robot is 8 inches away from an object.
inches = ping();
forward(); // Move the robot forward.
}
haltMotors(); // Stop the motors for 2 seconds, before changing direction.
delay(1000);
while (inches < 10){ // Until the robot is 10 inches away from the object, go backward.
inches = ping();
backward(); // Move the robot backward.
}
turnRight(); // Once the robot is done moving backward, turn the robot right.
delay(1500); // Note! Change this value (greater or smaller) to adjust how much the robot turns right
haltMotors();
delay(1000);
}
long microsecondsToInches(long microseconds){
return microseconds / 74 / 2;
}

@deu1
Copy link

deu1 commented Apr 21, 2018

Hi Nick,
Just a quick question. The Ping sensor, HC-SR04, has 4 pins: Vcc, Trig, Echo, Gnd. In your sketch "const int pingPin = 19;" You use pin 19 to connect the sensor to the Arduino. Because it shows pin 19 were you using an Arduino Mega 2560 to run the sketch? And, I don't understand how to set up the HC-SR04 with only one pin. Are both Trig and Echo connected to pin 19 on the Mega 2560? If I am using an Arduino Uno, which pin would I use? Any help would be most appreciated. Thanks.
Dan Urbauer
deu1@shaw.ca
P.S. Just occurred to me, maybe your not using the HC-SR04, but some other sensor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment