Need help programming for calibrating the flex sensors

Good day! I am very new at Arduino programming, what I want to do is to calibrate my flex sensors by using the Arduino Calibration code ( https://www.arduino.cc/en/Tutorial/Calibration ), what will happen is that I have a LCD that will tell the user to extend their finger which will give the maximum input of the flex sensor and will be read and stored by the program, then the user will flex their finger which will give the minimum input of the flex sensor whcih will also be read and stored by the program. I can’t seem to get it work :frowning: this is my program

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // Digital pins assinged on each LCD display pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#include <Servo.h>
Servo servo1; // creates the first servo object to control the servo

const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
const int inputPin = A1; // Analog Input pin for flex sensor
int pushpin = 7; // Digital Pin for pushbutton
int var = 0; // variable reading for pushpin
int sensorValue = 0; // the sensor value
int sensorMax = 0; // maximum sensor value
int sensorMin = 1023; // minimum sensor value
int angle;
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
servo1.attach(10); // attaches the servo object to the PWM pin 2
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0; }
lcd.begin(16,2); // indicates the size of the LCD display 16x2

}

void loop()
{

// MAIN PROGRAM for the Servomotors and LCD display
var = digitalRead(pushpin);
total = total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
total = total + readings[readIndex];
readIndex = readIndex + 1;

if (readIndex >= numReadings) {
readIndex = 0;
}

sensorValue = total / numReadings;
delay(1);

if (var == HIGH ){ // This conditions applies to the pushbutton when presssed

sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 180);
sensorValue = constrain(sensorValue, 0, 180); // in case the sensor value is outside the range seen during calibration
servo1.write(sensorValue);
lcd.clear();
lcd.setCursor(0, 0); // sets the character to column 0, row 0
lcd.print(angle);
lcd.print(" “);
lcd.print(” Degrees ");
delay(10);
}
else { // If the first condition is not met, it means that the pushbutton is unpressed and will read the following program

servo1.write(180); // returns the servomotor back to initial position
if (millis() < 2000){
lcd.print(“WELCOME”);
lcd.home();
delay(500);
}
else {
delay(500);
lcd.clear();
lcd.print(“PLEASE EXTEND”);
delay(10000);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

lcd.home();
lcd.print(“FLEX YOUR HAND”);
delay(10000);
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}

lcd.clear();
lcd.home();
lcd.print(“CALIBRATING”);;
delay(5000);
}
Serial.print(sensorMax);
Serial.print("|");
Serial.print(sensorMin);
Serial.print("|");
Serial.println(sensorValue);

}

delay(10); // waits time for the servo to operate again
// END
}

can't seem to get it work

What should it do ?
What does it do ?

Hi,

acesagcal:
I have a LCD that will tell the user to extend their finger which will give the maximum input of the flex sensor and will be read and stored by the program, then the user will flex their finger which will give the minimum input of the flex sensor whcih will also be read and stored by the program.

You have to understand that the statement delay(10000) means that your loop() code will stop running for 10 seconds. You don’t want that, you want to keep reading the input value for 10 seconds.

The other thing is that you want your program to do a sequence of things, the program needs to be in separate ‘states’ to “read the max. for 10 secs” or “read the min. for 10 secs” and so on.

There are several ways to arrange a program to time something without actually stopping (read up on millis() ).

The example I give here uses the very simple approach of setting a delay() at the end of loop() so we know it takes 10 milliseconds (roughly) for each loop. Then a delay of 1000 milliseconds is just a count of 100 loops.

This is just-typed-in, not-tested-at-all code as an example of how you can structure your code to give you what you want:

// Define STATES the program can be in:
#define WELCOME     0
#define EXTEND_HAND 1
#define FLEX_HAND   2 
#define CALIBRATING 3
#define FINISHED    4

int programState= WELCOME;
int looptimes;

void loop()
{
  //
  // read input and average values here
  // save new SensorValue

  switch( programState )
  {
  case WELCOME:
    lcd.print("WELCOME");
    delay(500);
    lcd.clear();
    lcd.print("PLEASE EXTEND");
    programState= EXTEND_HAND;
    looptimes= 0;
    break;
  case EXTEND_HAND:
    // record the maximum sensor value while in this state
    if (sensorValue > sensorMax) { 
      sensorMax = sensorValue;
    }
    if( looptimes >= 1000 ){        // done 1000 loops * 10mS = 10 seconds ?
      lcd.clear();
      lcd.print("FLEX YOUR HAND");  // change display for next state
      programState= FLEX_HAND;      // and switch to next STATE
      looptimes= 0;                 // remember to clear the loop counter
    }
    break;  
  case FLEX_HAND:
    // record the minimum sensor value while in this state
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }   
    if( looptimes >= 1000 ){    // done 1000 loops * 10mS = 10 seconds
      lcd.clear();
      lcd.print("CALIBRATING");
      programState= CALIBRATING;
      looptimes= 0;   
    }
    break;  
  case CALIBRATING:
    // is there anything to do in this state ?
    if( looptimes >= 500 ){ 
      lcd.print("FINISHED");
      looptimes= 0;
      programState= FINISHED;
    }
    break;
  case FINISHED:
    // program will stay in this state forever
    // unless we read some button and change state for example
    break;
  }
  looptimes= looptimes+1;  // count number of loops
  delay( 10 );     // go around loop() every 10 milliseconds
}

Yours,
TonyWilk

Thanks for the reply, yes indeed the delay was so long :slight_smile: