Code Repository!

Can we get everyone who has got their code to successfully run to post their files here? Ive been looking all over the site for a central area for code, and I cannot find one. If there is one, point me in that direction, if not, lets all start posting here!

Try not to paste lines of code here, lets keep it clean. uploaded files only please!

Format:

Name of code,

What it's for

File (zip or IDE compatible file)

 

What do you guys think?

You can only attach a file
You can only attach a file directly to the post if it is your post. I think the code should go on the robot page so you not only see the code but also how it is used and wired up.

Top left corner of any
Top left corner of any webpage on this website START HERE

yeah…

Yea, I can see that. I figured using the term "sketch" would tip off to the fact that im using an arduino and the start here is for a picaxe. I want a ready to go, out of the box pde file to run a robot using IR or a ping sensor… but thanks anyway.

 

Anyone else?

Object Exchange
The Object Exchange has code modules that are freely redistributable and free for use. I use it a lot. OBEX

Thanks Thegrue, glad at

Thanks Thegrue, glad at least one person is interested in helping.

 

@voodoobot, I am currently learning the language and am working to decipher the codes I HAVE found. I just want something that works out of the box, so that I can get an object avoiding bot going. I will build on the code then. Until i learn the code im looking for something someone else has written. This IS supposed to be open source right?

 

Instead of telling me to learn code and insulting my navigational skills, maybe some of you could help? Thanks for making a new person welcome!

I still wrestle with
I still wrestle with programming. I also chose a complex Processor to learn but that hasn’t stopped me from trying to learn Spin Language and Assembler on the Propeller. Learn where the resources for your processor are like forums and video tutorials or webinars are that deal specifically with the processor of your choice, becuse this is a general site with many flavors of MCU used and Picaxe seems to be preferred here.

lol…I think I’ll pass

lol…I think I’ll pass on helping…you seem to think you’re entitled to it…

and lets see…telling you??.."I would recommend trying to learn it and if you get stuck, then ask."

recommend is not telling…

not sure how that was insulting…but take it as you like I guess…

**Here ya go. **

Here ya go.

 

Hi jair, the Arduino start

Hi jair, the Arduino start here robot is linked in the first paragraph of the picaxe start here robot. Adding sonar or IR distance sensors should be easy. Youll find people are more willing to help you when youve already shown you are trying to help yourself. Open source doesnt mean were some kind of help desk techs just here to make you happy.

