Hello,
I have arduino pins 4 and 8 connected with a 1M resistor and a wire coming from 4, hooked up to a plant. At standby, I usually get readings somewhere in the 20s, when I touch the plant, depending on how far from the lead hook up, I get readings from 50-300.
Usually this works perfectly.
After minutes or hours, I get readings that are in the 400s at standby and 1000 when touched...or 200 at standby and 800 when touched etc. If i hit the reset pin on the arduino, everything starts back and I get between 20-300 again.
I set up some code to automatically reset the arduino every 10 seconds but this didn't solve the problem. I was still getting the number spike. Only a hard long push of the reset button seems to reset the read back.
So my questions are...
is there any components I could wire up to make this not happen? A diode? A capacitor?
Is there anything I could put in the code to keep my reading steady?
Here is what I have so far:
#include <CapacitiveSensor.h>
int resetPin = 12;
CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
const int numReadings = 10;
//smoothing stuff
long readings[numReadings]; // the readings from the sensor
long index = 0; // the index of the current reading
long total = 0; // the running total
long average = 0; // the average
//int inputPin = A0;
int resetTime;
void setup()
{
digitalWrite(resetPin, HIGH);
Serial.begin(9600);
//smoothing stuff
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop()
{
int CurrentTime = millis();
/*if (CurrentTime >= resetTime+10000){
digitalWrite(resetPin, LOW);
delay(1000);
digitalWrite(resetPin, HIGH);
resetTime=millis();
Serial.println("RESET!");
}
resetTime++;*/
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = cs_4_8.capacitiveSensor(10); //resolution set to 10
// 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
Serial.println(average);
delay(10); // delay in between reads for stability
}