1

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);
}
dda
1,5951 gold badge12 silver badges17 bronze badges
asked Mar 2, 2024 at 8:52
1
  • 1
    your code works correctly ... the problem is that the five readings occur within a few microseconds ... add delay code, or pushbutton code, to increase the interval between reads Commented Mar 2, 2024 at 15:58

1 Answer 1

1

Let me start of with a few assumptions, because this information / context is missing:

  • The force will be measured across a non-negligible amount of time (order of milliseconds, seconds)
  • The potmeter is controlled by either a human or a motor that takes some time to turn (milliseconds to seconds) it from the simulated 0N to 400N.

The codes in its current state, is just measuring the potmeter 5 times in "an instant" (a few microseconds).

I suggest that additional logic be implemented to change the potmeter to the wanted value, and then measure.

I also see a few more inconsistencies / illogical actions in this program, i.e. the same potmeter is used as the force and as the range. Read_Range_Sensor and Read_Loadcell perform exactly the same functionality, as potPin never changes.

The intention is not quite clear here (what needs to be achieved, how).

answered Mar 6, 2024 at 11:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.