Everyone started with nothing and learnt it slowly over time, I started with Arduino too. I read through the tutorials all over the net, looked up some sonar code and stuck it all together. Really it`s not hard if you take the time and I learnt a hell of a lot faster than if I had just downloaded someone elses sketch and tried to decipher it.

If you want out-of-the-box instant gratification maybe a robot kit would be more suitable?

so yeah

I appreciate all the sarcasm. So greatt o see that this is such a helpful community.

 

@Ezekiel - Thanks for the input. I really do appreciate help when I can get it. An out of the box robot is senseless, its out of the box code i am looking for.

 

@everyone else - I reverse engineer code. It’s how I learn. Starting from scratch does absolutely nothing for me. I need a viable working code to dismantle and learn from. Everyone learns differently. I’m sure at least one or two of you out there did some frankenstein code just to see what it did, based off of someone else’s code.

I don’t expect you to be some kind of help desk… I DO however expect you to point me in the right direction. I asked for a listing of codes, so that I may try them out. I did not ask to repeatedly be pointed to the start here robot. That robot has nothing to do with what i am building. My original post was intended to try to make a single area where new people like myself can come to pull code, instead of looking all over for it.

 

Furthermore, I would have used the search function on this site, except for the fact that it doesnt work. I have tried it on 2 computers and my iphone. It doesnt work, so I cannot do a site-wide search. I’m new to robotics, not new to forums. I know how to find what I am looking for if i want to spend hours looking for it. Hell, the designers of arduino go out of their way to say, why spend countless hours starting from scratch when you can use hardware and code that have been written and tested by good engineers (from getting started with arduino book published by MAKE).

So, once again, I am going to ask… I have poured through every page in the robot section, as well as the how-to’s, and have found only a few files that people have been kind enough to post, but each one has some modification or another. I just want a code that drives two motors (that will be on treads) through an L293, using an IR or ping sensor on a servo for sweep to navigate obstacles. Tested. Working. Effortless.

Does such a code exist?

 

</rant>

Youre right, an out of the**<br><p>Youre right, an out of the box robot is pretty pointless to me too. For that exact reason though code that is tested and works on one robot might not work on another, especially because they arent kits. Example.. you havent mentioned how you have the hardware wired up so even the best code needs your modifications on which pins you have used before it will work.

Do you have a working robot you want to try the code on, or are you just looking for something to study? I pasted some early code formy first robot below. It uses the analog sharp IR and a scanning servo. I deleted the part with the IR remote control because it sounds like you arent interested in that bit. Just 1 caveat.. it doesnt compile as-is with the latest Arduino IDE because the servo library has changed.

 

/

Wallace the Robot
27/08/08
Aaron Woodland

*IR remote key capturing.
*chassis movement functions.
*servo head moving.
*sharp IR reading and storing distance to an array.
autonomous or remote mode.
auto mode - drives around and avoids things sort of.
remote - accepts remote commands.

/

#include <ServoTimer1.h>

/
Variable/constant declarations /
int distWARN = 40; // distance constants
int distDANG = 15;

ServoTimer1 servo1;

int leftMotorA = 3;
int leftMotorB = 11;
int rightMotorA = 6;
int rightMotorB = 5;
int distance[20]; // distance array

int evade; // evasive action true or false

/
Function declarations /
int getDist();
void interrupted();
int getIRKey();
void forward(int rate);
void backward(int rate);
void left(int rate);
void right(int rate);
void allStop();
void scanDist();


void setup() {
pinMode(leftMotorA, OUTPUT); // Motor driver pins
pinMode(leftMotorB, OUTPUT);
pinMode(rightMotorA, OUTPUT);
pinMode(rightMotorB, OUTPUT);
servo1.attach(10);
Serial.begin(9600);
}


void loop()
{
allStop(); // stop and take a distance reading
scanDist();
if(evade == 1) // Evasive manouver. back up
{
backward(200);
delay(2000);
evade = 0;
Serial.println("backing up…");
}
if(distance[8] && distance[9] && distance[10] > distWARN) // front is clear
{
Serial.println("going forward…");
forward(255);
} else // uh oh something is in my way!!!
{
allStop();
delay(500);
if(distance[5]||distance[6]||distance[7] < distance[8])
{
Serial.println("turning left…");
left(150);
}
else if(distance[13]||distance[12]||distance[11] > distance[10])
{
Serial.println("turning right…");
right(150);
}
}
delay(2000);
}
}

/
Movement routines /
void forward(int rate)
{
if((rate > 255) || (rate < 0)){ return; }
analogWrite(leftMotorA, 0);
analogWrite(leftMotorB, rate);
analogWrite(rightMotorA, 0);
analogWrite(rightMotorB, rate);
}

void backward(int rate)
{
if((rate > 255) || (rate < 0)){ return; }
analogWrite(leftMotorB, 0);
analogWrite(leftMotorA, rate);
analogWrite(rightMotorB, 0);
analogWrite(rightMotorA, rate);
}

void allStop()
{
analogWrite(leftMotorA,0);
analogWrite(leftMotorB,0);
analogWrite(rightMotorA,0);
analogWrite(rightMotorB,0);
servo1. write(90);
}

void left(int rate)
{
if((rate > 255) || (rate < 0)){ return; }
analogWrite(leftMotorB, 0);
analogWrite(leftMotorA, rate);
analogWrite(rightMotorB, rate);
analogWrite(rightMotorA, 0);
servo1.write(140);
}

void right(int rate)
{
if((rate > 255) || (rate < 0)){ return; }
analogWrite(leftMotorB, rate);
analogWrite(leftMotorA, 0);
analogWrite(rightMotorB, 0);
analogWrite(rightMotorA, rate);
servo1.write(40);
}


/
Sensor functions **/

void scanDist()
{
for(int p=0; p<20; p++)
{
servo1.write(40 + p
5);
if(p == 0) { delay(300); } else { delay(100); }
distance[p]= getDist();
if(distance[p] <= distDANG){ evade=1; } // Evasive action planned

}
servo1.write(90);
}

int getDist()
{
int runningTotal = 0;
int distRead = 0;
int distAvg = 0;
int distance = 0;
// Average the sensor reading 5 times
for (int i=0; i < 5; i++)
{
distRead = analogRead(inputIr);
runningTotal = runningTotal + distRead;
}
distAvg = (runningTotal / 5);

printString("Distance(cm) =\t");
// Calculate linear slope of reading (thanks, Acroname!):
if(distAvg > 3){ distance = (6787 / (distAvg - 3)) - 4; } else{ distance = 80; } //catch divide by zero
if(distance > 80){ distance = 80; } //prevent ridiculous numbers coming out from the divide
printInteger(distance);
printString("\n\r");

return distance;
}

Another Arduino robot

Another Arduino robot ressource including source code can be found at makezine. Makey the robot

 

Its funny you mention that

Its funny you mention that article, I’m already reading it right now… lol. I have already posted in the comments asking the author about his code. The code is perfect, as it was written in processing, which i understand a heck of a lot better than C. The only problem that I saw in there was that his code for Object avoidance does not mention the servo.

Can anyone confirm this? I cant find any mention of servo contro. Here is his code:

 

/*
* 06_Object_Avoidance
*
* Makey robot roams around, while avoiding objects
* sensed with the Ping ultrasonic rangefinder.
*
* Makey Robot, MAKE Magazine, Volume 19, p. 77
* Created: June 2009 Kris Magri
* Modified:
*
*/

#define pingPin 9 // Ping ultrasonic sensor on pin D9.
#define leftDir1 7 // Left motor direction 1, to AIn1.
#define leftDir2 6 // Left motor direction 2, to AIn2.
#define rightDir1 5 // Right motor direction 1, to BIn1.
#define rightDir2 4 // Right motor direction 2, to BIn2.

#define BOUNDARY 20 // (cm) Avoid objects closer than 20cm.
#define INTERVAL 25 // (ms) Interval between distance readings.

// setup
// Set motor pins as OUTPUTS, initialize Serial

void setup()
{
pinMode(leftDir1, OUTPUT); // Set motor direction pins as outputs.
pinMode(leftDir2, OUTPUT);
pinMode(rightDir1, OUTPUT);
pinMode(rightDir2, OUTPUT);
Serial.begin(9600);
}


// Main program
// Roam around while avoiding objects.
//
// Set motors to move forward,
// Take distance readings over and over,
// as long as no objects are too close (determined by BOUNDARY).
// If object is too close, avoid it – back up and turn.
// Repeat.

void loop()
{
long distance; // Distance reading from rangefinder.

forward(); // Robot moves forward continuously.
do
{
distance = readDistance(); // Take a distance reading.
Serial.println(distance); // Print it out.
delay(INTERVAL); // Delay between readings.
}
while(distance >= BOUNDARY); // Loop while no objects close-by.

// Robot has sensed a nearby object and exited the while loop.
// Take evasive action to avoid object.
backward(); // Move backward 500ms.
delay(500);
rightTurn(300); // Turn right 300ms.

} // end Main program


// forward
//
// Move robot forward by setting both wheels forward.
// Will persist until something else changes the
// motors’ directions.

void forward()
{
digitalWrite(leftDir1, LOW); // Left motor forward.
digitalWrite(leftDir2, HIGH);
digitalWrite(rightDir1, LOW); // Right motor forward.
digitalWrite(rightDir2, HIGH);
}


// backward
//
// Move robot backward by setting both wheels backward.
// Will persist until something else changes the
// motors’ directions.

void backward()
{
digitalWrite(leftDir1, HIGH); // Left motor backward.
digitalWrite(leftDir2, LOW);
digitalWrite(rightDir1, HIGH); // Right motor backward.
digitalWrite(rightDir2, LOW);
}


// rightTurn
//
// Turn robot to right by moving wheels in opposite directions.
// Amount of turning is determined by duration argument (ms).

void rightTurn(int duration)
{
digitalWrite(leftDir1, HIGH); // Left motor backward.
digitalWrite(leftDir2, LOW);
digitalWrite(rightDir1, LOW); // Right motor forward.
digitalWrite(rightDir2, HIGH);
delay(duration); // Turning time (ms).
}


// readDistance
// Take a distance reading from Ping ultrasonic rangefinder.
// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

long readDistance()
{
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 an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// Convert the time into a distance.
cm = microsecondsToCentimeters(duration);
return(cm);
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance traveled.
return microseconds / 29 / 2;
}

The 'object following
The ‘object following example’ uses the servo to look for obstacles with the ping sensor.