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

Could you cut’n’paste the
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
Yeah we have to know the errors

If I had to guess I’d say
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.

one thing to make sure of
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
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.

You can’t understand other’s code…
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.

arduino ide v.12
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 have done that, i can use
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

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

 

cool, thank you i think i

cool, thank you i think i can get this too work. i have a bse to tinker with. any idea why that other one ifound did not work?

i can not read coding to well to see why i didn’t work. i have all the librarys i need and just updated to todays new afmotor library. also using the 328 chip.

i have the 328 chip as

i have the 328 chip as well

but havent used it yet it is for another project later on

i have no Idea about the bse

and since i finally just debuggged mine but havent tested it yet with the motors on

ill take a look at yours now

oops i meant base to tink
oops i meant base to tink with

I tried to copypaste your

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

thank you jka, i found more
thank you jka, i found more problems. you gave me some good pointers. the space, the // error, but i also found that the quote marks were the wrong type too got it compiled yaaa…!!!

now to try to figure out how

now to try to figure out how to add the sevro panning like all the others, but it can wait till it moves a bit first

 

Ah, the quote marks. I found

Ah, the quote marks. I found those as well, but thought they were frmo the cut’n’paste.

A general comment about your code. I don’t like your use of global variables. It is dangerous to use the same variable all over the program, because you can easily get in a situation where you modify the variable as a side-effect. Take for example a statement like this:

dist = distCalc();
delay(20);
if (distCalc()-dist > 10) {
//yadda yadda yadda
}

…to see if the distance has changed over time. You start by setting dist to the current distance, wait some time and see if the distance - old distance is greater than 10.The problem is, that in the call to distCalc() you modify dist as a side-effect, so when you return to the if-statement and compare with dist, you get a 0 difference.This can be hard to debug, because apparently there is nothing wrong with the statement above. Use a local dist declaration in distCalc() and loop() andpass it as a paramter to the functions that needs it.

 

um ok wow, did not know
um ok wow, did not know that. i don’t really know anything about this stuff. i could at one time read a basic code and run the program in my head. this stuff i way beyond me, but i am learning slowly.

We all started there. The
We all started there. The difference is, that when I started programming, I didn’t have an internet where I could ask questions :slight_smile:

i had a teacher who i
i had a teacher who i quickly learned more than he knew. which was sad i as i knew very little.