go_away

Author Topic: IR code/sensor not working as expected - modified from $50 code  (Read 2498 times)

0 Members and 1 Guest are viewing this topic.

Offline ozbotTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 1
I have built the IR Emitter/Detector basic sensors as in the sensor tut, using the default values given. Two sensors, left & right.

As I understand, the sensor should have zero voltage to input pins when the detector is not active (ie, not near object).  So I use the code below, and my right wheel does turn backwards, and keeps going, regardless of the sensor being open or obstructed.  Any value I give to threshold results in the same behaviour.  If I swap >threshold for <threshold it just ignores the IR section and photovores fine. Am I missing something obvious?

(I have been over the electronics, couldn't find any shorts....)

Code: [Select]

//SoR Include
#include "SoR_Utils.h" //includes all the technical stuff


int main(void)
{
//declare variables here
//int i=250;//a 'whatever' variable
int sensor_left=0;//left photoresistor
int sensor_right=0;//right photoresistor
int ir_left=0;//left ir sensor
int ir_right=0;//right ir sensor
int threshold=200;//the larger this number, the more likely your robot will drive straight
int threshold2=8;


/****************INITIALIZATIONS*******************/
//other stuff Im experimenting with for SoR
//uartInit();  // initialize the UART (serial port)
//uartSetBaudRate(9600);// set the baud rate of the UART for our debug/reporting output
//rprintfInit(uartSendByte);// initialize rprintf system

//timerInit(); // initialize the timer system

configure_ports(); // configure which ports are analog, digital, etc.
a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

//rprintf("Initialization Complete\r\n");
/**************************************************/


/*********ADD YOUR CODE BELOW THIS LINE **********/
LED_off();//turn LED on


while(1)
{
//store sensor data
sensor_left=a2dConvert8bit(5);
sensor_right=a2dConvert8bit(4);
ir_left=a2dConvert8bit(3);
ir_right=a2dConvert8bit(2);

if (ir_left>threshold)//turn slowly back-right
{
servo_right(27);
servo_left(35);
}
else if (ir_right>threshold)//turn slowly back-left
{
servo_left(42);
servo_right(35);
}
else
{
//detects more light on left side of robot
if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold2)
{
//go left
servo_left(44);
servo_right(44);
}

//detects more light on right side of robot
else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold2)
{
//go right
servo_left(25);
servo_right(25);
}

//light is about equal on both sides
else
{
if(sensor_left>threshold2 && sensor_right>threshold2)
{
//go straight
servo_left(25);
servo_right(44);
}
else     //stay put
{
servo_left(35);
servo_right(35);
}

}

}
/* Servo Test Code
i=250;
while(i>0)
{
servo_left(40);
i--;
}

i=250;
while(i>0)
{
servo_left(24);
i--;
}
*/

//rprintf("Initialization Complete\r\n");

//output message to serial (use hyperterminal)
//print("Hello, World! Read My Analog: %u\r\n", sensor_0);

delay_cycles(500);//a small delay to prevent crazy oscillations
}
/*********ADD YOUR CODE ABOVE THIS LINE **********/

return 0;
}

I was so disappointed to learn that half the people I know are under average........but the other half more than make up for it.

Offline offy

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: IR code/sensor not working as expected - modified from $50 code
« Reply #1 on: March 19, 2009, 05:56:28 PM »
This is my code for range finder. You can modify it as needed. It might help you.
(In C coded for the Arduino platform)

Code: [Select]
#include <Servo.h>
Servo left;
Servo right;
Servo scanner;
int posl = 0;
int posr = 0;
int pos = 0;
int poss = 0;
int value = 0;
int treshold = 340;
void setup()
{
  left.attach(9);
  right.attach(10);   
  scanner.attach(11);
}
void loop() {
  ReadSharpSensor();
}
void ReadSharpSensor () {
  value = analogRead(0);
  if (value > treshold) {
    back();
  }
  else
  {
    move();
  }
}
void back () {
  left.write(135);
  right.write(135);
  if(value < treshold)
  {
  left.write(135);
  right.write(135);
  delay(100);
    move();
  }
}
void move () {
  right.write(45);
  left.write(135);
}


Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,659
  • Helpful? 169
    • Society of Robots
Re: IR code/sensor not working as expected - modified from $50 code
« Reply #2 on: April 07, 2009, 10:53:23 PM »
ozbot did you ever figure this out?

Offline ozbotTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 1
Re: IR code/sensor not working as expected - modified from $50 code
« Reply #3 on: July 23, 2009, 08:39:35 AM »
no, not yet.  been sidetracked on other projects.  I realised that the processor was not sitting in the socket properly, (cannot seem to get the right size socket in jaycar, Atmel Mega8), so I am now upgrading the construction & printing my first PCB :)
will have a new MB and sensor boards soon & see if that helps.
« Last Edit: July 23, 2009, 08:41:44 AM by ozbot »
I was so disappointed to learn that half the people I know are under average........but the other half more than make up for it.

Offline ozbotTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 1
Solved: IR code/sensor not working as expected - modified from $50 code
« Reply #4 on: December 05, 2009, 03:57:55 AM »
Solved it.....

Have not got a better pcb yet, havin trouble with paper types.

So I decided to make new sensors using the basic circuit from the sensors page.  Did not work.  I hooked it up to 3.3 V and tested it with a multimeter, used a camera to see IR.  IR emitting : yes.  Volt change : 3.3-3.1 when sensor obstructed.  I measure the resistance of reciever.  It be max at 260 ohms!!  So I replace R2, 11k with 470k and now measures from 2.0 - 0.7 V.  Tested with the code, had  to change the > (greater than threshold) to < (less than threshold) and now works as expected.  Still gotta tweak it, but it works!
I was so disappointed to learn that half the people I know are under average........but the other half more than make up for it.

 


Get Your Ad Here