 # Char array? assingning values to variables

Hello!

I have a question, I hope you can help me.

Suppose I have an "x" sensor, it can be whatever.

This will be throwing me data, for example, every 50ms.

And I have 3 variables (x1, x2 and x3). Where x1 will be the sensor value at 50ms, x2 at 100ms and x3 at 150ms.

My question is: How do I assign these values ​​to the variables? In addition, the sensor will continue tanking values ​​every 50ms, so, when its take the next value. My x1 will be at 100ms, x2 at 150ms and x3 at 200ms, again, for example.

I think that it can be do with a char array, that is the reason for the name of the post. But I'm not sure xD!

Best regards!!

Example for a sr04 taking values every 50ms:

``````#include "SR04.h"
#include "math.h"
#define TRIG_PIN 12
#define ECHO_PIN 11

SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);

int vol;
int dist;
unsigned long t = millis();

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

void loop() {

dist = sr04.Distance();
vol = ((PI * (10*10)) * dist);

if (millis() - t >= 50)
{
Serial.println(vol);
t = millis();
}
}
``````

If you want to store the most recent three samples, one way is a circular buffer:

``````int values;
int index = 0;

void loop()
{
values[index++] = x;
if (index >= 3)
index = 0;
}
``````

Thx John!

This is my code now and its works!

``````#include "SR04.h"
#include "math.h"
#define TRIG_PIN 12
#define ECHO_PIN 11

SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);

int vol;
int dist;
int values;
int index = 0;
String arreglo = "";
unsigned long t = millis();
int x1;
int x2;
int x3;

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

void loop() {

if (millis() - t >= 5000)
{

dist = sr04.Distance();
values[index++] = dist;
Serial.println(dist);
t = millis();

if (index >= 3)
{
index = 1;
x1 = values;
x2 = values;
x3 = values;
Serial.print(x1) + Serial.print("/") + Serial.print(x2) + Serial.print("/") + Serial.println(x3);
values = values ;
}
}
}
``````

The first index should be 0, not 1.

``````if (index >= 3)
{
index = 1;
x1 = values;
x2 = values;
x3 = values;
Serial.print(x1) + Serial.print("/") + Serial.print(x2) + Serial.print("/") + Serial.println(x3);
values = values ;
}
``````

values = values ;

thats why i put the index like 1, and not 0. The first value is taken from the last

why would you do that?

Is necesary for the proyect.

Gonna be a PID Controler and to calculate the integral and the derivative I need to use the 3rd value as a first value.

Postscript: I do it cus dont allow me to use the PID library 