Hey I just purchased a 50mm SoftPot and am using it to set a humidity variable, the only problem is that when the Potentiometer is released it goes back to a reading of zero. Can you help me with a code that can detect the last value before the soft pot is released so i can use it to set the humidity level?
Sounds like you need to detect that the pot is being “touched” and then keep track of the value until it’s detected as being “not touched”. Then you (your code) uses the last value before the “untouch” to set the variable. The first part is easy, you look for the analog voltage to be > (or <, depending on how you’ve wired this up) some “touch” threshold voltage.
Keeping track of the input is also easy, I suspect your code does that now. Just put the samples into a ring array (of length N) and move the pointer/index.
So what’s left is to determine when the pot is not being touched. My inclination is to keep a running average (once touched) of N samples (N can’t be too long since it must track the user input changes) and then when 2 or 3 consecutive samples differ from the average by some %, or some fixed amount, declare the pot to be “untouched”. Remove the last 2 or 3 samples from the running average, use the average of the remaining as your set point.
Thank you for your response it was very helpful! I Soldered a pulldown resister to the soft pot analog in I used the arduino smoothing code from their website and then just added one if statement that checks if Analog read value is over 10 before putting it through the code.
const int numReadings = 10;
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
int val = 0;
int analogPin = A2;
void setup()
{
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
val = analogRead(analogPin);
if (val > 10)
readings[index] = val;
// 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(1); // delay in between reads for stability
}
So a couple of if() … else() statements should do it.