Sensor values increment in jumps, not smoothly

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
luv2solder
 
Posts: 42
Joined: Tue Jun 21, 2011 4:15 pm

Sensor values increment in jumps, not smoothly

Post by luv2solder »

I am using an Arduino MEGA 2560 to read a vacuum sensor and display the result in mm Hg and percent of maximum possible vacuum on a 20x4 LCD display. The problem is that the values do not increment smoothly. Instead, they jump from 0 to 7.6 mm Hg, then to 15.2 mm Hg, then to 22.8 mm Hg, then to 30.4 mm Hg, etc. -- always with 7.6 mm Hg jumps even though I am smoothly increasing the vacuum level. I have the same problem when I smoothly reduce the vacuum: 30.4 jumps to 22.8 to 15.2 to 7.6 to 0 -- never anything between those 7.6 mm Hg jumps.

Here is the code:

Code: Select all

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int vacPin = 0; // input pin for vacuum sensor

void setup() {
  lcd.begin(20, 4);
}

void loop(){
  float vac; // sensor value
  float percent; // the mapped value
  float vacBASE;
  float mmHg;
  vacBASE=37.00; // manually zero reading by inserting raw vacuum value to correct for offset voltage at zero vacuum
  vac = (analogRead(vacPin) - vacBASE) * 1.06; // 1.06 to correct for sensor span of 4.7 V, not 5 V
  percent = map(vac,0,1023,0,100);
  lcd.setCursor(0, 0);
  lcd.print("VACUUM: ");
  mmHg=((percent*760)/100);
  lcd.setCursor(0, 1);
  lcd.print(mmHg,1);
  lcd.print(" mm Hg   ");
  lcd.print(percent,1);
  lcd.print("%");
  delay(1000);
  lcd.clear();
}

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Sensor values increment in jumps, not smoothly

Post by adafruit_support_bill »

Code: Select all

      percent = map(vac,0,1023,0,100);
      lcd.setCursor(0, 0);
      lcd.print("VACUUM: ");
      mmHg=((percent*760)/100);
The map function is going to give you a maximum of 100 steps over the full-scale.
The last calculation multiplies by 760 then divides by 100 - in effect, multiplying by 7.6.

Better to slip the mapping step (which is effectively dividing your resolution by a factor of ~10) and divide by 1023 in the units conversion.

User avatar
luv2solder
 
Posts: 42
Joined: Tue Jun 21, 2011 4:15 pm

Re: Sensor values increment in jumps, not smoothly

Post by luv2solder »

Thank you. I realized the problem last night (after turning off my computer -- hence no update then) after reading about how the map function uses integers. I sincerely appreciate your reply (and others you've given), which is why I order from you whenever possible.

BTW, if anyone is interested, I'll post my updated code that reads the MPX5100DP vacuum sensor and displays the following onto a 20x4 LCD display:
  • 1. raw sensor reading
    2. vacuum in mm Hg
    3. vacuum in psi
    4. vacuum in kPa (kilopascals)
    5. vacuum as % of maximum possible

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”