Arduino code:
const int flexPin = A0; // Analog pin the flex sensor is connected to
const float fixedResistance = 47000.0; // Resistance of the fixed resistor in ohms
// Calibration values (replace these with your own)
const float straightResistance = 25000.0; // Resistance of the flex sensor when
straight
const float bentResistance = 100000.0; // Resistance of the flex sensor when fully
bent
const float straightAngle = 0; // Angle corresponding to straight position (in
degrees)
const float bentAngle = 90.0; // Angle corresponding to fully bent position (in
degrees)
void setup() {
[Link](9600); // Initialize serial communication
}
void loop() {
int flexValue = analogRead(flexPin); // Read the analog value from the flex
sensor
float voltage = flexValue * 5.0 / 1023.0; // Convert analog value to voltage
float flexResistance = fixedResistance * (5.0 / voltage - 1.0); // Calculate
resistance of the flex sensor
// Map the resistance value to an angle using linear interpolation
float angle = map(flexResistance, straightResistance, bentResistance,
straightAngle, bentAngle);
[Link](angle); // Print the angle to the serial monitor
delay(1000); // Short delay between readings
}