go away spammer

Author Topic: analogRead  (Read 3949 times)

0 Members and 1 Guest are viewing this topic.

Offline TrumpkinTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,176
  • Helpful? 5
analogRead
« 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?
Code: [Select]
/* 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);
}
Robots are awesome!

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: analogRead
« Reply #1 on: August 22, 2008, 10:12:48 PM »
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:
Code: [Select]
int IR_thresh = 100;   

void analogRead(int pin);//declare here, then define it somewhere else

int main()
{

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: analogRead
« Reply #2 on: August 22, 2008, 11:07:35 PM »
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;

Offline TrumpkinTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,176
  • Helpful? 5
Re: analogRead
« Reply #3 on: August 27, 2008, 10:02:50 AM »
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.

Code: [Select]
/* 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);
}


« Last Edit: August 27, 2008, 10:04:51 AM by Trumpkin »
Robots are awesome!

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: analogRead
« Reply #4 on: August 27, 2008, 10:55:15 AM »
Heres the pseudo code

Code: [Select]
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.

Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

 


Get Your Ad Here

data_list