Modifying source code (IR instead of ping)

So I have finally found a code that seems like it is going to work from this node:

https://www.robotshop.com/letsmakerobots/node/2744

 

However, I want to use a sharp IR sensor instead of the ping, as the sharp is cheaper and im on a strict budget for this project. I have no idea how to change the code, as I am just beginning to learn the language. Can anyone help me out? Here is the source code from that node:

 

/*
Rover - 1.0
Buhatkj - Oct/2008
*/
#include <Servo.h>

//mimimum safe distance in front of us in inches
int minSafeDist = 7;

//PING and pan servo head
unsigned long echo = 0;
int ultraSoundSignal = 12; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
int servoPin = 10;
Servo servo1;
int SERVOCENTER = 97;
int SERVOLEFT = 67;
int SERVORIGHT = 127;

//motor 1 = right
int m1a = 4;
int m1b = 3;
int m1e = 2;

//motor 2 = left
int m2a = 7;
int m2b = 6;
int m2e = 5;

//pan ping distance vars all in inches
int leftDist = 0;
int rightDist = 0;
int centerDist = 0;

//for debugging
int ledPin = 13; // LED connected to digital pin 13

//this is the cheapest, crappiest circular buffer evar
int actionBuffer[10];

void initBuffer(){
for(int i = 0; i < 10; i++){
actionBuffer[i] = 0;
}
}

void pushBuffer(int x){
for(int i = 0; i < 9; i++){
actionBuffer[i+1] = actionBuffer[i];
}
actionBuffer[0] = x;
}

int oscillating(){
int osc = 0;
for(int i = 0; i < 5; i++){
if ((actionBuffer[i] == 1)||(actionBuffer[i] == 2)){
osc++;
}
}
return osc;
}

void setup() // run once, when the sketch starts
{
//init all pins
pinMode(ledPin, OUTPUT); // sets the digital pin as output

pinMode(ultraSoundSignal,OUTPUT);
pinMode(servoPin, OUTPUT); // sets the digital pin as output
servo1.attach(servoPin);
servo1.write(97); //center our servo

pinMode(m1a, OUTPUT); // sets the digital pin as output
pinMode(m1b, OUTPUT); // sets the digital pin as output
pinMode(m1e, OUTPUT); // sets the digital pin as output

pinMode(m2a, OUTPUT); // sets the digital pin as output
pinMode(m2b, OUTPUT); // sets the digital pin as output
pinMode(m2e, OUTPUT); // sets the digital pin as output

//enable the motors
digitalWrite(m1e, HIGH); // on
digitalWrite(m2e, HIGH); // on

initBuffer();

Serial.begin(9600);
Serial.println("starting...");

//SelfTest();
//Serial.println("done self test...");
}

unsigned long ping(){
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
ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
Serial.println(ultrasoundValue);
return ultrasoundValue;
}

void AllStop(){
digitalWrite(m2e, LOW); // off
digitalWrite(m1e, LOW); // off
}

void AllForward(){
digitalWrite(m2e, HIGH);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
digitalWrite(m1e, HIGH);
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
}

void AllReverse(){
digitalWrite(m2e, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
digitalWrite(m1e, HIGH);
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
}

void SpinRight(){
digitalWrite(m2e, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
digitalWrite(m1e, HIGH);
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
}

void SpinLeft(){
digitalWrite(m2e, HIGH);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
digitalWrite(m1e, HIGH);
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
}

void LookAround(){
//look around
servo1.write(SERVOLEFT); //left servo
delay(175); // waits for a second
leftDist = ping();
servo1.write(SERVOCENTER); //center our servo
delay(175); // waits for a second
centerDist = ping();
servo1.write(SERVORIGHT); //right servo
delay(175); // waits for a second
rightDist = ping();
servo1.write(SERVOCENTER); //center our servo
delay(175); // waits for a second
Serial.println(leftDist);
Serial.println(centerDist);
Serial.println(rightDist);
}

void LookAhead(){
//servo1.write(SERVOCENTER); //center our servo
//delay(175); // waits for a second
centerDist = ping();
}

void loop() // run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
LookAhead();
//LookAround();

if(centerDist >= minSafeDist){
AllForward();
pushBuffer(0);
delay(100);
}else{
//stop
AllStop();
delay(100);

//have a look around
LookAround();
if((oscillating() > 2)||(rightDist == leftDist)){
//best to turn around
SpinLeft();
initBuffer();
pushBuffer(3);
delay(2000);
//go in the direction that read farthest
}else if(leftDist > rightDist){
//best to turn left
SpinLeft();
pushBuffer(1);
delay(500);
}else if(rightDist >leftDist){
//best to turn right
SpinRight();
pushBuffer(2);
delay(500);
}else{
//this should NEVER happen
//best to turn around
SpinLeft();
initBuffer();
pushBuffer(3);
delay(2000);
}
}
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
}



Any help would be appreciated. Thanks!

did you search on arduino.cc
did you search on arduino.cc for info on how to interface the arduino with an IR sensor?

yea.

Yea I did. I found some scattered code references, problem is, the codes make no sense to me. I know HTML, it has a logical flow to the code, and I have picked up on that same trend with this code, but it is SEVERELY more complex than HTML. I’m really looking for someone to: A) Help me understand the flow of code and how to insert new commands without a million compile errors, and B) maybe someone to put the code together for me with some // instructions so that I can understand what it does and why it is there. If i have a working code to play with, I have something to roll back to when I break it.

