ANALOG INPUTS
The UNO has six analog inputs designated A0 t0 A5. These go from 0 to 1025 counts or 4.9 mV per count. A resistive voltage divider allows scaling to higher voltages. Use 3.3V as the nominal target voltage for the divider for approximately 675 counts. This will allow you to use the 3.3V pin of the UNO for stand alone testing when developing code. Just connect a 300-2K resistor from that pin to the analog input. Any signal has noise. Remember……Order emerges out of chaos. Add enough of these values together and things will be pretty stable. The running average method I use has the additional feature of scaling the number close to an engineering value. Keep in mind an interger can be negative and there is an upper limit to the value. Exceed that value and the number will roll over giving an unexpected result. Do some sample math before you start to see how large the numbers get. Numbers too small are also bad. Interger division truncates the value. Try to make numbers as large as possible before dividing. Instead of dividing by 100 to make a standard engineering unit like a watt, divide by 10 to make a deciwatt within the program. Avoid using floating point math.
MAP is a mapping or table conversion function that converts one number to another through interpolation. The example used here is temperature based on a diodes voltage. As diode voltage goes up the temperature goes down. If you know two points, map does all the work for you.
CONSTRAIN limits the upper and lower value of a variable.
// This section reads the analog signals. Range is 0 to 5V DC or 0 to 1023 counts or 4.9mV per count
AI0 = analogRead (0); // Anolog In. Read the battery voltage on PIN #A0
AI1 = analogRead (1); // Anolog In. Read the panel voltage on PIN #A1
AI2 = analogRead (2); // Anolog In. Read the solar voltage on PIN #A2
AI3 = analogRead (3); // Anolog In. Read the diode voltage on PIN #A3 to
// obtain Water Temperature
AI4 = analogRead (4); // Anolog In. Read the diode voltage on PIN #A3 to
// obtain Cooler Temperature
// This section takes a running average of the signal. Not really an average, it is the sum of N readings.
// One average of N samples is thrown out and a new sample is
// added in. At first it may not seem like the math works out but it does. Enter a fixed number and
// the final value will converge on that value times the number N.
// Not only does this average out the noise but it is a very convenient way to scale up the number.
// Adding up 20 A/D samples closely approximates battery voltage in mV.
// Just as easily that number can be made 19 or 21 to get you in the ballpark. Then you only need
// a slight resistor trim. Converting early to understandable engineering
// units makes the program much more understandable.
battery = battery - battery / 20; // Sum the readings by subtracting one average reading
// 13.5V = 3.3V
battery = battery + AI0; // Add latest A/D reading
panel = panel - panel / 48; // Sum the readings by subtracting one average reading
// 32V = 3.3V
panel = panel + AI1; // Add latest A/D reading
heater = heater - heater / 17; // Sum the readings by subtracting one average reading
// 80V = 3.3V
heater = heater + AI2; // Add latest A/D reading
heatervolt = heater / 100;
Wtemp = Wtemp - Wtemp / 10; // Sum the readings by subtracting one average reading before conversion to F
Wtemp = Wtemp + AI3; // Add latest A/D reading
// NOTE: Ctemp calculation is located in the refrigerator section so it can be easily removed from
// the program.
// Convert the digital input (times ten) into degree F using map function. Data conversions are easy
// using the map function of the ARDUINO
// Format for this is: map (input value, high temp value, low temp value, high temp of 212
// degrees, low temp of 32 degrees)
// Your A/D values may vary due to diode type and current through diode. Get values using
// ice bath and boiling water
// If you know air temp and get value in ice bath, high temp value may be determined by
// changing value untill the correct air temp
// value is obtained. No need to burn your figures. NOTE: diode has a lower voltage at
// higher temperature
// Constrain function keeps data values within the known values used by map function.
// Wtemp = constrain(Wtemp, 2600, 3700); // limits range of sensor values to between
//2600 and 3800
// FW = map(Wtemp, 2584, 3654, 212, 32); // For THREE small signal diodes with 10K pullup
// to +5
// map (X, low, high, 212F, 32F) FW is water temp in degrees F
// 3654 is measured low in ice bath
Wtemp = constrain(Wtemp, 3000, 5000); // limits range of sensor values to between 3000 and 5000
FW = map(Wtemp, 3100, 4867, 212, 32); // For FOUR small signal diodes with 10K pullup to +5V
// map (X, low, high, 212F, 32F) FW is water temp in degrees F
// 4867 is measured low in ice bath
// 4520 is measured at 68 degrees