Let's Make Robots!

code i found for just what i have built. help :)

i need to know what is wrong with this code. I know absolutely nothing about coding. but i can build anything. (kind of a hobby) I can build it but have no idea how to use it yet.

arduino deci.

ladyada motor shield

lithium backpack and various other batteries

 

here is the problem, the code i found does not compile. comes up with errors, but i dont know how to fix them, i have done several things and have got past a few, only to have others pop up.

*/

//    GETTING EVERYTHING SETUP AND READY
#include <AFMotor.h>
AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
unsigned long echo = 0;
unsigned long ultrasoundValue = 0;
int ultraSoundSignal = 14;    // sets enable to pin 14
float dist = 0;      // variable to store the converted distance in inches

// ***************************   MAIN STRAIGHT ROUTINE  *********************************** \\
void straight()
{
motor.run(FORWARD);
motor2.run(FORWARD);
Serial.print(”Forward Hoooo!”);  // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}
// ***************************   MAIN TURNING ROUTINE  *********************************** \\
void turnRight()
{
motor.run(BACKWARD);
motor2.run(FORWARD);
Serial.print(”Turning Right!!”);// ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}

// ***************************   PING FUNCTIONS TO GET DISTANCES  *********************************** \\
float distCalc() // distance calculating function converts analog input to inches
{
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor

echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
dist = (echo / 58.13 8) * .39; //convert to CM then to inches

return dist;

}

// ***************************   THE AVOIDING OBJECTS ROUTINE  *********************************** \\
void AvoidObjects()
{
if(dist < 8)                            // if the distance is less than 8 inches
{
Serial.print(”object detected closer than allowed!!!”);   // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
turnRight();                            // turns right using turnRight function
delay(100);
}

else                                      // otherwise
{
straight();                            // go straight using the straight function
Serial.print(” continuing forward “);  //  ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}
}

// ***************************   SETTING UP THE PROGRAM  *********************************** \\
void setup()
{

motor.setSpeed(200);          // sets motor 1 speed to 200
motor2.setSpeed(200);          // sets motor 2 speed to 200

pinMode(ultraSoundSignal, OUTPUT);  // sets enable as output

straight();                    // initializes go straight
}

// ***************************   MAIN PROGRAM LOOPS  *********************************** \\
void loop() {
Serial.begin(9600);
distCalc();
AvoidObjects();
delay(20);
int x = 0;
x = dist;
Serial.println(x);  //     ADDED TO APPEND DISTANCE TO SERIAL MONITOR SO I COULD SEE PROBLEMS..
delay(250);
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Could you cut'n'paste the errors in here? It will make it easier for us to help you find the error.
Yeah we have to know the errors
If I had to guess I'd say you probably need to download the AFMotor.h library from the adafruit site for use with the adafruit motorshield....Just a guess.
is the editor your using. Prolly the one that comes with the arduino. The code looks fine but it may have an issue compiling if the editor is not set to the correct language. I believe that the arduino can be programmed in both basic and c. This is the c language. If it is set for basic then the basic compiler will not compile this code. I have never messed with the arduino so I would not know how to change it but it is something to possibly look at.
thre are too many errors to list, as for the sensor it is the ping. but that does not matter. thanks for the help. i am using the arduino ide v.12. as soon as i fix one thing it comes up with another. i shall just quit this code and look for something else. As i have stated i can build anything given the parts and directions, but i have no idea how to use it yet. zero on all programming except very basic, basic from about 20 years ago., once again thanks for the help.
This IDE should have some already made libraries as far as I can see. I would look at simple code samples from the manual on certain things. Don't fully use someones code but you can learn from it and adapt it to your needs.

I tried to copypaste your code into the Arduino IDE and compile. I use version 0011 and found a few things:

 

1. Remove the \\ at the end of the comments

2. in "dist = (echo / 58.13 8) * .39; //convert to CM then to inches " you have a space between 3 and 8.

 

I think thats all I changed to make it compile

I am a builder and have tried to use other people's code. It don't work, yo. Keep building! --but in terms of code, start with getting a LED to blink and go from there.
i have done that, i can use the motor test code, i kinda understand it, the ping code i can use and understand it a bit, this code looks just like all the rest but i doesn't work. i can not even upload it. i always compile before uploading new stuff. i am gett a lot of advise but not much is helpful. please just forget about this thread and let it die. i will look else where. thank you for the advise.

heres what i use for my code for the same setup 

i even have the video of it working  to here 

you will need this file and this one

this only goes forward and reverse

turing you will hvae to add but hope this helps

 #include <AFMotor.h>

AF_DCMotor motor(4, MOTOR12_64KHZ);
AF_DCMotor turn(2, MOTOR12_64KHZ);
int pingPin = 9;

void setup()
{
  Serial.begin(9600);
  motor.setSpeed(200);
  turn.setSpeed(0);
}

void loop()
{
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We 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);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
 


  delay(100);
 if (inches > 15) {
 
  motor.run(FORWARD);
  turn.run(RELEASE);
 }
 else {
    motor.run(BACKWARD);
    turn.run(BACKWARD);
    delay(400);
   
 }
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}