The updated Arduino sketch follows: #include <EEPROM.h> const int OUT_PIN = A2; const int IN_PIN = A0; //Capacitance between IN_PIN and Ground //Stray capacitance is always present. Extra capacitance can be added to //allow higher capacitance to be measured. const float IN_STRAY_CAP_TO_GND = 24.48; //initially this was 30.00 const float IN_EXTRA_CAP_TO_GND = 0.0; const float IN_CAP_TO_GND = IN_STRAY_CAP_TO_GND + IN_EXTRA_CAP_TO_GND; const int MAX_ADC_VALUE = 1023; //averaging setup const int numReadings = 10; //the number of readings to be averaged int readings[numReadings]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average //Bargraph setup const int ledCount = 10; // the number of LEDs in the bar graph const int led1 = 2; const int led2 = 3; int ledPins[] = { led1, led2, 4, 6, 7,8,9,10,11, 12 }; // an array of pin numbers to which LEDs are attached int calLowPin = A4; // use 10k pullup resistor with momentary int calHiPin = A5; // switch to ground int calLow = 0; int calHi = 0; int calLowVal = 40; // approx range of probe empty int calHiVal = 80; // to full in pfd int calSwitch = 5; void setup() { pinMode(calLowPin, INPUT); pinMode(calHiPin, INPUT); // initialize all the readings to 0: for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; pinMode(OUT_PIN, OUTPUT); //digitalWrite(OUT_PIN, LOW); //This is the default state for outputs pinMode(IN_PIN, OUTPUT); //digitalWrite(IN_PIN, LOW); pinMode(calSwitch, OUTPUT); // loop over the led pin array and set them all to output: for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT); } Serial.begin(9600); } void loop(){ calLow = digitalRead(calLowPin); calHi = digitalRead(calHiPin); //Capacitor(Probe) under test between OUT_PIN and IN_PIN //Rising high edge on OUT_PIN pinMode(IN_PIN, INPUT); digitalWrite(OUT_PIN, HIGH); int val = analogRead(IN_PIN); //Clear everything for next measurement digitalWrite(OUT_PIN, LOW); pinMode(IN_PIN, OUTPUT); //Calculate result float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val); int sensorval = capacitance; // subtract the last reading: total= total - readings[index]; // read from the sensor: readings[index] = sensorval; //average routine // add the reading to the total: total= total + readings[index]; // advance to the next position in the array: index = index + 1; // if we're at the end of the array... if (index >= numReadings) // ...wrap around to the beginning: index = 0; // calculate the average: average = total / numReadings; // send it to the computer as ASCII digits delay(1); // delay in between reads for stability // read the sensor: int sensorReading = average; // map the result to a range from 0 to the number of LEDs: int ledLevel = map(sensorReading, calLowVal, calHiVal, 0, ledCount); if (calLow == LOW) { //take empty tank cal reading digitalWrite(calSwitch, HIGH); calLowVal = average; EEPROM.update(0, calLowVal);} //save empty calibration result else { digitalWrite(calSwitch, LOW); } if (calHi == LOW) { //take full tank cal reading digitalWrite(calSwitch, HIGH); calHiVal = average; EEPROM.update(1, calHiVal);} //save full calibration result else { digitalWrite(calSwitch, LOW); } // loop over the LED array: for (int thisLed = 0; thisLed < ledCount; thisLed++) { // if the array element's index is less than ledLevel, // turn the pin for this element on: if (thisLed < ledLevel) { digitalWrite(ledPins[thisLed], HIGH); // High for transistor input } // low for low current display // turn off all pins higher than the ledLevel: else { digitalWrite(ledPins[thisLed], LOW); // Low for transistor input } //High for low current display if (digitalRead(led1) == 1 && digitalRead(led2) == 0) { digitalWrite (led1, LOW); //Blink bottom led if it's //Low for xstr input delay (200); //the only one lit digitalWrite (led1, HIGH); //High for xstr input delay (200); } } /*Serial.print("Capacitance Value = "); Serial.print(sensorval); Serial.println(" pF "); Serial.print(" Calibration Low = "); //for debug Serial.print(calLowVal); //comment out for final upload Serial.println(" pF "); Serial.print("Calibration High = "); Serial.print(calHiVal); Serial.println(" pF ");*/ while (millis() % 500 != 0); calLowVal = EEPROM.read(0); //values needed if system was powered calHiVal = EEPROM.read(1); //down if (EEPROM.read(0) == 255) { //if new chip EEPROM.write(0, 40); //approx empty reading of probe } if (EEPROM.read(1) == 255) { EEPROM.write(1, 80); // approx full reading of probe } } |