The calibration problem in the code

I made a code for my two flexion sensors but I can't calibrate them to display the correct values on my LCD. do you think you can help me? this is the code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

int flexPin1 = A0;
int flexPin2 = A1;
const int LCD_COLS = 16;  // Number of LCD columns
const int LCD_ROWS = 2;
float VCC = 4.7; // Arduino este alimentat cu 5V VCC
float R2 = 10000;
// Calibration values - adjust these as needed
int value1;
int value2;
const int STRAIGHT_VALUE_1 =600 ;  // Raw sensor value when straight (sensor 1)
const int STRAIGHT_VALUE_2 = 600;  // Raw sensor value when straight (sensor 2)
const int BEND_VALUE_1 = 1023;   // Raw sensor value at 90 degrees bend (sensor 1)
const int BEND_VALUE_2 = 1023;   // Raw sensor value at 90 degrees bend (sensor 2)

// LCD display object
LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);

void setup()
{
  // Initialize the LCD
  lcd.begin(LCD_COLS, LCD_ROWS);
  lcd.init();

  // Print initial text on the LCD
  lcd.print("Angle 1:   g");
  lcd.setCursor(0, 1);
  lcd.print("Angle 2:   g");

  Serial.begin(9600);
  pinMode(flexPin1, INPUT);
  pinMode(flexPin2, INPUT);
}

void loop()
{
  value1 = analogRead(flexPin1);
  value2 = analogRead(flexPin2);

  // Map the sensor values to angles
  int angle_1 = map(value1, STRAIGHT_VALUE_1, BEND_VALUE_1, 90, 0);
  int angle_2 = map(value2, STRAIGHT_VALUE_2, BEND_VALUE_2, 90, 0);
  angle_1 = constrain(angle_1, 0, 90);
angle_2 = constrain(angle_2, 0, 90);


  // Display the angles on the LCD
  lcd.setCursor(9, 0);
  lcd.print(angle_1);
  lcd.print(" ");  

  lcd.setCursor(9, 1);
  lcd.print(angle_2);
  lcd.print(" "); 

  Serial.println(value1);
  Serial.println(value2);
  delay(1500);
}

What's the problem?

(There's no need to set analogue pins used with analogRead as INPUT)

1 Like

I can't calibrate the sensors to show the correct values on the LCD

Try to pretend that we don't know what the "correct" values might be, and how incorrect the values you see are

1 Like

I should be able to calibrate these sensors in such a way that when they are introduced into the collar created by me, when the subject sits with his head motionless, they display 0 degrees and when he starts to bend his head, they measure the cervical flexion angle from 0 to 90 degrees. for example, when I manage from the code to set 0 degrees as the starting point, instead of displaying 90 degrees, when the sensor is bent to 90, it displays 50

Hi,
Can you please post link to data/specs of your sensors.

Why don't you just recalibrate with your software.
So 90 displays 90.

Tom.. :smiley: :+1: :coffee: :australia:

How were these two values determined?

You also have the low and high values in the map function reversed.

I don't understand what else I can do to make them work and display correctly. the completion of my degree depends on this project.

I tried the maximum value that they can measure. I took them from the serial monitor

I tried a lot of values... I don't know what else to do to calibrate them.

That's the maximum value you can get from a ten bit ADC

what values do you advise me to use?

First you need to measure the resistance of the flex sensor with an ohmmeter when it is bent 90 degrees.

Let's say you measure 50K ohms.

Then the bend_value for 90 degrees would be: 1023 x [50K/(10K+50K)] = 852

As I already mentioned, You also have the low and high values in the map function reversed.

1 Like

The resistance is wrapped in cables, I can't measure it

Hi, @iulianapavel123
Thanks for the spec sheet.

Have you got a proper schematic of your project, I doubt if a Degree committee would accept Fritzy diagrams.

@jim-p suggestion is a good one and you should plot a graph of angle vs resistance, don't expect the response to be linear.

I assume the strip is mounted vertically in line with the neck.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:
PS. Interesting approach I would have used an IMU to measure angle.

YES, the sensor is positioned vertically, but I can't make the table, because the resistors are covered by heat shrink. this is the diagram in the paper. I don't have time to change the components and the approach. I really need to make it work like this

Follow this excellent, general tutorial on how to calibrate sensors.

that's not what i need

Good luck with your project.

1 Like