Interpreting sensor input for robot.

Hello, I am currently trying to make a robot that will follow me by using a HiTechnic inferred LED beacon and a HiTechnic Ir seeker sensor. What I want it to do is if the beacon is in sectors 1 thru 4 the robot would turn left until it's in sector 5, and if the beacon in sectors 6 thru 9 the robot would turn right till it's in sector 2, and if it's in sector 2 it does nothing except detect if I am farther than 2 ft using a Parallax Ultrasonic distance sensor, and if I am it will move forward until I am no longer farther than 2 ft. I need help with making my Arduino Uno Board interpret these sensors recorded values so that i can use them in If-Then-Else statements that tell the motors what to do. Can anyone help me?

References/Research:

http://www.usfirst.org/sites/default/files/uploadedFiles/Robotics_Programs/FTC/FTC_Documents/Using_the_IR_Seeker.pdf

http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&key=HBK2100

http://learn.parallax.com/KickStart/28015

http://blog.birkb.eu/hitechnic-infrared-seeker-library-for-arduino/

are you sure this sensor can be connected to the Arduino and not just the NXT brick ?
how does the connector look like ?

I would use the cable that comes with the sensor to connect it to the Arduino. The cable has 6 wires inside it, White, Black, Red, Green, Yellow, and Blue. White goes to a battery +9V (Vcc), Black goes to Arduino GND, Red goes to battery - (GND), Green goes to Arduino +3.3V (Vcc), Yellow goes to Arduino SCL, and Blue goes to Arduino SDA.

Click this link for more info:

http://blog.birkb.eu/hitechnic-infrared-seeker-library-for-arduino/

that means you are using the I2C connection to the Arduino; SDA = pin 4, SCL = pin 5 .

have you already tried connecting all these and just trying to read the raw data FROM the sensor and display it to the Serial Monitor in Arduino ?

What i want to know is do I need any special code to interpret the data coming from this sensor, and if so what is it? What I want is the directional data to output a number between 1 and 9 representing each of the sectors.

Click this for more info:

http://www.usfirst.org/sites/default/files/uploadedFiles/Robotics_Programs/FTC/FTC_Documents/Using_the_IR_Seeker.pdf

Crock101:
Click this for more info:

http://www.usfirst.org/sites/default/files/uploadedFiles/Robotics_Programs/FTC/FTC_Documents/Using_the_IR_Seeker.pdf

it is of no use to me.
just glancing at it, it seems like code specific to output on the NXT brick - don't expect to use that code on the Arduino.

Crock101:
What i want to know is do I need any special code to interpret the data coming from this sensor, and if so what is it? What I want is the directional data to output a number between 1 and 9 representing each of the sectors.

i guess the answer is 'Yes' - you will need actual Arduino code.

have you tried writing any yet ?

what have you already done with the Arduino ?

AGAIN;

have you already tried connecting all these and just trying to read the raw data FROM the sensor and display it to the Serial Monitor in Arduino ?

I've done a bit of programing with the arduino but nothing too advanced, if I could have the arduino tell me what values the sensor is outputting while it's happening, that would go a long way twords helping me complete this project. What I have so far is the commands telling the motors what to do, i just need the data from the sensors to active them. I'll try some test runs without the interpretation code and see what happens. In the mean time don't be afraid to post anything you might find helpful on this topic.

Never mind I found this website that says the sensor already outputs a value from 0 to 9(for each sector). Now I just need to know if the arduino will be able to use this code to pick up on only the Ir beacon and not just any source of Ir light.

void initializeRobot()
{
HTIRS2setDSPMode(irSeeker, DSP_1200);// FTC beacons are 1200Hz emitters
return;
}

I'll test it myself, but if anyone knows how or if this needs to be adapted please post a reply.

Here is the website:

Crock101:
I've done a bit of programing with the arduino but nothing too advanced,

nothing wrong with that

Crock101:
if I could have the arduino tell me what values the sensor is outputting while it's happening, that would go a long way twords helping me complete this project.

some people would give you the code served on a platter and you can be on your way in the project immediately, but i would prefer to give you the tools and show you how to do it yourself.

Crock101:
What I have so far is the commands telling the motors what to do, i just need the data from the sensors to active them.

