Wednesday, February 20, 2008

Code for Reading From a Range Sensor


Here is code (a program called irsensor.c) to read data from an IR range sensor. (Sharp GP20YA02
The sensor is hooked up to pin #40 on the microprocessor.
 
-----------------------

//

// irsensor.c

//

// Simple program to read data from a Sharp 2YOA02 IR distance sensor and send it to the computer.

// Adapted from a2dtest.c from Pascal Stang.

//

// rtwomey@ucsd.edu

//

 

// includes

#include <avr/io.h> // include I/O definitions (port names, pin names, etc)

#include <avr/interrupt.h> // include interrupt support


#include "global.h" // include our global settings

#include "uart.h" // include uart function library

#include "rprintf.h" // include printf function library

#include "timer.h" // include timer function library (timing, PWM, etc)

#include "a2d.h" // include A/D converter function library

#include "vt100.h" // include VT100 terminal support


// constants


// function declarations


// program

int main(void)

{

int readVal=0;


// initialize the UART (serial port)

uartInit();

uartSetBaudRate(57600);

// make all rprintf statements use uart for output

rprintfInit(uartSendByte);

// initialize the timer system

timerInit();

// turn on and initialize A/D converter

a2dInit();


// configure a2d port (PORTA) as input

// so we can receive analog signals

DDRA = 0x00;

// make sure pull-up resistors are turned off

PORTA = 0x00;


// set the a2d prescaler (clock division ratio)

// - a lower prescale setting will make the a2d converter go faster

// - a higher setting will make it go slower but the measurements

//   will be more accurate

// - other allowed prescale values can be found in a2d.h

a2dSetPrescaler(ADC_PRESCALE_DIV32);


// set the a2d reference

// - the reference is the voltage against which a2d measurements are made

// - other allowed reference values can be found in a2d.h

a2dSetReference(ADC_REFERENCE_AVCC);


// use a2dConvert8bit(channel#) to get an 8bit a2d reading

// use a2dConvert10bit(channel#) to get a 10bit a2d reading


// loop, and transmit the A/D value every 10 msecs.

while(1)

{

                readVal=a2dConvert8bit(0);

rprintf("%dn", readVal);

//uartSendByte(readVal);

// wait a 0.1s before reading next value

timerPause(100);

}


return 0;

}



-----------------------

Here is a program for taking the readings from the processor, and rendering them as color on the screen. The program is written in processing, which you can download here.

To get this to program to work, you first need to change the irsensor.c program to switch the program from sending data as text to the terminal window, and instead to send it as data to be read by processing.  Change it as follows:

  1. uncomment uartSendByte(readVal) above (remove the "//" at the beginning of that line)
  2. comment out rprintf("%dn", readVal); (add a "//" at the beginning of that line)
  3. recompile the program.  (make, from the terminal, or [AvrLib] Build All in Programmer's Notepad)

And then run this program in processing, AtoD:

/*

Simple program to demonstrate a2d functionality and IR rangefinding.
A Sharp 2YOA02 distance sensor is connected to an ADC pin on the microprocessor,
and distance data is fed through the serial port to this program. 

*/

import processing.serial.*;

Serial port;  // Create object from Serial class
int val;      // Data received from the serial port

float MULTIPLIER = 1.6;  // Scales readings to full color range.

void setup() 
{
  size(600, 600);

  // Open the port that the board is connected to and use the same speed (57600 bps)
  port = new Serial(this, Serial.list()[0],57600);
  
}

void draw()
{

  if (0 < port.available()) {  // If data is available,
    val = port.read();         // read it and store it in val
  }
 
  background(val*MULTIPLIER, 0, 0);  // color the window proportional to distance reading.
  
}

No comments: