-
Notifications
You must be signed in to change notification settings - Fork 0
tutorial IR line sensor
How can objects be real if your sensors aren't real?
This tutorial is one of two that covers the following sensors:
- IR line sensor
- IR distance sensor
This sensor needs the following header files to access the relevant functions.
#include "RASLib/inc/common.h"
#include "RASLib/inc/adc.h"
After you include this header in file, let's create two functions, initGPIOLineSensor and gpioLineSensorDemo. We can use the initializer and your own method to grab the line sensor data for your final robot.
For initGPIOLineSensor, declare a new tADC and a tBoolean for initialization outside of it, and initialize the tADCinside of it.
static tADC *adc[4];
static tBoolean initialized = false;
void initGPIOLineSensor(void){
if(initialized){
return;
}
initialized = true;
//set four pins for ADC input - you can use these 4 I/O pins or select your own
adc[0] = InitializeADC(PIN_D0);
adc[1] = InitializeADC(PIN_D1);
adc[2] = InitializeADC(PIN_D2);
adc[3] = InitializeADC(PIN_D3);
}
You can find a full list of pins to use at the GPIO pins page.
gpioLineSensorDemo should loop until a key is pressed, and on every loop it reads from the line sensor and output it on the console.
void gpioLineSensorDemo(void){
Printf("Press any key to quit\n");
//loop until key is pressed
while(!KeyWasPressed()){
To read from the line sensor, this demo uses ADCRead();. This function requires one parameter: a reference to the pin initialized from InitializeADC. This is the address stored in the adc[].
This translates to:
Printf(
"Line Sensor values: %1.3f, %1.3f, %1.3f, %1.3f\r",
ADCRead(adc[0]),
ADCRead(adc[1]),
ADCRead(adc[2]),
ADCRead(adc[3])
);
}
Printf("\n");
}
Now you should be able to call both these methods in your main and run it on your robot! Here's the full code for the IR Line Sensor:
#include "RASLib/inc/common.h"
#include "RASLib/inc/linesensor.h"
static tADC *adc[4];
static tBoolean initialized = false;
void initGPIOLineSensor(void){
if(initialized){
return;
}
initialized = true;
//set four pins for ADC input - you can use these 4 I/O pins or select your own
adc[0] = InitializeADC(PIN_D0);
adc[1] = InitializeADC(PIN_D1);
adc[2] = InitializeADC(PIN_D2);
adc[3] = InitializeADC(PIN_D3);
}
void gpioLineSensorDemo(void){
Printf("Press any key to quit\n");
//loop until key is pressed
while(!KeyWasPressed()){
Printf(
"Line Sensor values: %1.3f, %1.3f, %1.3f, %1.3f\r",
ADCRead(adc[0]),
ADCRead(adc[1]),
ADCRead(adc[2]),
ADCRead(adc[3])
);
}
Printf("\n");
}
For further reading, visit the adc.h wiki page.