#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with I2C address, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2); // Update 0x27 with your LCD's I2C address if different
int flexSensorPin = 25; // Analog pin to read the flex sensor output
int flexValue; // Variable to store the sensor value
int angle; // Variable to store the angle
void setup(void) {
Serial.begin(115200);
// Initialize the LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
delay(100); // Allow some time for the LCD to initialize
// Display initial message
lcd.setCursor(0, 0);
lcd.print("Initializing...");
delay(1000); // Pause to see the initialization message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Flex Sensor");
delay(1000); // Pause to see the "Flex Sensor" message
}
void loop() {
// Read the analog value from the flex sensor
flexValue = analogRead(flexSensorPin);
// Map the raw flex sensor value to an angle (0-180 degrees)
// Assuming the flex sensor value ranges from 0 to 1023
angle = map(flexValue, 0, 2073, 0, 180);
// Clear the LCD and display flex sensor data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Flex Angle:");
lcd.setCursor(0, 1);
lcd.print(angle); // Display the angle on the LCD
// Print the flex value to the serial monitor
Serial.print("Raw Flex Value: ");
Serial.println(flexValue);
Serial.print("Angle: ");
Serial.println(angle);
// Add your custom processing based on flex sensor value here
// Example:
if (angle > 90) {
// Code for when the sensor is bent more than 90 degrees
lcd.setCursor(10, 1);
lcd.print("Bent");
Serial.println("Status: Bent");
} else {
// Code for when the sensor is less bent
lcd.setCursor(10, 1);
lcd.print("Flat");
Serial.println("Status: Flat");
}
delay(500); // Slow down the updates for readability
}