#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows
// Flex sensor pins
const int flexPins[] = {5, 6, 7, 16, 17}; // GPIO pins connected to flex sensors
int flexValues[5]; // Array to store readings
void setup() {
Wire.begin(8, 9); // SDA = GPIO 8, SCL = GPIO 9
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Flex Sensors");
Serial.begin(115200); // Initialize Serial communication
// Initialize sensor pins
for (int i = 0; i < 5; i++) {
pinMode(flexPins[i], INPUT);
}
delay(2000);
lcd.clear();
}
void loop() {
// Read flex sensor values
for (int i = 0; i < 5; i++) {
flexValues[i] = analogRead(flexPins[i]);
}
// Print voltage values to Serial Monitor
Serial.println("Flex Sensor Voltages:");
for (int i = 0; i < 5; i++) {
float voltage = map(flexValues[i], 0, 4095, 0, 5000) / 1000.0; // Convert to voltage in volts
Serial.print("Sensor ");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(voltage, 3); // Print voltage with 3 decimal places
Serial.println(" V");
}
Serial.println("------------------------");
// Determine the number to display
int number = getNumber(flexValues);
// Display the number on the LCD
lcd.setCursor(0, 0);
lcd.print("Number: ");
lcd.setCursor(8, 0);
lcd.print(number);
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second line
delay(500); // Refresh delay
}
// Function to determine the number based on voltage ranges
int getNumber(int values[]) {
float normalized[5];
for (int i = 0; i < 5; i++) {
normalized[i] = map(values[i], 0, 4095, 0, 5000) / 5000.0; // Normalize to 0.0 - 1.0
}
for (int i = 1; i <= 28; i++) {
float lowerBound = (i - 1) * 0.0357; // Adjusted interval for 28 ranges
float upperBound = i * 0.0357;
if (normalized[0] > lowerBound && normalized[0] <= upperBound &&
normalized[1] > lowerBound && normalized[1] <= upperBound &&
normalized[2] > lowerBound && normalized[2] <= upperBound &&
normalized[3] > lowerBound && normalized[3] <= upperBound &&
normalized[4] > lowerBound && normalized[4] <= upperBound) {
return i;
}
}
return 0; // Default if no condition is met
}