# is there a way to create a "Delta" value for an analog input signal?

so I want to use the analog input to measure a 0-5v sensor.
and I have that just fine
but I also want to be able to see the velocity/voltage change over a specific period of time

I know its as simple as analogRead to get the main value

and I know I can then use current value -PrevReading
but I need it for longer than 1 frame...
I need it to do that math over 1ms and keep updating it giving me the differences from the new value and whatever the value was 1ms ago,
my analog signal is being fed to another device at a rate one value every 0.1 ms
and I need that delta value to be sent out at that same rate as it updates calculating from a value 1ms ago

so example would be
example would be at 0ms, valueA = 3
at 0.1ms value A = 3.2
at time 1.0ms, valueA is 4
delta value would be 1.0

at 1.1ms, valueA is 3.9
so delta would be 0.7
storing 1ms worth of values to be able to do the comparison and always doing First in First out math

is this possible??

It appears that you need to store the last ten or so readings. This is easily done with an array. You need to know how to:

Declare an array.
Store into an array.
Use a value in the array.
Use a loop to move all of the elements of the array. (Actually, this problem can be solved without moving any elements but that is a more advanced topic. )

Sure, this is possible !

ok… I have an array, reading the value, moving them along with this code

``````int deltaX[200];

for(int x = 0; x<200; x++){
}

Serial.println(deltaX[199]-deltaX[0]);
``````

But I am unable to get it to use my variable which is used earlier in my code
it just returns a value of 0 when I try to use this

``````int deltaX[200];

for(int x = 0; x<200; x++){
deltaX[x] = (Voltage1);
}

Serial.println(deltaX[199]-deltaX[0]);
``````

What am I missing

``````for(int x = 0; x<200; x++){
deltaX[x] = (Voltage1);
}
``````

Why are you setting all 200 entries in the array to the same value ?

It would be helpful to see a complete sketch rather than snippets of it

this isnt setting them all to the same value
this is updating on the fly as I understand from youtube videos
and it appears to look that way on my device when I watch the serial monitor

or am I reading it wrong?

this is the whole sketch
the only thing missing so far is the main void loop area where I have set up for 2 more sections that I havent even started on yet
which looks like this

``````void loop(){
analog_loop();
delta_loop();
visual_loop;
delay(0.1);

}
``````

store it as "Voltage1 for multiple sections to use without a value update
Use "Volatge1 in multiple math outputs

1st thing I need to solve, is how to use the Voltage1 Variable instead of having to say analogRead(A0) in the array

I don’t see any need for an array. You just need to take snapshots of the analog value every 1ms and compare each one to the previous value as you described in your original post. I would recommend using the micros() function to time the snapshots.

``````unsigned long lastSnapshot;
...
...
void loop() {
...
...
if (micros() - lastSnapshot > 1000) {
lastSnapshot += 1000;
...
...
``````
``````this isnt setting them all to the same value
``````

As Voltage1 does not change within the for loop then they will all have the same value

Can I please repeat my request for you to post a complete sketch so that we don't have to piece it together and guess how and where variables are declared

Your array should be 10 elements, not 200.
Every 100us you read A0 and calculate voltage.
You then shift all the array members up 1 to make room for the new voltage. The oldest value drops off the end. Insert the new voltage at the beginning.
You then calculate delta from the first and last array element.
Repeat the whole cycle.

ok… I think I understand what I’m not doing right after looking harder…

also… 200 is total samples needed…not 10

If the example in your OP is valid, the array size is 10 because the amount of history you have to keep, to be able to print the current delta value, is 10 samples. That your experiment produces 200 samples is not relevant to the core calculation. Exactly the same logic applies if it is a total of 10,000 samples.

If you need analog samples every 100us, you have to read A0 every 100us. Or what do you mean ?

also... here is everything

I already read the analog value from pin A0 and I put it in a variable called Voltage1
this pin A0 will get used in multiple spots, but I only want to read the value once per cycle
so I want to use

``````// Initialize Sensor Inputs /////////////////////////////////////

int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int sensorValue6 = 0;

int Voltage1 = 0;
int Voltage2 = 0;
int Voltage3 = 0;
int Voltage4 = 0;
int Voltage5 = 0;
int Voltage6 = 0;

unsigned long lastSnapshot;

void setup(){

//Serial Computer Port Speed//
Serial.begin(115200);
}

void loop(){
analog_loop();
delta_loop();
display_loop();
}

void analog_loop(void){

int Voltage1 = sensorValue1*0.488758553;
int Voltage2 = sensorValue2*0.488758553;
int Voltage3 = sensorValue3*0.488758553;
int Voltage4 = sensorValue4*0.488758553;
int Voltage5 = sensorValue5*0.488758553;
int Voltage6 = sensorValue6*0.488758553;
}

void delta_loop(void) {

if (micros() - lastSnapshot > 200) {
lastSnapshot += 200;

}

void display_loop(void) {
}
}
``````

FYI... this will get a CAN bus loop later, which is why I'm converting analog inputs into voltatages as whole intergers thru a multiplier...

the item that will read them will use a multiplier to get it back down to decimals

Have you finished editing the post above? Can I safely read the code and comment on it?

I suggest you get something working based on only one analog pin, then scale it up to all seven later. I don’t see an array in your latest code and it appears that the sample rate is now 1 sample every 200us.

``````int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int sensorValue6 = 0;
``````

Whenever you find yourself declaring variables with numbers at the end of their names, you should be using an array.

Please learn to use Auto Format, the indentation is a mess.

``````int Voltage1 = sensorValue1*0.488758553;
``````

When you put a type like "int" in front of a variable name, you are declaring a new variable, not updating any previously defined variable, even if the name is the same. If you do this inside a function, the new variable will only exist within that function, and it's value will be lost when the function ends.

To compound that, you declare an int then put a float value into it

ok...
so in this part of this equation...

``````if (micros() - lastSnapshot > 200) {
lastSnapshot += 200;

``````

what do I do to make it so that I can use the existing variable Voltage1 in place of newReading

if (micros() - lastSnapshot > 200) {
lastSnapshot += 200;

is it as simple as just get rid of the INT??

also, I'm going to combine everything into one giant loop instead of subloops

ok… I think I just saw what my brain wasnt understanding

``````  if (micros() - lastSnapshot > 1000) {
lastSnapshot += 1000;
``````

I’m sure theres some way to simplify this…
but this works right now… so I’m good

Thank you for your help and putting up with my “new to all of this”… stupidity

For goodness sake, please post a complete sketch rather than a snippet

Why are you copying Voltage1 to newReading ?
Where does Voltage1 come from ?

UKHeliBob:
For goodness sake, please post a complete sketch rather than a snippet

Why are you copying Voltage1 to newReading ?
Where does Voltage1 come from ?

are you not able to see that I already posted the whole thing back a few posts ago??

thereis zero reason to repost the entire thing again when I only need to work on the one section

for now I'm solved and functioning properly with my last code bit I posted

I'm now down to just this bit to do what I want it to do, and I'll be able to copy and paste for the other inputs I need to use this on as well

``````if (micros() - lastSnapshot > 1000) {
lastSnapshot += 1000;