Hi everyone,
So I'm using an Arduino UNO to build a circuit that will have a flex sensor detect changes in stomach circumference and at a certain threshold, will set off a vibration motor and LED simultaneously. The circuit looks like this (using 10k resistor for flex sensor and 1k for LED):
The code looks like this
[/*
Code to control vibration motor with flex sensor
Includes calibration on startup
*/
// declare pins
const int motorPin = 8;
const int sensorPin = A1;
// sensor variables
int sensor;
int flexMin = 1023; // high initial value
int flexMax = 0; // low initial value
int threshold; // will be calculated after calibration
const int n = 1; // number of purr cycles
int i; // counter for purring loop
void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
Serial.println("Calibrating flex sensor for 5 seconds...");
unsigned long startTime = millis();
// Calibration loop: 5 seconds
while (millis() - startTime < 5000) {
sensor = analogRead(sensorPin);
if (sensor < flexMin) flexMin = sensor;
if (sensor > flexMax) flexMax = sensor;
Serial.print("Calib Reading: ");
Serial.println(sensor);
delay(50); // small delay to limit reads
}
// Set dynamic threshold (50% between min and max)
threshold = flexMin + 0.5 * (flexMax - flexMin);
Serial.println("Calibration complete!");
Serial.print("Min: "); Serial.println(flexMin);
Serial.print("Max: "); Serial.println(flexMax);
Serial.print("Threshold: "); Serial.println(threshold);
}
void loop() {
sensor = analogRead(sensorPin);
// Print live sensor data to Serial Monitor
Serial.print("Live Reading: ");
Serial.println(sensor);
if (sensor > threshold) {
Serial.println("Threshold exceeded! Motor purring...");
i = 0;
while (i < n) {
digitalWrite(motorPin, HIGH);
delay(500);
digitalWrite(motorPin, LOW);
delay(500);
i++;
}
}
delay(100); // smooth out reads
}
]
Everything worked fine until the soldering for one of the ends on the flex sensor broke off, and I had to re-solder it. The soldering job was done well enough, but when I set-up the circuit again, my live readings from the flex sensor just came out to "0", with no amount of flexing changing this value. I thought the resistor might not be enough, so I switched out the 10k for a 47k, but the readings still remained inaccurate. Any ideas? Thanks so much for anyone who stumbles across this