Κώδικας:
// Dangerous Dave's Boost Gauge Project// Reads boost from MPX4250AP on Analog 0.
// Converts vacuum readings to inHg.
// Displays peak boost
// include libraries
#include <LiquidCrystal.h>
#include <LcdBarGraph.h>
float rawval = 0; // Setup raw sensor value
float kpaval = 0; // Setup kPa value
float boost = 0; // Setup boost value
float barboost = 0; // Setup value for boost bar
float vac = 0; // Setup vacuum value
float peak = 0; // Setup peak value
LiquidCrystal lcd(12, 11, 8, 7, 6, 5); // set LCD interface pins
byte lcdNumCols = 20; // number of columns in the LCD
LcdBarGraph lbg1(&lcd, 16, 0, 1); // setup bargraph with width 20 starting at column 0 on row 1
byte five[8] = { B01110, B01000, B01110, B00010, B01110, B00000, B00100, B00100 }; // Setup custom LCD character for bargraph legend
byte ten[8] = { B10111, B10101, B10101, B10101, B10111, B00000, B00100, B00100 }; // Setup custom LCD character for bargraph legend
byte fifteen[8] = { B10111, B10100, B10111, B10001, B10111, B00000, B00100, B00100 }; // Setup custom LCD character for bargraph legend
void setup() // Start setup
{
lcd.createChar(5, five); // Create custom character defined above
lcd.createChar(6, ten); // Create custom character defined above
lcd.createChar(7, fifteen); // Create custom character defined above
lcd.begin(20, 4); // set the LCD's columns and rows
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
// Setup static characters on the lcd
lcd.setCursor(0, 2);
lcd.print("Boost:");
lcd.setCursor(0, 3);
lcd.print("Peak :");
lcd.setCursor(14, 3);
lcd.print("Peak");
lcd.setCursor(4, 0);
lcd.write(5);
lcd.setCursor(9, 0);
lcd.write(6);
lcd.setCursor(14, 0);
lcd.write(7);
} // End setup
void loop() // Start loop
{
// some base calculations to get pressures
rawval = analogRead(0); // Read MAP sensor raw value on analog port 0
kpaval = (rawval * (0.005) / (0.022) + 20); // Calculate kpa value from raw value for debugging
boost = ((rawval * (0.005) / (0.022) + 20) * (0.145) - 14.5); // Calculate psi from raw value ***(calculations need proving)***
barboost = (rawval - 357); // Calculate boost value for the bargraph ***(not exact, needs investigation, currently uses higher raw value for a smoother bar but is not exact)
if ( boost <= 0 )
{
digitalWrite(7, HIGH);
}
else
{
digitalWrite(7, LOW);
}
if ( boost >= 17 )
{
digitalWrite(9, HIGH);
}
else
{
digitalWrite(9, LOW);
}
if ( boost >= 0, 03 )
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
if (boost >= 0) // Set condition for bargraph to show above zero (negative numbers cause LCD crash)
{
lcd.setCursor(9, 2);
lcd.print(boost, 1); // Prints the boost figure
lcd.setCursor(14, 2);
lcd.print("Boost "); // Prints 'psi' with a space after it to clear the 'g' off 'inHg'
lbg1.drawValue(barboost, 700); // Draw bar graph from the analog value read with a maxvalue of 500
}
if (boost < 0)
{
lcd.setCursor(9, 2);
lcd.print(vac, 1); // Prints the vacuum figure
vac = boost * -2.036025; // Used 'minus' 2.036025 so that the figure printed wont have a minus symbol in front of it
lcd.setCursor(14, 2);
lcd.print("Vaccum"); // Changes the units to 'inHg' on the lcd
lcd.setCursor(0, 1);
lcd.print(" "); // clears the boost bar if any characters are left after going below 0
}
if (boost > peak) // If current boost is higher than peak, store new value
{
peak = boost ; // Store new peak value in peak memory
}
lcd.setCursor(9, 3);
lcd.print(peak, 1); // Prints the peak value
} //end loop