Creating an Automatic Calibration Feature

Hi Guys:

So this code is for an automatic air pump that will pump the air and release the pressure through a valve, but in order to do this I need the pressure sensor to get calibrated. Currently I do calibrate the pressure senor manually and then reprogram the arduino (writing a program to see that where is the 0 mmHg using a PPT). I have this code that I simply have two issue's with. One, the LCD screen is updating the screen every 10 micro seconds (the main reason that I did this is because I wanted the live data to get displayed correctly), this is an annoying problem, How could I stop this? How could I Fix the issue? Secondly How could I put an option for an Auto calibration? I'm looking for some thing that you could have a button to press and that would make that 0 mmHg.

last Updated file.txt (15.2 KB)

theres a few problems that are easy to spot

like lcd.begin(20, 4); only needs to be done once in set up

using i is a bad idea as it looks like a one

repeating the same lcd prints 11 times

adding

lcd.clear (); all over the place even though the lcd didn't change (that causes flashing)

adding same library twice

#include <LiquidCrystal.h>

posting as a note pad instead of attaching as a file

see if this is any better (haven't changed your code just moved things about)

#include <LiquidCrystal.h>

int  analogInPin = A2;
const float Null = 0.50;                                     // Null VDC; datasheet Page 32.
const float Sensitivity = 13.33;                             // Sensitivity mV/psi; datasheet Page 32.
int SwitCont = 13;                                           // This stage is extra due to the fact of extra information....LED connected to digital pin 13.
int pinButton = 8;                                               // Pushbutton connected to digital pin 5.
int pinValve = 6;                                                 // Solenoid valve pin number assigned.
int pinMotor = 7;                                                 // 12V DC motor pin number assigned.
int hold = 0;
int count = 1;
int prevcount = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin = A0; //Potentiometer input pin
int potValue1 = 0;
int potValue2 = 0; // final display variable
float pressuremmHg = 0;
float pressure = 0;

void setup()
{
  pinMode(13, OUTPUT);                                    // Pin mode setup for the switch controller.
  pinMode(8, INPUT);                                      // Input of the switch.
  pinMode (7, OUTPUT);                                    // Pin mode setup for the DC motor.
  pinMode (6, OUTPUT);                                    // Pin mode setup for the Solenoid valve.
  lcd.begin(20, 4); // lcd rows and columns
  lcd.clear ();
  lcd.setCursor(0, 1);
  lcd.print("Current Pressure in"); // title of sorts
  lcd.setCursor(0, 2);
  lcd.print("The Air Chamber"); // title of sorts
}
void loop()                                                  //Begining of the loop counter.
{
  if (digitalRead (pinButton) == HIGH)
  {
    float pressurePSI, pressureVDC;            // Initializtions.
    pressure = analogRead(analogInPin);                    // Reading Information
    pressureVDC = (float)pressure * 0.0048828125;            // (5V/1024 (digital counts) = 0.0048828125) 1024 is the maximun voltage at 5V and 0 is the 0V.
    pressureVDC = pressureVDC - Null;                        // Sensor Calculations.
    pressurePSI = pressureVDC / Sensitivity * 1000;
    pressuremmHg = pressurePSI - 0.15 ;

    if (count != 12) {//same screen up till 12
      if (count != prevcount) {
        //only if count changed clear once
        lcd.clear ();
      }
      lcd.setCursor(0, 1);
      lcd.print("Current Pressure in"); // title of sorts
      lcd.setCursor(0, 2);
      lcd.print("The Air Chamber"); // title of sorts
      lcd.setCursor(6, 4);
      lcd.print(pressuremmHg);
      lcd.print("mmHg   ");
      delay(10);
      prevcount = count;
    }

    switch (count)
    {
      case 1:
        digitalWrite (6, HIGH);
        delay (300);
        digitalWrite (7 , HIGH);
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure > 140)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 2;
        break;

      case 2:
        digitalWrite (6, HIGH);
        digitalWrite (7 , HIGH);
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure > 210)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 3;
        break;

      case 3:
        digitalWrite (6, HIGH);
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure < 170)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 4;
        break;

      case 4:
        digitalWrite (6, HIGH);
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 5;
        break;

      case 5:
        digitalWrite (6, HIGH );
        digitalWrite (7, HIGH);
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure > 400)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 6;
        break;

      case 6:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 7;
        break;

      case 7:
        digitalWrite (6, HIGH );
        digitalWrite (7, HIGH);
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure > 920)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 8;
        break;

      case 8:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure < 896)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 9;
        break;

      case 9:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure < 871)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 10;
        break;

      case 10:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure < 842)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 11;
        break;

      case 11:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          pressure = analogRead(analogInPin);
          if (pressure < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 12;
        break;

      case 12:
        digitalWrite (6, HIGH);
        delay (1000);
        digitalWrite (6, LOW);
        digitalWrite (7, LOW);
        delay(10000);
        break;
    }
  }
}

