# 5.a. OrangutanAnalog - Analog Input Library

## Overview

This 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-xx8, Orangutan LV-168, and Baby Orangutan B), the battery voltage level in millivolts (3pi robot, SV-xx8), and the value of the temperature sensor in tenths of a degree F or C (on the Orangutan LV-168 only). This library gives you more control than existing Arduino analog input functions.

You do not need to initialize your OrangutanAnalog object before use. All initialization is performed automatically when needed.

All of the methods in this class are static; you should never have more than one instance of an OrangutanAnalog object in your sketch.

## OrangutanAnalog Methods

Complete documentation of this library’s methods can be found in Section 2 of the Pololu AVR Library Command Reference.

## Usage Examples

This library comes with two example sketches that you can load by going to File > Examples > OrangutanAnalog. The example sketches that come with the OrangutanMotors library also make limited use of this library.

### 1. OrangutanAnalogExample

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), and then it proceeds to execute the rest of the code in loop() while the ADC hardware works. Polling of the isConverting() 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.

#include <OrangutanLEDs.h>
#include <OrangutanAnalog.h>

/*
* OrangutanAnalogExample for the 3pi, Orangutan SV-xx8,
*    Orangutan LV-168, or Baby Orangutan B
*
* This sketch uses the OrangutanAnalog library 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.
*
* http://www.pololu.com/docs/0J17/5.a
* http://www.pololu.com
* http://forum.pololu.com
*/

OrangutanLEDs leds;
OrangutanAnalog analog;

unsigned int sum;
unsigned int avg;
unsigned char samples;

void setup()                      // run once, when the sketch starts
{
analog.setMode(MODE_8_BIT);    // 8-bit analog-to-digital conversions
sum = 0;
samples = 0;
avg = 0;
analog.startConversion(TRIMPOT);  // start initial conversion
}

void loop()                       // run over and over again
{
if (!analog.isConverting())     // if conversion is done...
{
sum += analog.conversionResult();  // get result
analog.startConversion(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
leds.red(LOW);                  // red LED off
delayMicroseconds(256 - avg);
leds.red(HIGH);                 // red LED on
delayMicroseconds(avg + 1);
} 

### 2. OrangutanAnalogExample2

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 Fahrenheit. 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 <OrangutanLCD.h>
#include <OrangutanAnalog.h>

/*
* OrangutanAnalogExample2: for the Orangutan LV-168
*
* This sketch uses the OrangutanAnalog library 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, though all but the temperature-measuring portion
* will work on the 3pi robot (on the 3pi, analog input 6 connects to 2/3rds
* of the battery voltage rather than a temperature sensor) and the
* Orangutan SV-xx8 (on the SV-xx8, analog input 6 connects to 1/3rd of
* the battery voltage).
*
* 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!
*/

OrangutanLCD lcd;
OrangutanAnalog analog;

void setup()                      // run once, when the sketch starts
{
analog.setMode(MODE_10_BIT);    // 10-bit analog-to-digital conversions
}

void loop()                       // run over and over again
{
lcd.gotoXY(0,0);                // LCD cursor to home position (upper-left)
lcd.print(analog.toMillivolts(analog.readTrimpot()));  // trimpot output in mV
lcd.print(" mV  ");             // added spaces are to overwrite left over chars

lcd.gotoXY(0, 1);               // LCD cursor to start of the second line

// get temperature in tenths of a degree F
unsigned int temp = analog.readTemperatureF();
lcd.print(temp/10);             // get the whole number of degrees
lcd.print('.');                 // print the decimal point
lcd.print(temp - (temp/10)*10); // print the tenths digit
lcd.print((char)223);           // print a degree symbol
lcd.print("F  ");               // added spaces are to overwrite left over chars

delay(100);                     // wait for 100 ms (reduces LCD flicker)
}
(702) 262-6648
Same-day shipping, worldwide
My account Comments or questions? About Pololu Contact Ordering information Distributors