Society of Robots - Robot Forum
Electronics => Electronics => Topic started by: brian on March 12, 2008, 06:08:16 PM
-
Hello, so today I was experimenting with the sharp IR range finder and I have some problems. Right now I have it set so that when the sensor reading is not 0 it turns on an LED. The problem is that the LED stays off all the time, however if I spend a minute or two positioning it in front of a mirror I can get the LED to turn on.
Any ideas?
-
Which Sharp IR sensor? Which micro controller and which pins are you using? Can you show us the code?
-
Output the data to hyperterminal.
Also, try having the LED turn on if it receives values from say 0 to 5 and see if it then works.
-
The Sharp IR I am using is this one http://www.sparkfun.com/commerce/product_info.php?products_id=242
Microcontroller is an Atmega8 and I am using pin PC5... then here is my code.
#include "a2d.h"
#include <avr/interrupt.h> // Defines pins, ports, etc to make programs easier to read
#define F_CPU 1000000 // Sets up the default speed for delay.h
#include <util/delay.h>
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)
int main(){
DDRC = 0x00; //configure all C ports for input
PORTC = 0x00; //make sure pull-up resistors are turned off
DDRD = 0xFF; //configure all D ports for output
DDRB = 0xC7; //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')
a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage
//int tim=0;
//int loc=1;
int IRSensor=0;
int IRSensorThresh=0;
IRSensorThresh=a2dConvert8bit(5);
_delay_ms(2000);
while(1){
IRSensor = a2dConvert8bit(5);
if(IRSensor <= 5 && IRSensor >=0){
PORT_ON(PORTD, 4);
}else{
PORT_OFF(PORTD, 4);
}
}
return(0);
}
This code is currently taking into account Admins advice, but unfortunately now the LED stays on no matter what. I would output the data to hyperterminal but I still have to make the modifications to my board (which I guess I should do).
-
Use a multimeter to measure the signal wire. See if the voltage changes as expected. I just want to rule out electrical issues.
And . . . I've been known to make this same dumb mistake myself but . . . are you sure its plugged into the right port?
-
Ok, so I checked the readings with a multimeter, it seems to work... if I change the distance of the object its pointing at I get a votage change. So it looks like either my code, or my atmega8.
EDIT: Solved the problem. The issue was with my code, in global.h the F_CPU was defined for a 3.6mhrtz processor then I later redefined it as 1mhrtz.