// RCONTROL
// Version 1.00
// March 2012
//
//This program hooks a variable reistor to the AO input. Adjusting the pot will change the value returned by the AD converter. The results are shown 3 ways:
// RAW: The value of the last A/D conversion. This will probably jump around a bit.
// Trunc: Increased stability by truncating the lower bits of the reading
// Ave: A number of readings avereaged together to increas stability.
//
// The results are shown on a Unified Microsystems ATS-1 Terminal Shield. www.unifiedmicro.com
///RCONTROL Program by Gary C. Sutcliffe is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
//Updates:
int analogPin = 0; //The wiper is connected to Analog pin 0
#define CLS 0x01 //Character to send to the ATS-1 to clear the LCD and home the cursor on the LCD display
// experiment with the following values to see what the results are
#define AVE_SAMPLES 4 //number of samples to collect to for the average
#define TRUNC_VAL 4 //divisor for truncating lower bits. 2 will truncate LSB, 4 lowest 2 bits, 8 lowest 3 bits
void setup()
{
Serial.begin(4800); // opens serial port, sets data rate to 4800 baud to display
}
void loop()
{
int rawAD; //Raw A/D reading for display
int aveAD; // Averaged A/D reading
int truncAD; //AD reading with
int count; // used for keeping track of number of samples taken
while(1)
{
aveAD = 0; //Clear the avereaged value
for(count = 0; count < AVE_SAMPLES; count++) //loop to collect a number of samples for the average
{
rawAD = analogRead(analogPin); // read the input pin
aveAD = aveAD + rawAD; //add up all the readings in aveAD
}
aveAD = aveAD/AVE_SAMPLES; //aveAD now has the average of the samples taken
truncAD = rawAD/TRUNC_VAL;
Serial.write(CLS); //clears the LCD display and puts cursor in upper left corner
Serial.print("R: "); //start printing out the different results R: is the raw value
Serial.print(rawAD);
Serial.print(" A: "); //A is the averaged value
Serial.println(aveAD);
Serial.print("Truncated: ");
Serial.print(truncAD);
delay(2000); //put a little delay before we start a new set of readings
} //End of while() statement
} // End of loop()