1#include <OrangutanLEDs.h>
2#include <OrangutanAnalog.h>
3
4/*
5 * OrangutanAnalogExample for the 3pi, Orangutan SV-xx8,
6 *    Orangutan LV-168, or Baby Orangutan B
7 *
8 * This sketch uses the OrangutanAnalog library to read the voltage output
9 * of the trimpot in the background while the rest of the main loop executes.
10 * The LED is flashed so that its brightness appears proportional to the
11 * trimpot position.
12 *
13 * http://www.pololu.com/docs/0J17/5.a
14 * http://www.pololu.com
15 * http://forum.pololu.com
16 */
17
18OrangutanLEDs leds;
19OrangutanAnalog analog;
20
21unsigned int sum;
22unsigned int avg;
23unsigned char samples;
24
25void setup()                      // run once, when the sketch starts
26{
27  analog.setMode(MODE_8_BIT);    // 8-bit analog-to-digital conversions
28  sum = 0;
29  samples = 0;
30  avg = 0;
31  analog.startConversion(TRIMPOT);  // start initial conversion
32}
33
34void loop()                       // run over and over again
35{
36  if (!analog.isConverting())     // if conversion is done...
37  {
38    sum += analog.conversionResult();  // get result
39    analog.startConversion(TRIMPOT);          // and start next conversion
40    if (++samples == 20)
41    {
42      avg = sum / 20;             // compute 20-sample average of ADC result
43      samples = 0;
44      sum = 0;
45    }
46  }
47
48  // when avg == 0, the red LED is almost totally off
49  // when avg == 255, the red LED is almost totally on
50  // brightness should scale approximately linearly in between
51  leds.red(LOW);                  // red LED off
52  delayMicroseconds(256 - avg);
53  leds.red(HIGH);                 // red LED on
54  delayMicroseconds(avg + 1);
55}
56