Pressure in system adjustable with potentiometer

Hello, I'm working on my first bigger project. I want to control pressure in system with potentiometer. Value from potentiometer is converted to 0-100 [%] and it should be equal to cca 0-30000 [Pa] pressure in system. I have small compressor, pressure sensor, lcd and air valve (all supplied by DC power externaly). The initialization and calibration of air in the system should start only when value on potentiometer will change. I'm not sure what I'm doing wrong but it everytime stuck on "while" contition.

I'll be glad for any idea, how to solve it. Thank you in advance.

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

// Potenciometr 
int analogInput_0 = A0; // Potentiometer at Analog 0
int potRaw; // Potentimeter value (0-1023)
int potPercent; // Potentimeter value (0-100)
int lastPotPercent=0; // Potentimeter value from previous step

// Senzor tlaku 
int analogInput_1 = A1; // Pressure sensor at Analog 1
int pressureRaw; // Pressure value (0-1023)
float pressure; // Pressure value (0-400000 [Pa])
int pressurePercent; // Pressure percentage (30000 Pa = 100%) - it is the limit I want to reach

// Motor and valves
int digitalOutput_4 = 4; // Compressor for inflating
int digitalOutput_5 = 5; // Valve for air bleed

int value;

LiquidCrystal_I2C	lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack

void setup()
{
  Serial.begin(9600);
  pinMode (analogInput_0, INPUT);
  pinMode (digitalOutput_4, OUTPUT);
  pinMode (digitalOutput_5, OUTPUT);
  
  // actiavtion of LCD
  lcd.begin (16,2); // pro 16 x 2 LCD display
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  pinMode(analogInput_1, INPUT);
}

void loop()
{
  
  potRaw = analogRead(analogInput_0);
  pressureRaw = analogRead(analogInput_1); 
  pressure = abs(map(pressureRaw, 0, 1023, 0, 400000));
  pressurePercent = pressure/300;
  potPercent = abs(map(potRaw, 0, 1023, 0, 100));
 
  if (potPercent != lastPotPercent)
  {
  value = min(pressurePercent, potPercent);
   
    // Turn on compressor   
    if (value = pressurePercent)
    
    while (pressurePercent < potPercent)
    {
    digitalWrite(4, HIGH);
    }
    else
    {
    digitalWrite(4, LOW);
    }
    
    // Turn on valve for air bleed
    while (pressurePercent > potPercent)
    if (value = potPercent)
    {
    digitalWrite(5, HIGH);
    }
    else
    {
    digitalWrite(5, LOW);
    }
  }
  lastPotPercent = potPercent;
  

  lcd.home ();

  lcd.print(pressurePercent, 1);
  lcd.print(" % Press");

  lcd.setCursor (0,1); 

  lcd.print(potPercent, 1);
  lcd.print(" % Pot");
  delay(50);

}

but it everytime stuck on "while" contition.

Yes it will, because there is nothing that changes the variables in the while conditions inside the loop. You must keep reading the pressurePercent or the potPercent or both inside the loop.

Besides what Mike said,

if (value = pressurePercent)

Perhaps you meant:-

if (value == pressurePercent)

Also, following this there are no curly brackets around the code you want to execute if the condition is true.

Did you mean if?:-

while (pressurePercent < potPercent)
    {
    digitalWrite(4, HIGH);
    }
    else
    {
    digitalWrite(4, LOW);
    }

Many thanks to both you guys for advice! I've done some corrections and improvements and my system is working now almost perfectly :slight_smile:

I'm still fighting with one last issue - when I'm reading analog value from potentiometer, it should be in range 0-1023 and in the most cases it is solid constant value, but sometimes it jumping. For example from 1022 to 1023 (1022, 1022, 1023, 1022...) or... 561 to 562, even if I don't touch potentiometer.

Any ideas why it happens and how to prevent from happening it?

You will almost always get those minor changes in the output from an ADC. You need to write your code to ignore them. In other words, only treat it as a change if the value varies by (say) 5.

...R

Yes, this is the way how I have it at the moment, I set to ignore the changes which are lower than 2 and it is working, I was just curious, what it happen and if it something wrong done by me... :slight_smile:

Tomik723:
I was just curious, what it happen and if it something wrong done by me... :slight_smile:

An ADC creates a digital value representative of an analog value. If the analog value is just at the difference between two of the digital values you will get jitter. Imagine, for example, that the "correct" digital value should 103.5 but that must resovle resolve to either 103 or 104.

...R

but that must resovle to either 103 or 104.

and noise will ensure that sometimes you get 103 and at others you get 104.

Understand perfectly, it sounds logical, thanks for explaination.