circular or dynamic array

Hi
I am trying to determine the delta, or degree of change, from a simple changing analog voltage (I’m currently using a 500k pot for testing; 0-1023). Following some guidance I implemented this code:

int hist[10];
int inVal;
int delta;

//initialize the array, then

void loop(){
inVal=analogRead(A0); 

 for(int i=0; i<10; i++){
 hist[i]=hist[i+1];
 }
hist[9]=inVal;

delta = hist[9]-hist[5];
delay(50);
}

I have a fair idea of what this is doing, but as it isn’t functioning as expected, could someone review it to identify any flaws or improvements please?
what currently happens is that when the pot isn’t moved I get zero, when the pot is moved slowly I get low delta; when moved quickly I get high values BUT the delta value is relative to the pot value itself, so in the low pot range, low delta is low - when the pot is in the high range, low delta is actually high!

Thanks
Brendan

for(int i=0; i<10; i++){
 hist[i]=hist[i+1];
 }

You don’t have a “hist [9 +1]”

There is no need to move data around, simply indices.

What exactly are you trying to achieve?

If you just want to know how much the reading has changed since last time you went round the loop, then just keep a variable with the last value like this:

int oldReading = 0;
void loop()
{
  int newReading = analogRead(A0);
  int delta = newReading - oldReading;
  oldReading = newReading;
}

Hi

thanks for this; my code with your solution is below. I just had my "Doh!!" moment.........the pot I'm using is logarithmic, hence the unexpected high delta values.

int newSample = 0;         //voltage reading
int oldSample = 0;
int sampleRate = 200;

void setup(){
  Serial.begin(9600); 
}

void loop(){
  newSample=analogRead(A0);
  delay(sampleRate);
  newSample/=6;
  int delta = newSample-oldSample;
  oldSample=newSample;
  Serial.print(newSample);
  Serial.print("\t\t\t\t");
  Serial.println(abs(delta));
}

Brendan