aghaderi2006:
Hi Guys:

So this code is for an automatic air pump that will pump the air and release the pressure through a valve, but in order to do this I need the pressure sensor to get calibrated. Currently I do calibrate the pressure senor manually and then reprogram the arduino (writing a program to see that where is the 0 mmHg using a PPT). I have this code that I simply have two issue's with. One, the LCD screen is updating the screen every 10 micro seconds (the main reason that I did this is because I wanted the live data to get displayed correctly), this is an annoying problem, How could I stop this? How could I Fix the issue? Secondly How could I put an option for an Auto calibration? I'm looking for some thing that you could have a button to press and that would make that 0 mmHg.

including a #include file twice is unnecessary but compiler (preprocessor) will take care of it. If you are using state machine use "statex" instead of non-descriptive "count". I would recommend to carefully comment each state, what is its function and when it gets switched to next state. Assuming that "while" loop takes finite time to reach desired pressure, and you want to know the pressure I would insert the LCD print inside the "while" loop. As coded the 2 seconds delay will show the current pressure AFTER 2 seconds . I think your program is not running the states and their "while" loops correctly if the LCD gets updated every 10 microseconds.

Sorry for the late reply, Thanks for the feed back, really appreciated. Ill try it again and see the out come. Ill keep you posted.

Concerning autocalibration. I have seen little auto tire caps which have a little binary pressure gauge built in. They appear as one color, until your tire pressure drops below a certain level, then they show a different color. They are probably not very accurate, but if you google about and find such a binary pressure gauge of reasonable accuracy, perhaps you could autocalibrate by adapting such a gauge to your system. Lower the tank pressure below the binary criterion of the gauge, then slowly pump up the tank until the gauge changes state.

A little far out, but that's what science is all about.

come on mate help us out by posting the code correctly so we can just select and paste. please read the how to post at the top of the forum

So I have uploaded your code and everything just works fine (I changed the code a bit but that was only for the Pressure sensor), there is a small problem that I have noticed that the LCD is displaying the previous state (case), for instance, when the chamber is filled with 100mmHg air pressure the LCD shows 0mmHg on the LCD screen. The LCD displays is showing the previous state. How could i Fix this issue? I have attached the code below, Again thanks for the fast responses.

#include <LiquidCrystal.h>

int  analogInPin = A2;
int SwitCont = 13;                                           // This stage is extra due to the fact of extra information....LED connected to digital pin 13.
int pinButton = 8;                                               // Pushbutton connected to digital pin 5.
int pinValve = 6;                                                 // Solenoid valve pin number assigned.
int pinMotor = 7;                                                 // 12V DC motor pin number assigned.
int hold = 0;
int count = 1;
int prevcount = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin = A0; //Potentiometer input pin
int potValue1 = 0;
int potValue2 = 0; // final display variable