okay, telling motors what to do is simple enough, what you need to learn about is Serial communication - ie. getting the data from a sensor connected to the Arduino. (in your case, the sensor is sending the data to the Arduino via I2C (which is an even more advanced version of Serial communication - so let's take it slowly and learn the basic Serial communication first)
have a look at this page; Serial - Arduino Reference
if that seems a bit much to learn, try this tutorial from SparkFun;
Serial Communication - SparkFun Learn

Crock101:
I'll try some test runs without the interpretation code and see what happens. In the mean time don't be afraid to post anything you might find helpful on this topic.

i think it's you that shouldn't be afraid to try something.

Crock101:
Never mind I found this website that says the sensor already outputs a value from 0 to 9(for each sector).

"already outputs a value from 0 to 9" - but to where ? using which code ?
i am guessing that website is informative for using the NXT brick - you are using the Arduino now, those resources might not be particularly helpful.

Crock101:
Now I just need to know if the arduino will be able to use this code to pick up on only the Ir beacon and not just any source of Ir light.

void initializeRobot()
{
HTIRS2setDSPMode(irSeeker, DSP_1200);// FTC beacons are 1200Hz emitters
return;
}

chances are it won't - that's a code snippet - if you don't already have the definition of HTIRS2setDSPMode() there is NO WAY the Arduino is going to understand what to do.

Crock101:
I'll test it myself, but if anyone knows how or if this needs to be adapted please post a reply.

Here is the website:
Forums - FIRST Forums

i don't think that website is of any use to you, it looks like it's for some totally different platform to the Arduino - i'm a newbie myself and i don't know what all this #pragma stuff is about.

you need to learn to use the Arduino using the Arduino environment, or the "IDE" - the basic starting page is;

what other sensors do you have that you could start experimenting with ?
if you want to get a quick jump to see how Serial communication works, you can always use the Serial Monitor which is available in the Arduino IDE (icon in the top-right corner); use the code here; Serial.read() - Arduino Reference

Will the Library i found earlier help me with this?

Crock101:
Will the Library i found earlier help me with this?

Help you with 'what' ?
That library 'does it all' for you !
You're looking for help WITH the library - that is pretty advanced, even beyond my level.
You're choosing quite a complicated project to start with if you don't know the basics of Serial & I2C communications.

If you take the time to learn those, you should be able to interpret that sensor WITHOUT the complicated library.

So based on the info you sent me the first thing i need to do is use this code:

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
to find out what bytes of data are being sent to the arduino from the sensor. I can then assign values to those bytes of data, and then use those values in the commands for my motors to make the robot follow me around. Correct?

yes, correct - now we move forward !
don't forget to use the CODE tags (#) when you post code.

if you open your Serial Monitor - you can actually send those bytes manually via your keyboard, that way you can test your code to run the motors already - let's say type 'w' moves the robot forward, 's'= backwards, 'a' & 'd' = left & right respectively.

after you've got that code running smoothly and able to command your robot the way you want and it moves as you expect, then we insert code to ask the sensor to provide "those bytes" automatically.

in other words, in the above code - instead of just :

Serial.println(incomingByte, DEC);

you can add;

if (incomingByte == 'w')
{
   << insert your code to move the robot forwards here >>
}

you could leave the Serial.println() in there just to confirm what keys are being pressed.

What do you think of my code so far?

int incomingByte = 0;
int LeftF= 1;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
const int pingPin = 7;
void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
        if (Serial.available() > 0) {
                incomingByte = Serial.read();

                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
{
  long duration, inches, cm;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}

long microsecondsToInches(long microseconds);
{
  return microseconds / 74 / 2;
}

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 travelled.
  return microseconds / 29 / 2;
}
{
If (incomingByte = 97) Then;
digitalWrite(RightF,HIGH);
digitalWrite(LeftB,HIGH);
EndiIf;

If (incomingByte = 100) Then;
digitalWrite(LeftF,HIGH);
digitalWrite(RightB,HIGH);
EndIf;

If (incomingByte = 119) Then;
digitalWrite(LeftF,HIGH);
digitalWrite(RightF,HIGH);
EndIf;

If (incomingByte = 0) Then;
digitalWrite(LeftB,HIGH);
digitalWrite(RightF,HIGH);
EndIf;

If (incomingByte = 115) Then;
digitalWrite(LeftB,HIGH);
digitalWrite(RightB,HIGH);
EndIf;
}
}

For some reason it dosn't like this line of code when i try to upload it.

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

it says that microsecondsToInches was not declared in the scope and I don't know how it wants me to declare it.

I got the code from here:

Crock101:
What do you think of my code so far?
...
...

not too bad i guess, but take a look at your If...Then section, it shouldn't be down there, and the syntax isn't quite correct, have a look at http://arduino.cc/en/Reference/If for how Arduino uses the If...Then conditional statement

Crock101:
For some reason it dosn't like this line of code when i try to upload it.

  inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);




it says that **microsecondsToInches** was not declared in the scope and I don't know how it wants me to declare it.

i'm not sure about that one, i'll try it out and get back to you.

Crock101:
I got the code from here:

http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

