Orangutan X2 and Sharp IR sensors

Hi! It’s me again and I have another newbie-ish question.
I’m using OX2 and 1 Sharp IR sensor ( exactly this one: robotshop.ca/sharp-gp2d12-ir … -80-2.html ).

The project is to make a robot, to participate in a sumo robot contest on September 2009. Since the field, where the robots are “fighting” is only a meter wide(around 40 inches), and sensor covers almost everything, the robot can make simple actions depending on the sensor output values

Depending on the distance between the sensor and the object, rangefinder gives a voltage between 2,4V @ 10cm distance(it’s around 4 inches) and 0.4V @ 80cm distance( around 35 inches).

So a pretty simplified code would look like this:

#include "SPI.h"
#include <avr/io.h>

int main ()
{
//first we have to define ports and then we can perform a simple ADC conversation and this is where 
//the problem starts
//after performing a simple conversation I have to read the value of the sensor and then act uponing that.

if (the_sensor_value) < 1V
{
SPIInit();
setMotor1( 130 );
setMotor2( -130 );// Robot rotates and searches for the opponent
}
else

{
setMotor1 ( 255 ); //attack the opponent with full speed
SetMotor2 ( 255 );
}

while (1);
return 0;
}

So my question is about the syntax for the conversation between the sensor and the micro.
How could I define sensor output value as variable?

I read throught the book kit: C Programming for Microcontrollers and I was wandering throught this forum and found both of these topics:


I actually didn’t understand how to perform after the ADC conversation is made and how to use the values read.

I hope I don’t bother you too often with silly questons.

Hello.

Did you get an LCD with your Orangutan X2? If so, construting and debugging your program will be much easier.

The second forum thread you linked to seemed particularly appropriate, so I suggest you read through it a second time and ask any specific questions that remain.

As a first step, I suggest you create a program that simply displays the results of ADC conversions on the analog input to which your sensor is connected. If you have an LCD, you can write them to the LCD a few times per second. Otherwise, you could use the X2’s serial SPI functions to send the conversion results to a computer and monitor them with a terminal program. This will give you feeling of what values the sensor returns for various conditions. See what happens when there is object 40 inches away, when there is an object 20 inches away, when there is no object, etc. Once you have characterized the feedback you are getting from your sensor, you can construct a program that responds appropriately to the sensor output. For example, let’s assume you have a function to read the output voltage of the sensor (you can write this function using code from other threads on this forum):

SPIInit();

while (1)
{
  unsigned int sensorValue = analogRead(0);  // read the voltage on pin PA0

  if (sensorValue > 100)  // if we see something
  {
    setMotor1( 255 );  // attack the opponent
    setMotor2( 255 );
  }
  else
  {
    setMotor1( 130 );
    setMotor2( -130 );// Robot rotates and searches for the opponent
  }
}

In the above example, I am using 100 as the threshold for deciding that the sensor is seeing something. You should pick a better value based on testing that lets you characterize the sensor response. Does this make sense?

- Ben