Society of Robots - Robot Forum
Software => Software => Topic started by: Trumpkin on August 22, 2008, 07:09:21 PM
-
Hey people, when I try to compile this code I get this error "undefined reference to analogRead". Is there a library or something I'm supposed to include?
/* Blinker Demo */
/* Include useful pre-defined functions*/
#include <avr/interrupt.h> // Defines pins, ports, etc to make programs easier to read
#define F_CPU 100000UL // Sets up the default speed for delay.h
#include <util/delay.h>
int IR_pin = 5;
int val = 0; //a variable to store IR value
int IR_thresh = 100;
int main()
{
for ( ;; )
{
val = analogRead(IR_pin);
if (val < IR_thresh)
{
DDRD = _BV(PD7); // enable output on port D, pin 7
PORTD = _BV(PD7);
_delay_ms(1000);
PORTD &= ~_BV(PD7);
_delay_ms(1000);
}
}
return(0);
}
-
Where did this line come from?
val = analogRead(IR_pin);
Anyway, the error is because you need to declare what analogRead() is before you use it.
For example:
int IR_thresh = 100;
void analogRead(int pin);//declare here, then define it somewhere else
int main()
{
-
you can either just declare, or declare and initialize.
<data_type> <variable>;
or
<data_type> <variable> = <number to initialize with>
ex:
int val;
or
int val = 0;
-
I got the code to compile and download on to my microcontroller but the LED just blinked randomly. :-\ I wanted the LED to blink when my hand got close to the Sharp IR. I changed the code a bit, heres what I want it to do
auto calibrate
blink the LED to let you know that it has calibrated
when an object gets within the calibrated range have the LED blink
I probably have a lot of things wrong since I'm not very good at programming yet.
/* Blinker Demo */
/* Include useful pre-defined functions*/
#include <avr/interrupt.h> // Defines pins, ports, etc to make programs easier to read
#define F_CPU 100000UL // Sets up the default speed for delay.h
#include <util/delay.h>
#include <SoR_Utils.h>
int IR_pin = 5;
int val = 0;
int main()
{
//automatically calculates threshold value before run
int IR_thresh=a2dConvert8bit(IR_pin);//sensor reading
PORT_ON(PORTD, 7);
_delay_ms(5000);
PORT_OFF(PORTD, 7);
_delay_ms(5000);
for ( ;; ) //loops forever
{
val = a2dConvert8bit(IR_pin);
if (val < IR_thresh)
{
PORT_ON(PORTD, 7);
_delay_ms(1000);
PORT_OFF(PORTD, 7);
_delay_ms(1000);
}
}
return(0);
}
-
Heres the pseudo code
Read value of Sharp and store in variable "distance"
If distance is more than threshold Then // I wrote more because the Sharp IR returns a higher value the closer you are
Flash_LED
Else
Turn_LED_Off
Endif
Loop
The actual C code is pretty simple , the most complicated part I could think of is getting the values of the Sharp IR sensor and see the values for close objects and the values for further. If I remember correctly the closer u are the higher the value - but I am not sure , the last time I used a Sharp IR sensor was in that electronics book combination lock.