Auto Zero Or Pushbutton To Set Zero Of A Sensor

I am wanting to auto zero a sensor(MPX5100) or be able to use a pushbutton to zero. Right now I just have it setup for a trim pot.

Any help greatly appreciated.

#include "U8glib.h"

  int rawValue; // A/D readings
  //int offset = 38; // zero pressure adjust (~30-72)
  int fullScale = 819; // max pressure adjust (~798-840)
  float pressure; // final pressure in mmHg
  int battery;
  int pressurezero;

  //pinMode(A1, INPUT);
  //digitalWrite(A1, HIGH);

  // constructor call for display
  U8GLIB_ST7920_128X64 u8g(13, 11, 10, U8G_PIN_NONE); // SPI Com: SCK = en = 13, MOSI = rw = 11, CS = di = 0

  void draw(void)
{
  // graphic commands to redraw the complete screen should be placed here
}


  void setup(void)
{
  u8g.setRot180(); // flip screen / rotated for current display
  // set SPI backup if required / not required with current display
  //u8g.setHardwareBackup(u8g_backup_avr_spi);
}


  void loop(void)
{
  pressurezero = analogRead(A2)/8;
  rawValue = analogRead(A0);
  pressure = (rawValue - pressurezero)* 650.00 / (fullScale - pressurezero); // 1300.00 normaly 650.00 for calibrated pressure conversion
  battery = analogRead(A1)/10.1;  // Display battery percentage at max 100%

  // picture loop
  u8g.firstPage();

  do
{

  draw();

  if(battery >65)
{
  u8g.setFont(u8g_font_unifontr); //Set font
  u8g.drawStr( 30, 10, "FLOW RATE");

  u8g.setFont(u8g_font_fub30r); //Set font
  u8g.setPrintPos(32, 46);
  u8g.print(pressure,0);
  
  u8g.setFont(u8g_font_unifontr); //Set font
  u8g.drawStr( 16, 64, "BATTERY %");
  u8g.setPrintPos(88, 64);
  u8g.print(battery);
}
  else
{
  u8g.setFont(u8g_font_unifontr); //Set font
  u8g.drawStr( 7, 38, "CHARGE BATTERY");
}
} 
  while( u8g.nextPage() );

  // rebuild the picture after some delay
  delay(500);
}

Try this...

const int ZeroButton = 4; //put the zero button on pin 4. Connects this pin to ground when pressed.
const int PressurePin = A0; //pressure sensor is connected to this analog pin

void setup() {
   ...
   pinMode(ZeroButton, INPUT_PULLUP);
}

void loop() {
  if(digitalRead(ZeroButton) == LOW) {
    //zero button is currently pressed
    pressurezero = analogRead(PressurePin);
  }
  rawValue = analogRead(PressurePin);
  ...
}

I will give that a try!

Thanks,
Kevin

Ok here is the code I am using it in. It seems to have messed everything up on the display. What am I doing wrong?

Thanks,
Kevin

// include the library code:
   #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
   LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

   int rawValue; // A/D readings
   int offset = 40.5; // zero pressure adjust (~30-72)
   int fullScale = 819.0; // 819 max pressure adjust (~798-840)
   float pressure; // final pressure in mmHg
   int Battery; // battery % of use
   int pressurezero;
   const int ZeroButton = 4; //put the zero button on pin 4. Connects this pin to ground when pressed.
   const int PressurePin = A0; //pressure sensor is connected to this analog pin


   void setup()
{
   // set up the LCD's number of columns and rows:
   lcd.begin(16,2);
   pinMode(ZeroButton, INPUT_PULLUP);
}

  void loop() 
{
  rawValue = analogRead(A0);
  pressure = (rawValue - offset)* 655.00 / (fullScale - offset); // for calibrated pressure conversion
  Battery = analogRead(A1)/8.05;  // Display battery percentage at max 100%

{
  if(digitalRead(ZeroButton) == LOW)
{
  //zero button is currently pressed
  pressurezero = analogRead(PressurePin);
}
  rawValue = analogRead(PressurePin);
}

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

  lcd.setCursor(0 , 0);
  lcd.print("FLOW RATE:");
  lcd.print(pressure,0);
  lcd.print("   ");
  
  if (Battery >65)
{
  lcd.setCursor(0 , 1);
  lcd.print("BATTERY:");
  lcd.print(Battery);
  lcd.print("%     ");
}
  else
{
  lcd.setCursor(0 , 1);
  lcd.print("*CHARGE BATTERY*");
}
  delay(500);
}

What am I doing wrong?

Piss-poor indenting, for one thing.

Useless curly braces, for another.

And last, but not least, you have not told us what the code actually does, and how that differs from what you expect. "It seems to have messed everything up on the display. " is just whining. It is not useful information.

Why do you assign a new value to rawvalue on every pass through loop(), and then again assign it a new value if some "button" is pressed?

Seems I posted what I want to do in the original post!

I was letting MorganS know what happened on the code they posted when I inserted it & implemented into mine. Seems like your the one Whining!

If you look at the code you can see it reads pressure, it has an offset so you can match the pressure sensor up to the processor and I want to be able to be able to zero it automatically or with a pushbutton.

The code is to read pressure just like a pressure meter and reads it in mmhg.