I know it is a lot to ask, but i REALLY want to learn this stuff. Can anyone help out with this?

what are you making?
what are you making?

No offence…

But I think you’re approach is wrong.

I’d recommend starting out by simply connecting your IR sensor, read up on how it works, how to calibrate it (the datasheet should mention that otherwise google it), and play around with it until you understand how it works and you’re getting useful readings.

Do the same with a servo. Find some SIMPLE code examples and take it from there.

If that is too hard I suggest starting out by blinking some LEDs and sending a “hello” via serial and other basic stuff. That’s what I did a few months ago when I got my Arduino.

In other words take it step by step, and soon you’ll be able to write your own code. Both this forum and the Arduino forum are excellent places to ask specific questions, but getting people to write your code for you is another thing.

The Arduino learning and playground sections are filled with all sorts of code examples you can start out with. There is also a function for reading (GP2D12) sharp sensors, which you could use as a starting point once you get that far (if that is the sensor you have).

Anyway good learning and have fun :wink:

just a simple rover. all the
just a simple rover. all the codes i have found compile, but they require a ping sensor instead of IR.

For some reason, everyone
For some reason, everyone keeps telling me that staring that way is best. I’ve explained in another thread that this is not how i learn. I learned HTML by taking websites and disecting their code. I learn best by taking something that works apart and finding out what makes it tick. I hope someone on this forum understands this. Thanks for the advice though.

Fair enough

But 1st off…HTML isn’t a programming language. It’s a markup language and a bit simpler to disect since it’s very concrete. Programming is somewhat more abstract and a bit of “theory” can be quite useful.

Besides an IR sensor isn’t just an IR sensor. There are different models that work differently and even 2 units of the same model have to be calibrated specifically for that unit. Generally you’ll also wanna get to know a bit about the hardware besides the actual programming.

However I understand. I also like to learn that way and I taught myself how to program (games) many years ago mainly by disecting code examples I found.

All I’m saying though is that you may benefit from starting out disecting some simpler examples that focus on one thing at a time (LEDs, IR sensor, servo, sonar, serial etc).

I’m afraid that it’s very

I’m afraid that it’s very hard to build an arduino robot based on someone else’s code if you don’t know anything about programming. I would advice you to just start out by learning the arduino programming step by step go from hello world blinkng led to reading switches then try to drive a servo read a pontentio meter (which by the way uses the same techniques to read a sharp Ir) etc… That way you learn the programming, once you grasp the basic programming flow of the arduino it becomes much easier to adapt an existing code to your hardware setup.

 

I understand that most of

I understand that most of you think i8t would be easier for me to learn that way. Give it up. I’m set in my ways and will have an easier time learning in the manner I am accustomed to. Just disecting others’ code, I have already learned how to define constants and call apon them. I have learned how to identify pins to the IDE and how to set them input/output. I have also learned how to set various pins high or low. I have the MAKE: Getting started with Arduino book, and I have read through a bunch of that too. But since nobody seems to want to help me, instead of constantly suggesting to start from A and move to B, instead of backwards, can someone maybe answer another question?

If I have a code set up to call on a ping sensor for data, and that ping sensor is nonexistant because I cant afford it yet, what would happen if i hooked everything else up and tried to run it on my Arduino? The code uploaded fine, but i havent got everything hooked up yet…

 

Just a side thought here… And I mean no disrespect to anyone on this site… But everyone here seems to be FAR more difficult to speak with than any other forum i have ever been to. People seem to look down on me just because my learning style varies from theirs. When I try to explain, they post snide comments and then look down on me… Someone might want to suggest some kind of sensitivity training or something… srsly, everyone on these forums seems… crabby to me.

I learn by disecting code

I learn by disecting code too. Thats ho wI have learned 3 or 4 different programming languages. The problem is in robotics there are a lot of numbers used tat can confuse someone trying to read someone else’s code. For example when setting a servo position you refer to it by its pin number. You have a lot of numbers like 8 9 11 16 that make no sense unless you know what the code does already and how it is hooked up.

I learn programming languages by tearing the code apart but I was lost with robotics until I started small and breadboarded everything and took it a piece at a time. TRUST ME we have had many people saying the same things as you and all of them tried to disect code and kept coming back with questions and confused and none of them finished their first robot.

how about you write your own
how about you write your own code

Well…since I don’t know

Well…since I don’t know how the Arduinos work…my best guess would be to do this:

Make a new method for your sensor. Let’s call it "look"

int look() {

// In here you check the status of the pin that the IR sensor is on. If it is low (nothing close enough to trigger) return a 0, else if it is high (something is in range), return a 1.

}

Now you have access to the status of the IR sensor. You must modify your “look around” code to match.

void LookAround(){
//look around
servo1.write(SERVOLEFT); //left servo
delay(175); // waits for a second
left = look();
servo1.write(SERVOCENTER); //center our servo
delay(175); // waits for a second
center = look();
servo1.write(SERVORIGHT); //right servo
delay(175); // waits for a second
right = look();
servo1.write(SERVOCENTER); //center our servo
delay(175); // waits for a second
Serial.println(left);
Serial.println(center);
Serial.println(right);

}

This should print out 1s and 0s according to whether or not something is in the way of the IR sensor. Lastly, modify this bit of code by changing the variable name (if you’ve done it) to check whether or not something is in the way…

if(center == 0){
AllForward();
pushBuffer(0);
delay(100);
}else{
//stop
AllStop();

delay(100);

Hope this has helped…

- P