void setup()
{
  pinMode(13, OUTPUT);                                    // Pin mode setup for the switch controller.
  pinMode(8, INPUT);                                      // Input of the switch.
  pinMode (7, OUTPUT);                                    // Pin mode setup for the DC motor.
  pinMode (6, OUTPUT);                                    // Pin mode setup for the Solenoid valve.
  lcd.begin(20, 4); // lcd rows and columns
  lcd.clear ();
  lcd.setCursor(0, 0);
  lcd.print("Current pressure in"); // title of sorts
  lcd.setCursor(0, 1);
  lcd.print("the air Chamber is: "); // title of sorts
}
void loop()                                                  //Begining of the loop counter.
{
 // if (digitalRead (pinButton) == HIGH)
  {
       float pressuremmHg; // final pressure in mmHg
       int offset = 54; // zero pressure adjust (~30-72)
       int fullScale = 822; // max pressure adjust (~798-840)
       int rawValue; // A/D readings
       rawValue = analogRead(analogInPin);
       pressuremmHg = (rawValue - offset) * 300.0 / (fullScale - offset);
   

    if (count != 12) {//same screen up till 12
      if (count != prevcount) {
        //only if count changed clear once
        lcd.clear ();
      }
      lcd.setCursor(0, 0);
      lcd.print("Current pressure in"); // title of sorts
      lcd.setCursor(0, 1);
      lcd.print("the air chamber is: "); // title of sorts
      lcd.setCursor(6, 3);
      lcd.print(pressuremmHg);
      lcd.print(" mmHg   ");
      delay(10);
      prevcount = count;
    }

    switch (count)
    {
      case 1:
        digitalWrite (6, LOW);
        delay (300);
        digitalWrite (7 , HIGH);
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue > 140)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 2;
        break;

      case 2:
        digitalWrite (6, LOW);
        digitalWrite (7 , HIGH);
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue > 210)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 3;
        break;

      case 3:
        digitalWrite (6, HIGH);
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);;
          if (rawValue < 170)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 4;
        break;

      case 4:
        digitalWrite (6, HIGH);
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);;
          if (rawValue < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 5;
        break;

      case 5:
        digitalWrite (6, LOW );
        digitalWrite (7, HIGH);
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);;
          if (rawValue > 400)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 6;
        break;

      case 6:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 7;
        break;

      case 7:
        digitalWrite (6, LOW );
        digitalWrite (7, HIGH);
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue > 920)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 8;
        break;

      case 8:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue < 896)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 9;
        break;

      case 9:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue < 871)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 10;
        break;

      case 10:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue < 842)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 11;
        break;

      case 11:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          rawValue = analogRead(analogInPin);
          if (rawValue < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 12;
        break;

      case 12:
        digitalWrite (6, HIGH);
        delay (1000);
        digitalWrite (6, LOW);
        digitalWrite (7, LOW);
        delay(10000);
        break;
    }
  }
}

your code has a lot of problems with the flow. Long delays and the use of "while" create gaps where the lcd is not being updated. Try this its a band aid that may work. With out sitting down with a pencil, paper and designing the program to flow with out stopping it might work as you want it to.

#include <LiquidCrystal.h>

int  analogInPin = A2;
int SwitCont = 13;                                           // This stage is extra due to the fact of extra information....LED connected to digital pin 13.
int pinButton = 8;                                               // Pushbutton connected to digital pin 5.
int pinValve = 6;                                                 // Solenoid valve pin number assigned.
int pinMotor = 7;                                                 // 12V DC motor pin number assigned.
int hold = 0;
int count = 1;
int prevcount = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin = A0; //Potentiometer input pin
int potValue1 = 0;
int potValue2 = 0; // final display variable
float pressuremmHg; // final pressure in mmHg
int rawValue; // A/D readings
int offset = 54; // zero pressure adjust (~30-72)
int fullScale = 822; // max pressure adjust (~798-840)

