Documents » Pololu AVR C/C++ Library User's Guide » 6. Example programs »6.c. Orangutan Analog Input FunctionsOverviewThis section of the library provides a set of methods that can be used to read analog voltage inputs, as well as functions specifically designed to read the value of the trimmer potentiometer (on the 3pi Robot, Orangutan LV-168, and Baby Orangutan B), the value of the temperature sensor in tenths of a degree F or C (on the Orangutan LV-168 only), and the battery voltage (3pi Robot only). C++ users: See Section 5.a of Programming Orangutans from the Arduino Environment for examples of this class in the Arduino environment, which is almost identical to C++. Complete documentation of the functions can be found in Section 3 of the Pololu AVR Library Command Reference. Usage ExamplesThis library comes with two examples in 1. analog1Demonstrates how you can use the methods in this library to read the analog voltage of the trimmer potentiometer in the background while the rest of your code executes. If the ADC is free, the program starts a conversion on the TRIMPOT analog input (channel 7), and then it proceeds to excecute the rest of the code in loop() while the ADC hardware works. Polling of the analog_is_converting() method allows the program to determine when the conversion is complete and to update its notion of the trimpot value accordingly. Feedback is given via the red user LED, whose brightness is made to scale with the trimpot position. This example will work for both the Orangutan LV-168 and the Baby Orangutan B.
#include <pololu/orangutan.h>
/*
* analog1: for the Orangutan LV-168 or Baby Orangutan B
*
* This example uses the OrangutanAnalog functions to read the voltage
* output of the trimpot in the background while the rest of the main
* loop executes. The LED is flashed so that its brightness appears
* proportional to the trimpot position. This example will work on
* both the Orangutan LV-168 and Baby Orangutan B.
*
* http://www.pololu.com/docs/0J20/6.c
* http://www.pololu.com
* http://forum.pololu.com
*/
unsigned int sum;
unsigned int avg;
unsigned char samples;
int main()
{
set_analog_mode(MODE_8_BIT); // 8-bit analog-to-digital conversions
sum = 0;
samples = 0;
avg = 0;
start_analog_conversion(TRIMPOT); // start initial conversion
while(1)
{
if (!analog_is_converting()) // if conversion is done...
{
sum += analog_conversion_result(); // get result
start_analog_conversion(TRIMPOT); // and start next conversion
if (++samples == 20)
{
avg = sum / 20; // compute 20-sample average of ADC result
samples = 0;
sum = 0;
}
}
// when avg == 0, the red LED is almost totally off
// when avg == 255, the red LED is almost totally on
// brightness should scale approximately linearly in between
red_led(0); // red LED off
delay_us(256 - avg);
red_led(1); // red LED on
delay_us(avg+1);
}
}
2. analog2Intended for use only on the Orangutan LV-168. It displays on the LCD the trimmer potentiometer output in millivolts and the temperature sensor output in degrees Farenheit. If you hold a finger on the underside of the Orangutan LV-168’s PCB near the center of the board, you should see the temperature reading slowly start to rise. Be careful not to zap the board with electrostatic discharge if you try this!
#include <pololu/orangutan.h>
/*
* analog2: for the Orangutan LV-168
*
* This example uses the OrangutanAnalog functions to read the voltage
* output of the trimpot (in millivolts) and to read the Orangutan
* LV-168's temperature sensor in degrees Farenheit. These values are
* printed to the LCD 10 times per second. This example is intended
* for use with the Orangutan LV-168 only.
*
* You should see the trimpot voltage change as you turn it, and you can
* get the temperature reading to slowly increase by holding a finger on the
* underside of the Orangutan LV-168's PCB near the center of the board.
* Be careful not to zap the board with electrostatic discharge if you
* try this!
*
* http://www.pololu.com/docs/0J20/6.c
* http://www.pololu.com
* http://forum.pololu.com
*/
int main()
{
set_analog_mode(MODE_10_BIT); // 10-bit analog-to-digital conversions
while(1) // run over and over again
{
lcd_goto_xy(0,0); // LCD cursor to home position (upper-left)
print_long(to_millivolts(read_trimpot())); // trimpot output in mV
print(" mV "); // added spaces are to overwrite left over chars
lcd_goto_xy(0, 1); // LCD cursor to start of the second line
unsigned int temp = read_temperature_f(); // get temp in tenths of a degree F
print_long(temp/10); // get the whole number of degrees
print_character('.'); // print the decimal point
print_long(temp - (temp/10)*10); // print the tenths digit
print_character(223); // print a degree symbol
print("F "); // added spaces are to overwrite left over chars
delay_ms(100); // wait for 100 ms (otherwise LCD flickers too much)
}
}
|
|
Home
|
Contact
|
About
|
Forum
|
US toll free: 1-877-7-POLOLU |