I am having a difficult time creating some logic in my code.
I have a potentiometer connected to an ESP32 to simulate a load cell and a range sensor. The value for the force is between 0 and 400 newton and for the range between 50 and 0 mm. In the code I have a function that, when the position 40 mm is reached, starts the measure procedure where evenly depending on the position, the force and the correspondent position would need to be stored in a buffer. When reaching 0 mm a JSON would need to be created with the 5 measure points. If NUM_SAMPLES would be 5, we would start the first measure at 40 mm, the second at 30 mm, the third at 20 mm and so on. Reaching 0 mm the JSON would need to be created from the values from the buffer and printed out. The end result would need to be:
{"range_array":[40,30,20,10,0],"force_array":[82,163,241,322,400]}.
But now I get the JSON created and printed out continuously at 40 mm.
{"range_array":[40,30,20,10,0],"force_array":[82,82,82,82,82]}
If I go out from 40 mm it stops. Code follows. Thanks in advance for some help.
#include <Arduino.h> // Required by platformIO
#include <ArduinoJson.h> // JSON library for embedded C++
#include <WebSocketsServer.h> // WebSocketServer Bibliothek
const int potPin = 14; // Analog pin connected to the potentiometer
const int rangeMin = 0; // Minimum range of the simulated sensor in mm
const int rangeMax = 50; // Maximum range of the simulated sensor in mm
const int forceMin = 0; // Minimum force of the simulated sensor in newton
const int forceMax = 400; // Maximum force of the simulated sensor in newton
int rangeValue = 0;
int range_reading = 0;
int forceValue = 0;
int force_reading = 0;
unsigned long previousMillis = 0;
const long interval = 100;
WebSocketsServer webSocket = WebSocketsServer(81);
int NUM_SAMPLES = 5;
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
Read_Range_Sensor();
Read_Loadcell();
// Check if position is 40mm, then start measurement
if (range_reading == 40) {
performMeasurement();
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.print("Range: ");
Serial.print(range_reading);
Serial.print(" mm, Force: ");
Serial.print(force_reading);
Serial.println(" N");
}
}
void Read_Range_Sensor() {
rangeValue = analogRead(potPin);
range_reading = map(rangeValue, 0, 4095, rangeMax, rangeMin);
}
void Read_Loadcell() {
forceValue = analogRead(potPin);
force_reading = map(forceValue, 0, 4095, forceMin, forceMax);
}
void performMeasurement() {
// Arrays to store positions and forces
int range_array[NUM_SAMPLES];
int force_array[NUM_SAMPLES];
// Start measuring from 40mm to 0mm
for (int i = 0; i < NUM_SAMPLES; i++) {
range_array[i] = 40 - i * (40 / (NUM_SAMPLES - 1));
force_array[i] = map(analogRead(potPin), 0, 4095, forceMin, forceMax);
}
// Create JSON object
StaticJsonDocument<200> doc;
JsonArray rangeJson = doc.createNestedArray("range_array");
JsonArray forceJson = doc.createNestedArray("force_array");
// Populate JSON arrays
for (int i = 0; i < NUM_SAMPLES; i++) {
rangeJson.add(range_array[i]);
forceJson.add(force_array[i]);
}
// Serialize JSON to string
String jsonString;
serializeJson(doc, Serial);
serializeJson(doc, jsonString);
}