void setup()
{
  pinMode(13, OUTPUT);                                    // Pin mode setup for the switch controller.
  pinMode(8, INPUT);                                      // Input of the switch.
  pinMode (7, OUTPUT);                                    // Pin mode setup for the DC motor.
  pinMode (6, OUTPUT);                                    // Pin mode setup for the Solenoid valve.
  lcd.begin(20, 4); // lcd rows and columns
  lcd.clear ();
  lcd.setCursor(0, 0);
  lcd.print("Current pressure in"); // title of sorts
  lcd.setCursor(0, 1);
  lcd.print("the air Chamber is: "); // title of sorts
}
void loop()                                                  //Begining of the loop counter.
{
  lcdUpdate();
  // if (digitalRead (pinButton) == HIGH)
  {

    switch (count)
    {
      case 1:
        digitalWrite (6, LOW);
        delay (300);
        digitalWrite (7 , HIGH);
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue > 140)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 2;
        break;

      case 2:
        digitalWrite (6, LOW);
        digitalWrite (7 , HIGH);
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue > 210)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 3;
        break;

      case 3:
        digitalWrite (6, HIGH);
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue < 170)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 4;
        break;

      case 4:
        digitalWrite (6, HIGH);
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 5;
        break;

      case 5:
        digitalWrite (6, LOW );
        digitalWrite (7, HIGH);
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue > 400)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 6;
        break;

      case 6:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 7;
        break;

      case 7:
        digitalWrite (6, LOW );
        digitalWrite (7, HIGH);
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue > 920)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        digitalWrite (7 , LOW);
        delay (2000);
        count = 8;
        break;

      case 8:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue < 896)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 9;
        break;

      case 9:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {

          lcdUpdate();
          if (rawValue < 871)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 10;
        break;

      case 10:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue < 842)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 11;
        break;

      case 11:
        digitalWrite (6, HIGH );
        hold = 1;
        while (hold)
        {
          lcdUpdate();
          if (rawValue < 105)
          {
            hold = 0;
          }
        }
        digitalWrite (6, LOW);
        delay (2000);
        count = 12;
        break;

      case 12:
        digitalWrite (6, HIGH);
        delay (1000);
        digitalWrite (6, LOW);
        digitalWrite (7, LOW);
        delay(10000);
        break;
    }
  }
}

void lcdUpdate() {
  rawValue = analogRead(analogInPin);
  pressuremmHg = (rawValue - offset) * 300.0 / (fullScale - offset);
  if (count != 12) {//same screen up till 12
    if (count != prevcount) {
      //only if count changed clear once
      lcd.clear ();
    }
    lcd.setCursor(0, 0);
    lcd.print("Current pressure in"); // title of sorts
    lcd.setCursor(0, 1);
    lcd.print("the air chamber is: "); // title of sorts
    lcd.setCursor(6, 3);
    lcd.print(pressuremmHg);
    lcd.print(" mmHg   ");
    delay(10);
    prevcount = count;
  }
}

gpop1:
your code has a lot of problems with the flow. Long delays and the use of "while" create gaps where the lcd is not being updated. Try this its a band aid that may work. With out sitting down with a pencil, paper and designing the program to flow with out stopping it might work as you want it to.

So I have uploaded your code but there is a lot of noise on the screen, it looks good and it does exactly what I want but there are some extra characters that it just makes the screen to show some wired stuff. I really appreciate your help in this matter and you have helped me a lot, thank you for the fast responses.

nothing another band aid couldn't fix.....lol

the only thing moving on the screen should be the pressure so you can try this as a fix

void lcdUpdate() {
  rawValue = analogRead(analogInPin);
  pressuremmHg = (rawValue - offset) * 300.0 / (fullScale - offset);
  
  //decied what you want to do on screen 12
  
  if (count != 12) {//same screen up till 12
    if (count != prevcount) {
      //only if count changed clear once
      lcd.clear ();
    }
    lcd.setCursor(0, 0);
    lcd.print("Current pressure in"); // title of sorts
    lcd.setCursor(0, 1);
    lcd.print("the air chamber is: "); // title of sorts
    lcd.setCursor(6, 3);
    if(pressuremmHg<10){//think this is correct it adds a space 
    //so pressuremmHg when printed does not move
      lcd.print(" ");
    }
    if(pressuremmHg<100){
      lcd.print(" ");
    }
    lcd.print(pressuremmHg,3);//restrict the decimal to 3 points
 lcd.print(" mmHg");//dont let this run off the screen it will end
 //up on line one if you do
    delay(10);
    prevcount = count;
  }
}

gpop1:
nothing another band aid couldn't fix.....lol

the only thing moving on the screen should be the pressure so you can try this as a fix

Thank you man, really appreciated.