Sensor values: compare "now" vs "last"

Im trying to compare an analog value to its previous value over some time to get the difference as a result.r
something like...

const long interval

interval = 100 //millis

sensorValNow = analogRead(A0);
sensorValLast = (sensorValNow+interval)

result = (sensorValNow - sensorValLast)

Im sure this is a poor excuse for a workable bit of code, but i hope it gets the point across.

Do you have a question?

well yes, how to construct a working bit of code to do this

I don't understand how the interval seems to relate to the analogue reading.

Is 1/10th of a second somehow related to the last value?

the interval can be what ever. Ultimately it will determine the sensitivity of the result. THIS CODE DOES NOT WORK I am asking for help constructing code that will allow me to compare

(sensorValNow-SensorValLast) = result

How should the code be written to achieve this?

I was hoping i could use the bones of the "blink with out delay" sample, but i was having trouble writing a value after the interval was over

THIS CODE DOES NOT WORK

The code you have posted so far doesn't stand a hope of compiling, so it can never run,so it can never be said to work/not work.

So why not post the code you've tried?

The basic idea is:

(1) make and save measurement1
(2) record time1
(3) do something else for a while
(4) make and save measurement2
(5) record the time2
(6) compare measurements 2 and 1 and if needed, compute time2 - time1

Here is what I have. It compiles, and writes a result to the serial monitor. problem is the result is always "0.00". I thought maybe declaring the sensor as an INPUT was giving me trouble so i commented it out. no change.

unsigned long time = millis();
int sensorValNow = 0;
int sensor = A0;

void setup()
{
  //pinMode(sensor, INPUT);
  Serial.begin(9600);
}


void loop() {
  float sensorValNow;
  float sensorValLast;
  float result;

  sensorValNow = analogRead(sensor);

  {
    if (millis() - time > 1000)  //Has one second passed?
    {
      sensorValLast = sensorValNow;
      time = millis();           // reset time.

      result = (sensorValNow - sensorValLast);
      Serial.println(result);
    }
  }
}

  static float sensorValLast; But then you need to move the assignment to sensorValLast down a bit.

Is your question about scope? You want loop() to run for a while and don't know how to store the previous value outside loop?

Try a global variable or static variable.

I didn't try to compile this code so it could be rife with syntax errors. It's more to figure out what question you are asking. This is my guess at what I think you are asking.

Global Variable

int prevVal = 0; //This variable is global scope and can be accessed by the functions below.

int compareVals(int x, int y){
  //make whatever comparison you like
  if(x == y)
    return 0;
  else
    return 1;
}

loop(){
  int localVal;
  localVal = digitalRead(PIN_FOR_INPUT);

  if( compareVales(localVal, prevVal) ){
    //do some stuff
  }

  prevVal = localVal;
}

Static Variable

int compareVals(int x, int y){
  //make whatever comparison you like
  if(x == y)
    return 0;
  else
    return 1;
}

loop(){
  static int prevVal = 0; //This variable is local scope but is saved in memory where globals are and will persist (hold the value) until the scope comes back to the function that declared it.
  int localVal;
  localVal = digitalRead(PIN_FOR_INPUT);

  if( compareVales(localVal, prevVal) ){
    //do some stuff
  }

  prevVal = localVal;
}
  float sensorValNow;
  float sensorValLast;
  float result;

Every time loop() "loops", these variables are recreated new.

If that is not what you wish, either make those variables global (put the declarations outside of the setup() and loop() functions), or make them static, as suggested above.

...but only one, as noted above, needs to be static or global

AWOL:

  static float sensorValLast;

But then you need to move the assignment to sensorValLast down a bit.

by just changing the float to static float, it did start producing a result. but without scaling the analog input should read from 0-1023 representing 0-5V correct? From one end of the potentiometer to the other my result is 5 or -5. how can this be if the sensor input is not scaled?

it also incriments by 1.00. so far i have seen results of 1, 2, 3, 4, 5, 0 -1,-2,-3,-4,-5

From the last time you read the pot to the next my result is 5 or -5. how can this be if the sensor input is not scaled?

Would that be closer to the truth? (we can't see your code)

unsigned long time = millis();
int sensorValNow = 0;
int sensor = A0;

void setup()
{
  //pinMode(sensor, INPUT);
  Serial.begin(9600);
}


void loop() {
  float sensorValNow;
  static float sensorValLast;
  float result;

  sensorValNow = analogRead(sensor);

  {
    if (millis() - time > 1000)  //Has one second passed?
    {
      sensorValLast = analogRead(sensor);
      time = millis();           // reset time.

      result = (sensorValNow - sensorValLast);
      Serial.println(result);
    }
  }
}

      sensorValLast = analogRead(sensor); Think.
Does that make any sense?