Support » Pololu AVR C/C++ Library User’s Guide » 3. Functional Overview and Example programs »
3.a. Orangutan Analog Input Functions
Overview
This 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 SV, Orangutan LV-168, Orangutan SVP 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, SV-xx8, or SVP).
C++ users: See Section 5.a of Programming Orangutans and the 3pi Robot 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 2 of the Pololu AVR Library Command Reference.
Usage Examples
This library comes with two examples in libpololu-avr\examples
. The Orangutan Motors examples also make limited use of this section.
1. analog1
Demonstrates 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 on all devices except the SVP), and then it proceeds to execute 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.
On the Orangutan SVP, this example code will work, but it is not the recommended way of reading the trimpot. The trimpot reading and averaging is done on the auxiliary processor, so a simple avg=analog_read(TRIMPOT);
is sufficient to get the value of the trimpot and will not burden the CPU significantly. You can, however, change the channel number in the code below from TRIMPOT to a channel number from 0 to 7 in order to measure one of the eight analog ports on the AVR.
#include <pololu/orangutan.h> /* * analog1: for the Orangutan LV/SV-xx8 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/SV-xx8 and Baby Orangutan B. * * http://www.pololu.com/docs/0J20 * 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); // start next conversion if (++samples == 20) // if 20 samples have been taken... { 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. analog2
Intended for use on the Orangutan LV-168. Note that it will run on the 3pi robot and Orangutan SV-xx8, but the displayed temperature will be incorrect as the analog input connected to the temperature sensor on the Orangutan LV-168 is connected to 2/3rds of the battery voltage on the 3pi and to 1/3rd of the battery voltage on the Orangutan SV-xx8. 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/SV-xx8 * * 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/SV-xx8 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/SV-xx8'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 * 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) } }