standardizing a reading

Hello,
I have a 1M resistor in between pins 4 and 8 and am using the CapSense library to get a reading when you touch a plant. It works great for awhile, I get numbers between 15 and 215...but sometimes, for no reason I can understand, I get readings between 60 and 260, or 500 and 700 or 220 and 420, etc...the one thing it always has in common is the reading has a variation of about 200.

So I want to do some sort of code to normalize the reading so no matter what I always get 0-200.

Is this possible? I would just sample like 20ms of readings every, say, 10 seconds, make those the new low? But what happens if a sample is being taken while someone is touching the plant and the new low is actually the new high?

There must be an example of this type of code out there, no? I think it's a pretty normal thing but I don't really know where to look.

Thanks for any help!

What I do for my temperature sensor is take an average.
I take 10 readings say once every 5 minutes and just average them.

That way if I get any crazy numbers they are discounted.

Just use a for loop that takes 10 readings and add up all the readings.
Right after the loop divide the results by 10.

thanks so much for the help. i'm not sure exactly how that would work...because i can go for like an hour where the numbers are say 80-280 but i want it to be 0-200 so taking an average might not work. also what happens when someone interacts with the input? i want the spike to be recognized as an interaction....
basically it'd be something like i'd have to be constantly changing what the low number is...but at the same time not ever setting it to an interaction number....

What have you tried so far?

Post your code.

thanks. here’s what i have so far. it’s working okay unless i’m interacting with the sensor during the reading.

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);       
long previousTime = 0;
long checkTime = 5000;

const int sampleSize = 50;
int average = 0;
int readings[sampleSize];
int total = 0;
void setup()                    
{
  Serial.begin(9600);
  //set everything in array to 0
  for (int thisReading = 0; thisReading < sampleSize; thisReading++){
    readings[thisReading] = 0;
  }
}

void loop(){ 
  //get an average read every checkTime and use that number to subtract from final output
  unsigned long currentTime = millis();
  
  if (currentTime - previousTime > checkTime){
     //take a sample
    for (int i = 0; i < sampleSize; i++){
      total = total - readings[i];
      readings[i] = cs_4_8.capacitiveSensor(10); //resolution set to 10
      total = total + readings[i];
      
    }
    
    previousTime = currentTime; //reset timer
    average = total / sampleSize; //get average read of sample
    Serial.println("Sample taken at:"); //testing outputs
    Serial.println(average);
  }
  
  
  int currentRead = cs_4_8.capacitiveSensor(10);
  int newRead = currentRead - average;  
  //clamp reading so it's between 0 and 200
  newRead = max(newRead, 0);
  newRead = min(newRead, 200);
  //print out 
  Serial.println(newRead);   
  delay(200);        // delay in between reads for stability. high num right now for testing     
  
}

I am a bit confused.

Do you simply want to ignore readings that are outside a certain range (caused, perhaps, by someone interfering).

If you have a sequence of readings in the range 15 to 215 and then you discover that the next reading is (say) 280 you may be able to flag it as an error.

However if things move to a new range (say) 60 to 260 and if the next reading is (say) 187 which is valid in both ranges I don't see how you could tell that there had been a change of range.

This sounds to me like a problem that needs to be solved outside the Arduino.

...R