Hello all, I just wanted to post up my progress so far. This is day one for me as an Arduino owner, I feel I'm getting the hang of it OK.
Synopsis: I am designing a system that will measure the amount of water in a large tank, send email notifications when the level is low, and prevent a pressure pump that draws from the tank from running if the level is critically low. The thread about relay control of the pump is http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231937645/6#6, although I have not yet gotten that far.
First step was to prove that I could define an arbitrary range in inches, to be represented as 0-100%, and have the sensor readings output the position in that range. For the proof of concept, I used 10 LEDs for my "meter", and an 11th LED as my critically low indicator. This is the point at which the pump relay would be activated. The set point for the email notification is not in there, since the concept clearly works.
YouTube video is here:
Here's the sketch as well. I have serial output code in there for debugging purposes, which is commented out.
/*
Takes MaxSonar Analog input and shows the level
as a percentage of a range on 10 LEDs.
Carson C. Jan 2009
*/
int rangeMax = 36; // (INCHES) Top end of the range we want to measure, i.e. "100%"
int rangeMin = 6; // (INCHES) Bottom end of the range we want to measure, i.e. "0%" (There is no LED for 0%)
int rangeWarning = 10; // (INCHES) Warning variable, turns on an additional LED when the reading is at or below this point
float currentPercent = 0; // 0-100% range
int pinLEDLowEnd = 30; // Lowest pin in the LED array
int pinLEDHighEnd = 39; // Highest pin in the LED array
int pinLEDWarning = 40; // Warning LED
int pinAnalogInput = 5; // Analog Input pin for the range sensor
int pinSet; // Used to define which pin is currently being turned on/off
float signalDivisor = 1.953; // Tuning number for the analog signal
void setup() {
// Assign the LED output pins
for (int i=pinLEDLowEnd; i <= pinLEDHighEnd; i++) {
pinMode(i, OUTPUT);
}
// Assign the warning LED pin
pinMode(pinLEDWarning, OUTPUT);
// Serial output for debugging
//Serial.begin(9600);
}
void loop() {
float inches = float(analogRead(pinAnalogInput)) / signalDivisor; // Acquire the reading
// If the sensor returns a position beyond our desired range, reel it in
if (int(inches) > rangeMax) {
inches = float(rangeMax);
}
if (int(inches) < rangeMin) {
inches = float(rangeMin);
}
//Serial.print(int(inches));
//Serial.print(" inches raw\n");
currentPercent = (inches - rangeMin) / (rangeMax - rangeMin); // Calculate the current percentage position in the range
currentPercent *= 100; // Convert the decimal back to a whole number
// Set the meter LEDs
pinSet = pinLEDLowEnd;
for (int i = 10; i <= 100; i += 10) {
if (int(currentPercent) >= i) {
digitalWrite(pinSet, HIGH);
} else {
digitalWrite(pinSet, LOW);
}
pinSet++;
}
// Check the warning state and set the LED appropriately
if (int(inches) <= rangeWarning) {
digitalWrite(pinLEDWarning, HIGH);
} else {
digitalWrite(pinLEDWarning, LOW);
}
//Serial.print(int(currentPercent));
//Serial.print("%\n");
}
Next step is to smooth out the data returned by the sensor. Pointers?
[edit]Looks like there's some smoothing stuff in the Playground. What a resource![/edit]