yes, well, that's for an entirely different sensor altogether, if it's not connected to the Arduino and you're asking it to read a non-existent sensor - it makes no sense.

step back a bit and just control the robot with the keyboard via your Serial Monitor - at least you seem to be on the right track there, but DO CHANGE the ;

If (incomingByte = 100)

to

if (incomingByte == 100)

there is a big difference in meaning; the first is assigning values, the second is comparing values - read here for the details;

vs.
http://arduino.cc/en/Reference/If
if there's anything in there you don't understand - ASK !

Sorry I'm used to programing with visual basic, is this better?

int incomingByte = 0;
int LeftF= 1;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
const int pingPin = 7;
void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
        if (Serial.available() > 0) {
                incomingByte = Serial.read();

                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
{
if (incomingByte == 97){
digitalWrite(RightF,HIGH);
digitalWrite(LeftB,HIGH);
}

if (incomingByte == 100){
digitalWrite(LeftF,HIGH);
digitalWrite(RightB,HIGH);
}

if (incomingByte == 119){
digitalWrite(LeftF,HIGH);
digitalWrite(RightF,HIGH);
}

if (incomingByte == 0){
digitalWrite(LeftB,HIGH);
digitalWrite(RightF,HIGH);
}

if (incomingByte == 115){
digitalWrite(LeftB,HIGH);
digitalWrite(RightB,HIGH);
}
}
}

Also for future reference do you know what variable that ultrasonic sensor code was using to output it's value for distance in inches? I'm unsure if I should use the pingPin variable or not.

Crock101:
Sorry I'm used to programing with visual basic, is this better?

yes, it is - but look at where your loop ends; compare it to this Serial.available() - Arduino Reference

have you tried running the code ?
how does your robot behave ?

Crock101:
Also for future reference do you know what variable that ultrasonic sensor code was using to output it's value for distance in inches? I'm unsure if I should use the pingPin variable or not.

that pingPin variable is simply specifying the Arduino pin that it is connected to.
there's no point using it if you don't have the sensor itself.
the code for using that sensor is a whole new ballgame and you've only copied snippets of it which won't do you any good.

EDIT:
use the Auto Format feature ( Ctrl-T ) to tidy up your code and help you see it better.

retronet_RIMBA1ZO:
...
...

Crock101:
For some reason it dosn't like this line of code when i try to upload it.

  inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);




it says that **microsecondsToInches** was not declared in the scope and I don't know how it wants me to declare it.

i'm not sure about that one, i'll try it out and get back to you.

okay, i finally figured this one out ! :smiley:

the error was in the declaration;

long microsecondsToInches(long microseconds);
{
  return microseconds / 74 / 2;
}

the first semi-colon doesn't belong there !!
(good "refresher course" for me ! :smiley: )

Ok here is my latest code:

int incomingByte = 0;
int LeftF= 1;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
}

void loop() {
        if (Serial.available() > 0) {
                incomingByte = Serial.read();

                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
  {
    if (incomingByte == 97){
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
    }
  }
  {
    if (incomingByte == 100){
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
    }
  }
  {
    if (incomingByte == 119){
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
    }
  }
  {
    if (incomingByte == 0){
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
    }
  }
  {
    if (incomingByte == 115){
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
    }
  }
}

The problem is that the LeftF pin won't go low even though it does when I use this code:

int LeftF= 1;
int RightF= 2;
int LeftB= 3;
int RightB= 4;

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
}

void loop() {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
    }

Any suggestions?

Crock101:
...
...The problem is that the LeftF pin won't go low even though it does when I use this code:

int LeftF= 1;

int RightF= 2;
int LeftB= 3;
int RightB= 4;

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
}

void loop() {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
    }



Any suggestions?

when won't it go low ?
try adding comments to each line with the ASCII codes - like // press 'w' - move forward, etc.
also there seem to be quite a few unnecessary pairs of curly brackets;

  {
    if (incomingByte == 97){
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
    }
  }

you don't really need the first and last ones there, the if statement doesn't need to be enclosed in them.

and did you use the Ctrl-F to auto-format ?
do that and it should look closer to this;

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();

    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }

  if (incomingByte == 97){
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }
}

see where they're lined up ?

now look at the loop() code in its simple(pseudo) form;

void loop() {
// check #1
  if ([i]Serial is available[/i]) {
[i]    read the keypress and store to variable called [b]incomingByte [/b][/i]
  }

// check #2
  if ([i][b]incomingByte [/b] has a value[/i]){
    [i]write HIGH/LOW to these pins[/i]
  }
}

check #1 and #2 are mutually exclusive, meaning, even if check #1 was NOT true, ie. NO keypressed, check #2 would STILL be performed, because the variable incomingByte DOES have a value (from the previous loop).