Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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;
}
@mekaci89

This comment has been minimized.

Show comment Hide comment
@mekaci89

mekaci89 Apr 12, 2014

my arduino gives error at this line :

float ping () // This is the code that runs the PING ))) Sensor

Would you check please ? how to fix it ? thanks.

my arduino gives error at this line :

float ping () // This is the code that runs the PING ))) Sensor

Would you check please ? how to fix it ? thanks.

@jandrillox

This comment has been minimized.

Show comment Hide comment
@jandrillox

jandrillox May 22, 2014

i have same problem too :(

i have same problem too :(

@user405

This comment has been minimized.

Show comment Hide comment
@user405

user405 May 26, 2014

didn't try the code yet but seems to me that there shouldn't be any space between ping and (). So change
float ping ()
to
float ping()

user405 commented May 26, 2014

didn't try the code yet but seems to me that there shouldn't be any space between ping and (). So change
float ping ()
to
float ping()

@leem888

This comment has been minimized.

Show comment Hide comment
@leem888

leem888 Jun 25, 2014

help! I use the code and get a whole bunch of errors...I'm a bit of a beginner, so not sure how to fix this! I've built it all as per the instructions so not sure why it's not working. Here are the errors:

sketch_jun25e:24: error: stray '' in program
sketch_jun25e:10: error: 'ufb02oat' does not name a type
sketch_jun25e:8: error: 'AF_DCMotor' does not name a type
sketch_jun25e:9: error: 'AF_DCMotor' does not name a type
sketch_jun25e:10: error: 'AF_DCMotor' does not name a type
sketch_jun25e:11: error: 'AF_DCMotor' does not name a type
sketch_jun25e.ino: In function 'void setup()':
sketch_jun25e:17: error: 'motor1' was not declared in this scope
sketch_jun25e:18: error: 'maximum' was not declared in this scope
sketch_jun25e:18: error: expected ;' before 'speed' sketch_jun25e:20: error: 'motor3' was not declared in this scope sketch_jun25e:21: error: 'motor4' was not declared in this scope sketch_jun25e.ino: At global scope: sketch_jun25e:24: error: 'ufb02oat' does not name a type sketch_jun25e.ino: In function 'void forward()': sketch_jun25e:41: error: 'motor1' was not declared in this scope sketch_jun25e:41: error: 'FORWARD' was not declared in this scope sketch_jun25e:42: error: 'motor2' was not declared in this scope sketch_jun25e:43: error: 'motor3' was not declared in this scope sketch_jun25e:44: error: 'motor4' was not declared in this scope sketch_jun25e.ino: In function 'void backward()': sketch_jun25e:49: error: 'motor1' was not declared in this scope sketch_jun25e:49: error: 'BACKWARD' was not declared in this scope sketch_jun25e:50: error: 'motor2' was not declared in this scope sketch_jun25e:51: error: 'motor3' was not declared in this scope sketch_jun25e:52: error: 'motor4' was not declared in this scope sketch_jun25e.ino: At global scope: sketch_jun25e:56: error: expected initializer before 'the' sketch_jun25e.ino: In function 'void turnRight()': sketch_jun25e:66: error: 'motor1' was not declared in this scope sketch_jun25e:66: error: 'FORWARD' was not declared in this scope sketch_jun25e:67: error: 'motor2' was not declared in this scope sketch_jun25e:67: error: 'BACKWARD' was not declared in this scope sketch_jun25e:68: error: 'motor3' was not declared in this scope sketch_jun25e:69: error: 'motor4' was not declared in this scope sketch_jun25e.ino: In function 'void loop()': sketch_jun25e:74: error: 'ping' was not declared in this scope sketch_jun25e:75: error: expected;' before 'function'
sketch_jun25e:84: error: 'haltMotors' was not declared in this scope
sketch_jun25e:85: error: 'direction' was not declared in this scope
sketch_jun25e:89: error: request for member 'inches' in 'backward', which is of non-class type 'void ()()'
sketch_jun25e:93: error: 'right' was not declared in this scope
sketch_jun25e:95: error: 'how' was not declared in this scope
sketch_jun25e:95: error: expected `;' before 'much'

leem888 commented Jun 25, 2014

help! I use the code and get a whole bunch of errors...I'm a bit of a beginner, so not sure how to fix this! I've built it all as per the instructions so not sure why it's not working. Here are the errors:

sketch_jun25e:24: error: stray '' in program
sketch_jun25e:10: error: 'ufb02oat' does not name a type
sketch_jun25e:8: error: 'AF_DCMotor' does not name a type
sketch_jun25e:9: error: 'AF_DCMotor' does not name a type
sketch_jun25e:10: error: 'AF_DCMotor' does not name a type
sketch_jun25e:11: error: 'AF_DCMotor' does not name a type
sketch_jun25e.ino: In function 'void setup()':
sketch_jun25e:17: error: 'motor1' was not declared in this scope
sketch_jun25e:18: error: 'maximum' was not declared in this scope
sketch_jun25e:18: error: expected ;' before 'speed' sketch_jun25e:20: error: 'motor3' was not declared in this scope sketch_jun25e:21: error: 'motor4' was not declared in this scope sketch_jun25e.ino: At global scope: sketch_jun25e:24: error: 'ufb02oat' does not name a type sketch_jun25e.ino: In function 'void forward()': sketch_jun25e:41: error: 'motor1' was not declared in this scope sketch_jun25e:41: error: 'FORWARD' was not declared in this scope sketch_jun25e:42: error: 'motor2' was not declared in this scope sketch_jun25e:43: error: 'motor3' was not declared in this scope sketch_jun25e:44: error: 'motor4' was not declared in this scope sketch_jun25e.ino: In function 'void backward()': sketch_jun25e:49: error: 'motor1' was not declared in this scope sketch_jun25e:49: error: 'BACKWARD' was not declared in this scope sketch_jun25e:50: error: 'motor2' was not declared in this scope sketch_jun25e:51: error: 'motor3' was not declared in this scope sketch_jun25e:52: error: 'motor4' was not declared in this scope sketch_jun25e.ino: At global scope: sketch_jun25e:56: error: expected initializer before 'the' sketch_jun25e.ino: In function 'void turnRight()': sketch_jun25e:66: error: 'motor1' was not declared in this scope sketch_jun25e:66: error: 'FORWARD' was not declared in this scope sketch_jun25e:67: error: 'motor2' was not declared in this scope sketch_jun25e:67: error: 'BACKWARD' was not declared in this scope sketch_jun25e:68: error: 'motor3' was not declared in this scope sketch_jun25e:69: error: 'motor4' was not declared in this scope sketch_jun25e.ino: In function 'void loop()': sketch_jun25e:74: error: 'ping' was not declared in this scope sketch_jun25e:75: error: expected;' before 'function'
sketch_jun25e:84: error: 'haltMotors' was not declared in this scope
sketch_jun25e:85: error: 'direction' was not declared in this scope
sketch_jun25e:89: error: request for member 'inches' in 'backward', which is of non-class type 'void ()()'
sketch_jun25e:93: error: 'right' was not declared in this scope
sketch_jun25e:95: error: 'how' was not declared in this scope
sketch_jun25e:95: error: expected `;' before 'much'

@lotsacaffeine

This comment has been minimized.

Show comment Hide comment
@lotsacaffeine

lotsacaffeine Nov 7, 2014

Line 75 is the continuation of the comment from the end of line 74.
There are several other instances where that happens...just make sure to pretty up your code.

Line 75 is the continuation of the comment from the end of line 74.
There are several other instances where that happens...just make sure to pretty up your code.

@naiduc

This comment has been minimized.

Show comment Hide comment
@naiduc

naiduc Nov 15, 2014

when iam trying to compile the code iam getting the following error. can anyone help me???

Arduino: 1.5.6-r2 (Windows 7), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch_nov15f:9: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:10: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:11: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:12: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:13: error: expected primary-expression before ';' token
sketch_nov15f.ino: In function 'void setup()':
sketch_nov15f:18: error: 'motor1' was not declared in this scope
sketch_nov15f:19: error: 'maximum' was not declared in this scope
sketch_nov15f:19: error: expected ;' before 'speed' sketch_nov15f:21: error: 'motor3' was not declared in this scope sketch_nov15f:22: error: 'motor4' was not declared in this scope sketch_nov15f.ino: At global scope: sketch_nov15f:25: error: expected unqualified-id before '{' token sketch_nov15f.ino: In function 'void forward()': sketch_nov15f:41: error: 'motor1' was not declared in this scope sketch_nov15f:41: error: 'FORWARD' was not declared in this scope sketch_nov15f:42: error: 'motor2' was not declared in this scope sketch_nov15f:43: error: 'motor3' was not declared in this scope sketch_nov15f:44: error: 'motor4' was not declared in this scope sketch_nov15f.ino: In function 'void backward()': sketch_nov15f:49: error: 'motor1' was not declared in this scope sketch_nov15f:49: error: 'BACKWARD' was not declared in this scope sketch_nov15f:50: error: 'motor2' was not declared in this scope sketch_nov15f:51: error: 'motor3' was not declared in this scope sketch_nov15f:52: error: 'motor4' was not declared in this scope sketch_nov15f.ino: At global scope: sketch_nov15f:56: error: expected initializer before 'the' sketch_nov15f.ino: In function 'void turnRight()': sketch_nov15f:66: error: 'motor1' was not declared in this scope sketch_nov15f:66: error: 'FORWARD' was not declared in this scope sketch_nov15f:67: error: 'motor2' was not declared in this scope sketch_nov15f:67: error: 'BACKWARD' was not declared in this scope sketch_nov15f:68: error: 'motor3' was not declared in this scope sketch_nov15f:69: error: 'motor4' was not declared in this scope sketch_nov15f.ino: In function 'void loop()': sketch_nov15f:74: error: 'ping' was not declared in this scope sketch_nov15f:75: error: expected;' before 'function'
sketch_nov15f:84: error: 'haltMotors' was not declared in this scope
sketch_nov15f:85: error: 'direction' was not declared in this scope
sketch_nov15f:89: error: request for member 'inches' in 'backward', which is of non-class type 'void ()()'
sketch_nov15f:93: error: 'right' was not declared in this scope
sketch_nov15f:95: error: 'how' was not declared in this scope
sketch_nov15f:95: error: expected `;' before 'much'

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

naiduc commented Nov 15, 2014

when iam trying to compile the code iam getting the following error. can anyone help me???

Arduino: 1.5.6-r2 (Windows 7), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch_nov15f:9: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:10: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:11: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:12: error: expected constructor, destructor, or type conversion before '(' token
sketch_nov15f:13: error: expected primary-expression before ';' token
sketch_nov15f.ino: In function 'void setup()':
sketch_nov15f:18: error: 'motor1' was not declared in this scope
sketch_nov15f:19: error: 'maximum' was not declared in this scope
sketch_nov15f:19: error: expected ;' before 'speed' sketch_nov15f:21: error: 'motor3' was not declared in this scope sketch_nov15f:22: error: 'motor4' was not declared in this scope sketch_nov15f.ino: At global scope: sketch_nov15f:25: error: expected unqualified-id before '{' token sketch_nov15f.ino: In function 'void forward()': sketch_nov15f:41: error: 'motor1' was not declared in this scope sketch_nov15f:41: error: 'FORWARD' was not declared in this scope sketch_nov15f:42: error: 'motor2' was not declared in this scope sketch_nov15f:43: error: 'motor3' was not declared in this scope sketch_nov15f:44: error: 'motor4' was not declared in this scope sketch_nov15f.ino: In function 'void backward()': sketch_nov15f:49: error: 'motor1' was not declared in this scope sketch_nov15f:49: error: 'BACKWARD' was not declared in this scope sketch_nov15f:50: error: 'motor2' was not declared in this scope sketch_nov15f:51: error: 'motor3' was not declared in this scope sketch_nov15f:52: error: 'motor4' was not declared in this scope sketch_nov15f.ino: At global scope: sketch_nov15f:56: error: expected initializer before 'the' sketch_nov15f.ino: In function 'void turnRight()': sketch_nov15f:66: error: 'motor1' was not declared in this scope sketch_nov15f:66: error: 'FORWARD' was not declared in this scope sketch_nov15f:67: error: 'motor2' was not declared in this scope sketch_nov15f:67: error: 'BACKWARD' was not declared in this scope sketch_nov15f:68: error: 'motor3' was not declared in this scope sketch_nov15f:69: error: 'motor4' was not declared in this scope sketch_nov15f.ino: In function 'void loop()': sketch_nov15f:74: error: 'ping' was not declared in this scope sketch_nov15f:75: error: expected;' before 'function'
sketch_nov15f:84: error: 'haltMotors' was not declared in this scope
sketch_nov15f:85: error: 'direction' was not declared in this scope
sketch_nov15f:89: error: request for member 'inches' in 'backward', which is of non-class type 'void ()()'
sketch_nov15f:93: error: 'right' was not declared in this scope
sketch_nov15f:95: error: 'how' was not declared in this scope
sketch_nov15f:95: error: expected `;' before 'much'

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

@Pastyguy

This comment has been minimized.

Show comment Hide comment
@Pastyguy

Pastyguy Dec 7, 2014

Fix Almost
So I just had a look at the code and for the most part the "// text blah blah" is not formatted correctly
Ex
void(){ // the text that the guy wrought was totaly
bad and doest work blah blah.
text.moter=34.
}

so i look for the Words that are highlighted in the error log
EX
sketch_nov15f:95: error: expected `;' before 'much'

Heres the code i fixed But cant get the last two errors to go away

include <AFMotor.h>

include <Stepper.h>

/*
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);
}

{
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;
}

Pastyguy commented Dec 7, 2014

Fix Almost
So I just had a look at the code and for the most part the "// text blah blah" is not formatted correctly
Ex
void(){ // the text that the guy wrought was totaly
bad and doest work blah blah.
text.moter=34.
}

so i look for the Words that are highlighted in the error log
EX
sketch_nov15f:95: error: expected `;' before 'much'

Heres the code i fixed But cant get the last two errors to go away

include <AFMotor.h>

include <Stepper.h>

/*
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);
}

{
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;
}

@zrhkc7

This comment has been minimized.

Show comment Hide comment
@zrhkc7

zrhkc7 Mar 17, 2016

between the lines of

motor4.setSpeed(200);
}

{
long duration, inches;

it needs a line

long ping()

to make it compile

zrhkc7 commented Mar 17, 2016

between the lines of

motor4.setSpeed(200);
}

{
long duration, inches;

it needs a line

long ping()

to make it compile

@ahmedmohamedmohamedgamal

This comment has been minimized.

Show comment Hide comment
@ahmedmohamedmohamedgamal

ahmedmohamedmohamedgamal Apr 15, 2016

delete float and write again

delete float and write again

@ansarith

This comment has been minimized.

Show comment Hide comment
@ansarith

ansarith Jun 28, 2016

Here are the code that I already fixed it and it successfully compiled. If you have problem with #include <AFMotor.h> please download https://github.com/adafruit/Adafruit-Motor-Shield-library and open your sketch. Click on sketch and then select "Include Library" then Choose "Add Zip library" so you will browse to file you downloaded.
Thanks!
/*
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;
}

Here are the code that I already fixed it and it successfully compiled. If you have problem with #include <AFMotor.h> please download https://github.com/adafruit/Adafruit-Motor-Shield-library and open your sketch. Click on sketch and then select "Include Library" then Choose "Add Zip library" so you will browse to file you downloaded.
Thanks!
/*
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;
}

@keshri-prasanjeet

This comment has been minimized.

Show comment Hide comment
@keshri-prasanjeet

keshri-prasanjeet Jul 4, 2016

Please tell me , is my code correct?

/IR + Ultrasonic Line follower robot (black line on white surface)
This robot also has ultrasonic range sensor that enables it to stop when any obstacle is on the black line.
keshri-prasanjeet, keshriprasanjeet2@outlook.com
/
//inputs:
‪#‎ define ‬LIR 2 //left ir sensor

define RIR 3 //right ir sensor

define echoPin 4 //echo pin of ping )))

//outputs:

define direction_L 5 //left forward

define direction_R 6 //right forward

define triggerPin 7 //trigger pin of ping )))

void setup() { //setup function
Serial.begin(9600); //serial communication
for(int i = 2; i < 5; i ++){ //initialize inputs:
pinMode(i, INPUT);
}
for(int i = 5; i < 8; i ++){//initialize outputs:
pinMode(i, OUTPUT);
}
Serial.println("ready"); //starting message
}
void loop() { //loop function
//reading sensors
int DLIR = digitalRead(LIR); //left ir
int DRIR = digitalRead(LIR); //right ir
//trigger pulse of ping )))
digitalWrite(triggerPin, LOW);
delay(5);
digitalWrite(triggerPin, HIGH);
delay(5);
digitalWrite(triggerPin, LOW);
float dist = pulseIn(echoPin, HIGH); //get time
dist = dist / 58; //scaling to cm
Serial.println(dist); //print distance
if(dist > 10){ //if no object on the path:
if(DLIR == HIGH && DRIR == HIGH){ //if robot is on the BLACK path
//go forward:
analogWrite(direction_L, 255);
analogWrite(direction_R, 255);
Serial.println("go forward");
}
else if(DLIR == HIGH && DRIR == LOW){ //right sensor missed the parth
//turn left:
analogWrite(direction_L, 128);
analogWrite(direction_R, 255);
Serial.println("turn left");
}
else if(DLIR == LOW && DRIR == HIGH){ //left sensor missed the parth
//turn right:
analogWrite(direction_L, 255);
analogWrite(direction_R, 128);
Serial.println("turn right");
}
else{ //path missed!
//stop:
analogWrite(direction_L, 0);
analogWrite(direction_R, 0);
Serial.println("parth missed!");
}
}
else{//object found
//stop:
analogWrite(direction_L, 0);
analogWrite(direction_R, 0);
Serial.println("object found");
}
delay(100);
}

Please tell me , is my code correct?

/IR + Ultrasonic Line follower robot (black line on white surface)
This robot also has ultrasonic range sensor that enables it to stop when any obstacle is on the black line.
keshri-prasanjeet, keshriprasanjeet2@outlook.com
/
//inputs:
‪#‎ define ‬LIR 2 //left ir sensor

define RIR 3 //right ir sensor

define echoPin 4 //echo pin of ping )))

//outputs:

define direction_L 5 //left forward

define direction_R 6 //right forward

define triggerPin 7 //trigger pin of ping )))

void setup() { //setup function
Serial.begin(9600); //serial communication
for(int i = 2; i < 5; i ++){ //initialize inputs:
pinMode(i, INPUT);
}
for(int i = 5; i < 8; i ++){//initialize outputs:
pinMode(i, OUTPUT);
}
Serial.println("ready"); //starting message
}
void loop() { //loop function
//reading sensors
int DLIR = digitalRead(LIR); //left ir
int DRIR = digitalRead(LIR); //right ir
//trigger pulse of ping )))
digitalWrite(triggerPin, LOW);
delay(5);
digitalWrite(triggerPin, HIGH);
delay(5);
digitalWrite(triggerPin, LOW);
float dist = pulseIn(echoPin, HIGH); //get time
dist = dist / 58; //scaling to cm
Serial.println(dist); //print distance
if(dist > 10){ //if no object on the path:
if(DLIR == HIGH && DRIR == HIGH){ //if robot is on the BLACK path
//go forward:
analogWrite(direction_L, 255);
analogWrite(direction_R, 255);
Serial.println("go forward");
}
else if(DLIR == HIGH && DRIR == LOW){ //right sensor missed the parth
//turn left:
analogWrite(direction_L, 128);
analogWrite(direction_R, 255);
Serial.println("turn left");
}
else if(DLIR == LOW && DRIR == HIGH){ //left sensor missed the parth
//turn right:
analogWrite(direction_L, 255);
analogWrite(direction_R, 128);
Serial.println("turn right");
}
else{ //path missed!
//stop:
analogWrite(direction_L, 0);
analogWrite(direction_R, 0);
Serial.println("parth missed!");
}
}
else{//object found
//stop:
analogWrite(direction_L, 0);
analogWrite(direction_R, 0);
Serial.println("object found");
}
delay(100);
}

@jhenceon

This comment has been minimized.

Show comment Hide comment
@jhenceon

jhenceon Jul 9, 2016

Downloading, copy/pasting raw code created initial frustration with this code. The initial float pin() errors posted a few years ago are resolved by simply retyping them in the Arduino IDE. You'll see the float text turn green - and then you realize what a noob you are ;)

If you are equally noobish as myself, you may get the stray /357 error. I invite you to look to see where the line breaks are in the code. I had to clear 8 lines, but if you pay attention you'll see the problems.

Thanks to Make and the contributors for keeping a fun project up a running for several years!

jhenceon commented Jul 9, 2016

Downloading, copy/pasting raw code created initial frustration with this code. The initial float pin() errors posted a few years ago are resolved by simply retyping them in the Arduino IDE. You'll see the float text turn green - and then you realize what a noob you are ;)

If you are equally noobish as myself, you may get the stray /357 error. I invite you to look to see where the line breaks are in the code. I had to clear 8 lines, but if you pay attention you'll see the problems.

Thanks to Make and the contributors for keeping a fun project up a running for several years!

@rene9900

This comment has been minimized.

Show comment Hide comment
@rene9900

rene9900 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;
}

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;
}

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