If I try to change or move or put another code into then I get all errors on everything related to Liquid Crystal this is probably a bit complicated for me
Can anyone point me in straight track how I replaced Liquid Crystal LCD to use a mean WidgetLCD Blynk app.
Know Iâm asking a lot but is quick to learn and now Iâve not asked a lot how to do this here so I would be grateful if to any one wanted to see the code for me and help
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Sketch generator: http://examples.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
************************************************
*
* This example shows how to use Arduino Ethernet shield (W5100)
* to connect your project to Blynk.
* Feel free to apply it to any other example. It's simple!
*
* NOTE: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
* DON'T use them in your sketch directly!
*
* WARNING: If you have an SD card, you may need to disable it
* by setting pin 4 to HIGH. Read more here:
* https://www.arduino.cc/en/Main/ArduinoEthernetShield
*
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <inttypes.h>
#include <Wire.h>
//////////////////////////////////////
// Tire pressure : 1 bit = 0.01 PSI //
// Tank pressure : 1 bit = 0.01 PSI //
//////////////////////////////////////
//////// GLOBAL PARAMETERS /////
// Tire pressure control parameters:
const uint8_t TOLERANCE = 5; //[Tire pressure storage unit] set point tolerance
const uint16_t ALL_WITHIN_TOL_WAIT_TIME = 8000; //[ms]
const uint8_t TIME_BETW_MEAS = 50; //[ms]
const uint8_t NUM_OF_MEASUREMENTS_SHORT_FILTER = 6; //Number of measurements averaged
const uint8_t NUM_OF_MEASUREMENTS_LONG_FILTER = 50; //Number of measurements averaged
const uint16_t MAX_DEFLATION_TIME = 6000; //[ms]
const uint16_t MAX_INFLATION_TIME = 6000; //[ms]
const uint16_t WITHIN_TOL_SET_POINT_THRESHOLD = 10; //[raw set point bits]
const uint8_t STAND_BY_THRESHOLD = 10; //Stand by threshold
const uint8_t STAND_BY_MULTIPLAYER = 4; //Stand by multiplier
const uint16_t SETTLING_TIME = 100; //[ms] pressure settling time
const uint16_t VALVE_OPENING_TIME = 7; //[ms]
const float BITS_TO_PSIUNIT_MXP4250 = 3.8425; // 1 bit = 0,038425 psi
const int16_t FILTER_THRESHOLD = 100; // raw ADC value
const uint16_t FRONT_AXLE_OFFSET_ADJUST_TIME_LENGTH = 4000;
const uint16_t FRONT_AXLE_OFFSET_LOWER_POT_LIMIT = 50;
const uint16_t FRONT_AXLE_OFFSET_HIGER_POT_LIMIT = 950;
byte LCD_SYMBOLS[2][8] = { {B00100, B01110, B11111, B00100, B00100, B00100, B00100,}, {B00100, B00100, B00100, B00100, B11111, B01110, B00100,} };
// Tank parameters:
const uint16_t TANKPRESSURE_BLINK_LIMIT = 3000; //[Tank pressure storage unit]
const uint16_t TANK_PRESS_BLINK_DELAY = 700; //[ms]
const float BITS_TO_PSIUNIT_MXP5700 = 11.0135; // 1 bit = 0,110135 psi
const uint16_t OFFSET_MPX5700 = 35; // 0 - 1024, raw ADC unit
// Front panel parameters:
const uint16_t POT_NONLINEAR_THRES = 700; // range (0-1023)
const uint16_t FRONT_PANEL_TIME_THRES_PERIOD = 250;
uint16_t MAX_POT_COUNT = 1020; //(Not constant) Max output from POT ATH hĂ©r ĂŸarf aĂ° lĂĄta lesa Ășr EEPROM
uint16_t MIN_POT_COUNT = 0; //(Not constant) Min output from POT ATH hĂ©r ĂŸarf aĂ° lĂĄta lesa Ășr EEPROM
const uint8_t SETPOINT_MAXIMUM_PSI = 35;
char LCD_INIT_STRING1[] = "thorhalb v1.0";
char LCD_INIT_STRING2[] = " ";
char LCD_INIT_STRING3[] = " -.- -.-";
char LCD_INIT_STRING4[] = " -.- -.-";
char LCD_INIT_STRING5[] = "Enter front axle";
char LCD_INIT_STRING6[] = "offset: psi";
const int LCD_X[] = {6, 12, 6, 12}; // The position coordinates of the tire pressure on the screen
const int LCD_Y[] = {0, 0, 1, 1};
const uint8_t PIN_GREEN_LED = A3;
const uint8_t PIN_ALL_INDI = 0;
const uint8_t PIN_POT = A5;
// Control pins deceleration
const uint8_t PIN_VALVE0 = 9;
const uint8_t PIN_VALVE1 = 3;
const uint8_t PIN_VALVE2 = 5;
const uint8_t PIN_VALVE3 = 6;
const uint8_t PIN_VALVE4 = 7;
const uint8_t PIN_VALVE5 = 8;
const uint8_t PIN_PUMP = 1;
// Sensors
const uint8_t PIN_MPX4250 = A2;
const uint8_t PIN_MPX5700 = A1;
// EEPROM and control parameters
const uint8_t SINGLE_CHANNEL_DEFLATION_PARAM_ADDRESS = 0;
const uint8_t SINGLE_CHANNEL_INFLATION_PARAM_ADDRESS = 4;
const uint8_t FRONT_CHANNELS_DEFLATION_PARAM_ADDRESS = 8;
const uint8_t FRONT_CHANNELS_INFLATION_PARAM_ADDRESS = 12;
const uint8_t REAR_CHANNELS_DEFLATION_PARAM_ADDRESS = 16;
const uint8_t REAR_CHANNELS_INFLATION_PARAM_ADDRESS = 20;
const uint8_t ALL_CHANNELS_DEFLATION_PARAM_ADDRESS = 24;
const uint8_t ALL_CHANNELS_INFLATION_PARAM_ADDRESS = 28;
// Other constants:
const uint16_t SERIAL_BAUDRATE = 9600;
////////// GLOBAL VARIABLES /////
// Enum:
enum valveControlCommand {CLOSE_ALL, MEASURE, INFLATE, DEFLATE, CALIBRATION};
enum tireControlState {firstMeasurementOngoing, allWithinTolWaiting, firstMeasurementDone, inflationDeflationDone, secondMeasurementOngoing, secondMeasurementDone};
enum LCD_SYMBOL {ARROW_UP, ARROW_DOWN};
// Tire pressure control - global variables:
uint8_t activeTire = 0; //Four tires: 0-3
tireControlState tireState = allWithinTolWaiting; //Enum
uint16_t tireSensorOffset = 0; //Raw ADC value
int16_t setPoint = 0; //[Tire pressure storage unit]
uint16_t currentPotRaw = 0; //Current set point POT raw value
int16_t withinTolSetPoint = 0; //[Tire pressure storage unit]
int16_t summedPressure = 0; //Temp variable - used to accumulate measurements
boolean isAllWithinTol = false; //Are all channels within tolerance
uint8_t allWithinTolCounter = 0; //How many channels have been found in row within tolerance
uint8_t numOfMeasCounter = 0; //Current number of measurements taken
uint32_t TireTimeThres = 0; //Tire control time threshold
boolean isChBelow10PSI[4] = {true, true, true, true};
uint16_t firstMeasurementPressure = 0;
uint16_t secondMeasurementPressure = 0;
boolean isDeflating = false;
boolean isInflating = true;
uint16_t deflationTime = 0;
uint16_t inflationTime = 0;
boolean isSetPointChanged = true;
uint16_t numberOfMeasurementsInUse = NUM_OF_MEASUREMENTS_SHORT_FILTER;
int8_t frontAxleOffset = 0; // [psi*100] Range -4 to 4 psi ( -400 to 400)
int16_t maxPressureReading = 0;
int16_t minPressureReading = 1023;
uint8_t isShortFilterActive = true;
// Tank pressure control - global variables:
boolean isValve5Closed = true;
int16_t tankPress = 0; //[Tank pressure storage unit] Current tank pressure
uint32_t tankPressureLCDTimeThres = 0; //Tank pressure blink time threshold
uint32_t calibrationTankTimeThres = 0;
boolean tankBlink = false;
// EEPROM and control parameters - global variables:
uint16_t singleChannelDeflationParam = 11000;
uint16_t singleChannelInflationParam = 1200;
uint16_t frontChannelsDeflationParam = 11000;
uint16_t frontChannelsInflationParam = 1200;
uint16_t rearChannelsDeflationParam = 11000;
uint16_t rearChannelsInflationParam = 1200;
uint16_t allChannelsDeflationParam = 11000;
uint16_t allChannelsInflationParam = 1200;
uint32_t EEPROMTimeThres = 20000;
// Other - global variables:
uint32_t backgroundTasksTimeThres = 0; //Front panel time threshold
uint32_t blinkLEDTimeThres = 0; //Front panel LED blink time threshold
boolean isIndependentEnabled = true; //Independent channels enabled
uint16_t counter = 0; //Temp variable for performance test
uint16_t channel0 = 0;
uint16_t channel1 = 0;
uint16_t channel2 = 0;
uint16_t channel3 = 0;
// Global objects:
LiquidCrystal lcd(2, 3, 5, 6, 7); //Can not use this pin now because they are used by others now and so I need to use Blynk LCD
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "3295d2122335428a8a8877a04a64367b";
#define W5100_CS 10
#define SDCARD_CS 4
void setup()
{
Serial.begin(SERIAL_BAUDRATE);
pinMode(PIN_ALL_INDI, INPUT);
pinMode(PIN_GREEN_LED, OUTPUT);
pinMode(PIN_VALVE0, OUTPUT);
pinMode(PIN_VALVE1, OUTPUT);
pinMode(PIN_VALVE2, OUTPUT);
pinMode(PIN_VALVE3, OUTPUT);
pinMode(PIN_VALVE4, OUTPUT);
pinMode(PIN_VALVE5, OUTPUT);
pinMode(PIN_PUMP, OUTPUT);
lcdInit1();
tireSensorOffset = tireOffsetCalibration();
lcdInit2();
loadParamFromEEPROM();
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
Blynk.begin(auth, IPAddress(192,168,10,3)); // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
}
void loop()
{
if(backgroundTasksTimeThres < millis())
{
backgroundTasks();
}
if(TireTimeThres < millis())
{
tirePressureControl();
}
if(tankPressureLCDTimeThres < millis())
{
printTankPressureLCD();
}
Blynk.run();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void backgroundTasks()
{
backgroundTasksTimeThres = millis() + FRONT_PANEL_TIME_THRES_PERIOD;
readTankPress();
updatingSetPoint();
updatingALLOrIndividualSwitch();
updatingFrontPanelLED();
// Method to capture set point changes in all-within-tolerance mode where the period is very slow
if (isAllWithinTol && (abs(setPoint - withinTolSetPoint) > WITHIN_TOL_SET_POINT_THRESHOLD))
{
TireTimeThres = 0; // Measure the next channel immediately
withinTolSetPoint = setPoint; // So that this is only caught once - not four times
}
// Tank functions - only read sensor when tankValve is closed
if (isValve5Closed == true)
{
readTankPress();
}
}
void tirePressureControl()
{
switch (tireState)
{
case firstMeasurementOngoing:
takeOneMoreMeasurement();
if (numOfMeasCounter >= numberOfMeasurementsInUse)
{
tireState = firstMeasurementDone;
}
break;
case firstMeasurementDone:
if (maxPressureReading - minPressureReading > FILTER_THRESHOLD && isShortFilterActive == true)
{
isShortFilterActive = false;
tireState = firstMeasurementOngoing;
numberOfMeasurementsInUse = NUM_OF_MEASUREMENTS_LONG_FILTER;
}
else
{
valveControl(CLOSE_ALL);
isShortFilterActive = true;
maxPressureReading = 0;
minPressureReading = 1023;
firstMeasurementPressure = calcAverage();
numberOfMeasurementsInUse = NUM_OF_MEASUREMENTS_SHORT_FILTER;
displayTirePress(firstMeasurementPressure);
decideWhatToDo(firstMeasurementPressure);
updateAllWithinTol();
}
break;
case allWithinTolWaiting:
valveControl(MEASURE);
TireTimeThres = millis() + SETTLING_TIME;
tireState = firstMeasurementOngoing;
break;
case inflationDeflationDone:
if (activeTire == 60)
{
tireState = firstMeasurementOngoing;
}
else
{
tireState = secondMeasurementOngoing;
}
valveControl(MEASURE);
TireTimeThres = millis() + SETTLING_TIME;
break;
case secondMeasurementOngoing:
takeOneMoreMeasurement();
if (numOfMeasCounter >= numberOfMeasurementsInUse)
{
tireState = secondMeasurementDone;
}
break;
case secondMeasurementDone:
if (maxPressureReading - minPressureReading > FILTER_THRESHOLD && isShortFilterActive == true)
{
isShortFilterActive = false;
tireState = secondMeasurementOngoing;
numberOfMeasurementsInUse = NUM_OF_MEASUREMENTS_LONG_FILTER;
}
else
{
isShortFilterActive = true;
maxPressureReading = 0;
minPressureReading = 1023;
secondMeasurementPressure = calcAverage();
numberOfMeasurementsInUse = NUM_OF_MEASUREMENTS_SHORT_FILTER;
displayTirePress(secondMeasurementPressure);
changeToNextChannel();
updatingControlParameters();
valveControl(MEASURE);
TireTimeThres = millis() + SETTLING_TIME;
tireState = firstMeasurementOngoing;
}
break;
}
}
////////////// Support functions: ///////////////////////////
void readTankPress()
{
tankPress = (float)(analogRead(PIN_MPX5700) - OFFSET_MPX5700) * BITS_TO_PSIUNIT_MXP5700;
}
void changeToNextChannel()
{
// 0 - 3 = independent channels, 4 = front (0-1 ch), 5 rear (2-3 ch), 6 all channels
if (isIndependentEnabled == true)
{
activeTire = (activeTire + 1) % 4;
}
else if (frontAxleOffset == 0)
{
activeTire = 6;
}
else
{
if (activeTire == 5)
{
activeTire = 4;
}
else
{
activeTire = 5;
}
}
}
void printTankPressureLCD()
{
tankPressureLCDTimeThres = millis() + TANK_PRESS_BLINK_DELAY;
lcd.setCursor(0, 1);
if (tankPress > TANKPRESSURE_BLINK_LIMIT || tankBlink == false)
{
//Printing space if needed
if (tankPress < 995)
{
lcd.print(" ");
}
else if (tankPress < 9995)
{
lcd.print(" ");
}
// Print number
lcd.print((tankPress + 5) / 100); // 5 is so the rounding will be correct and 100 is because of the way the pressure is stored
tankBlink = true;
}
else
{
tankBlink = false;
lcd.print(" ");
}
}
void clearAllArrowsAndM()
{
for (int i = 0; i < 4; i++)
{
lcd.setCursor(LCD_X[i] - 1, LCD_Y[i]);
if (isChBelow10PSI[i])
{
lcd.print(" ");
}
else
{
lcd.print(" ");
}
}
}
void printArrowsAndM(int command)
{
clearAllArrowsAndM();
uint8_t startChannel = 0;
uint8_t endChannel = 0;
switch (activeTire)
{
case 0:
case 1:
case 2:
case 3:
startChannel = activeTire;
endChannel = activeTire;
break;
case 4:
startChannel = 0;
endChannel = 1;
break;
case 5:
startChannel = 2;
endChannel = 3;
break;
case 6:
startChannel = 0;
endChannel = 3;
break;
}
for (uint8_t i = startChannel; i < endChannel + 1; i++)
{
lcd.setCursor(LCD_X[i] - 1, LCD_Y[i]);
if (isChBelow10PSI[i])
{
lcd.print(" ");
}
switch (command)
{
case MEASURE:
lcd.write("M");
break;
case INFLATE:
lcd.write(byte(ARROW_UP));
break;
case DEFLATE:
lcd.write(byte(ARROW_DOWN));
break;
}
}
}
void updatingSetPoint()
{
uint16_t newPotRaw = analogRead(PIN_POT);
if (abs(newPotRaw - currentPotRaw) > 10 ) // POT hysteresis
{
uint16_t newSetPoint = potToPSI(newPotRaw);
if ( newSetPoint != setPoint)
{
setPoint = newSetPoint;
currentPotRaw = newPotRaw;
isSetPointChanged = true;
lcd.setCursor(0, 0);
if (setPoint < 995)
{
lcd.print(" "); // This is for alignment of decimal point on screen
}
lcd.print(((float)setPoint) / 100, 1);
}
}
}
uint16_t potToPSI(uint16_t potRaw)
{
uint16_t output = 0;
if (potRaw < POT_NONLINEAR_THRES)
{
output = map(potRaw, MIN_POT_COUNT, POT_NONLINEAR_THRES - 1, 0, 499);
}
else
{
output = map(potRaw, POT_NONLINEAR_THRES, MAX_POT_COUNT, 500, SETPOINT_MAXIMUM_PSI * 100);
}
// Round of numbers higher than 500 (5 PSI)
if (output > 500)
{
output = (output / (int)100) * (int)100;
}
else
{
output = (output / (int)50) * (int)50;
}
if (output < 50)
{
output = 25;
}
return output;
}
void updatingALLOrIndividualSwitch()
{
boolean allIndiSwitchEnabled = digitalRead(PIN_ALL_INDI);
if (allIndiSwitchEnabled && !isIndependentEnabled)
{
activeTire = 0;
isIndependentEnabled = true;
TireTimeThres = 0;
allWithinTolCounter = 0;
isAllWithinTol = 0;
valveControl(CLOSE_ALL);
}
else if (!allIndiSwitchEnabled && isIndependentEnabled)
{
activeTire = 6;
isIndependentEnabled = false;
TireTimeThres = 0;
allWithinTolCounter = 0;
isAllWithinTol = 0;
valveControl(CLOSE_ALL);
}
}
void updatingFrontPanelLED()
{
if (isAllWithinTol)
{
digitalWrite(PIN_GREEN_LED, 1);
}
else
{
digitalWrite(PIN_GREEN_LED, 0);
}
}
void takeOneMoreMeasurement()
{
uint16_t temp = analogRead(PIN_MPX4250) - tireSensorOffset;
if (temp > maxPressureReading)
{
maxPressureReading = temp;
}
if (temp < minPressureReading)
{
minPressureReading = temp;
}
summedPressure = summedPressure + temp;
numOfMeasCounter += 1;
TireTimeThres = millis() + TIME_BETW_MEAS;
}
uint16_t calcAverage()
{
numOfMeasCounter = 0;
int16_t currentPressure = (float)(summedPressure / numberOfMeasurementsInUse) * BITS_TO_PSIUNIT_MXP4250;
if ( currentPressure < 0 )
{
currentPressure = 0;
}
summedPressure = 0;
return currentPressure;
}
void displayTirePress(uint16_t currentPressure)
{
float printOutPress = (float)currentPressure / (float)100;
if (currentPressure > 37000) // This is to eliminate "negative" pressure
{
currentPressure = 0;
}
uint8_t isCurrentPressBelow10psi = currentPressure < 995;
uint8_t startChannel = 0;
uint8_t endChannel = 0;
switch (activeTire)
{
case 0:
channel0 = currentPressure;
startChannel = activeTire;
endChannel = activeTire;
isChBelow10PSI[activeTire] = isCurrentPressBelow10psi;
break;
case 1:
channel1 = currentPressure;
startChannel = activeTire;
endChannel = activeTire;
isChBelow10PSI[activeTire] = isCurrentPressBelow10psi;
break;
case 2:
channel2 = currentPressure;
startChannel = activeTire;
endChannel = activeTire;
isChBelow10PSI[activeTire] = isCurrentPressBelow10psi;
break;
case 3:
channel3 = currentPressure;
startChannel = activeTire;
endChannel = activeTire;
isChBelow10PSI[activeTire] = isCurrentPressBelow10psi;
break;
case 4:
channel0 = currentPressure;
channel1 = currentPressure;
startChannel = 0;
endChannel = 1;
isChBelow10PSI[0] = isCurrentPressBelow10psi;
isChBelow10PSI[1] = isCurrentPressBelow10psi;
break;
case 5:
channel2 = currentPressure;
channel3 = currentPressure;
startChannel = 2;
endChannel = 3;
isChBelow10PSI[2] = isCurrentPressBelow10psi;
isChBelow10PSI[3] = isCurrentPressBelow10psi;
break;
case 6:
channel0 = currentPressure;
channel1 = currentPressure;
channel2 = currentPressure;
channel3 = currentPressure;
startChannel = 0;
endChannel = 3;
isChBelow10PSI[0] = isCurrentPressBelow10psi;
isChBelow10PSI[1] = isCurrentPressBelow10psi;
isChBelow10PSI[2] = isCurrentPressBelow10psi;
isChBelow10PSI[3] = isCurrentPressBelow10psi;
break;
}
for (uint8_t i = startChannel; i < endChannel + 1; i++)
{
lcd.setCursor(LCD_X[i], LCD_Y[i]);
if (printOutPress < 9.95) // This is for alignment of decimal point on screen
{
lcd.print(" ");
}
lcd.print(abs(printOutPress), 1);
}
}
void updateAllWithinTol()
{
uint8_t numberOfChannelsInUse = 0;
switch (activeTire)
{
case 0:
case 1:
case 2:
case 3:
numberOfChannelsInUse = 3;
break;
case 4:
case 5:
numberOfChannelsInUse = 2;
break;
case 6:
numberOfChannelsInUse = 1;
break;
}
if (allWithinTolCounter >= numberOfChannelsInUse && !isAllWithinTol)
{
isAllWithinTol = true;
withinTolSetPoint = setPoint;
isSetPointChanged = false;
}
else if (allWithinTolCounter < numberOfChannelsInUse)
{
isAllWithinTol = false;
}
}
uint16_t calculateDeflationTime(uint16_t currentPressure)
{
uint16_t deflationParamInUse = 0;
switch (activeTire)
{
case 0:
case 1:
case 2:
case 3:
deflationParamInUse = singleChannelDeflationParam;
break;
case 4:
deflationParamInUse = frontChannelsDeflationParam;
break;
case 5:
deflationParamInUse = rearChannelsDeflationParam;
break;
case 6:
deflationParamInUse = allChannelsDeflationParam;
break;
}
uint16_t timeMilliSec = ((deflationParamInUse * 10) / log( (float)currentPressure / (float)setPoint )) + VALVE_OPENING_TIME;
if (timeMilliSec > MAX_DEFLATION_TIME)
{
timeMilliSec = MAX_DEFLATION_TIME;
}
return timeMilliSec;
}
uint16_t calculateInflationTimeMS(uint16_t currentPressure)
{
uint16_t inflationParamInUse = 0;
switch (activeTire)
{
case 0:
case 1:
case 2:
case 3:
inflationParamInUse = singleChannelInflationParam;
break;
case 4:
inflationParamInUse = frontChannelsInflationParam;
break;
case 5:
inflationParamInUse = rearChannelsInflationParam;
break;
case 6:
inflationParamInUse = allChannelsInflationParam;
break;
}
uint16_t timeMilliSec = ((float)( setPoint - currentPressure ) * ( inflationParamInUse / 100 ) ) + VALVE_OPENING_TIME;
if (timeMilliSec > MAX_INFLATION_TIME)
{
timeMilliSec = MAX_INFLATION_TIME;
}
return timeMilliSec;
}
void decideWhatToDo(int16_t currentPressure)
{
int16_t difference = currentPressure - setPoint;
if (activeTire == 0 || activeTire == 1 || activeTire == 4)
{
difference = difference - frontAxleOffset;
}
if (abs(difference) > TOLERANCE) // Last measurement not within tolerance
{
allWithinTolCounter = 0;
if (difference > 0)
{
valveControl(DEFLATE);
deflationTime = calculateDeflationTime(currentPressure);
TireTimeThres = millis() + deflationTime;
}
else if (1) //tankPress > 3000) // inflation only performed if tank press > 30 psi
{
valveControl(INFLATE);
inflationTime = calculateInflationTimeMS(currentPressure);
TireTimeThres = millis() + inflationTime;
}
tireState = inflationDeflationDone;
}
else // Last measurement within tolerance
{
allWithinTolCounter += 1;
changeToNextChannel();
if (isAllWithinTol && allWithinTolCounter > STAND_BY_THRESHOLD)
{
closeAllValves();
TireTimeThres = millis() + (STAND_BY_MULTIPLAYER * ALL_WITHIN_TOL_WAIT_TIME);
tireState = allWithinTolWaiting;
}
else if (isAllWithinTol)
{
closeAllValves();
if (activeTire == 6)
{
openActiveTireValve();
}
TireTimeThres = millis() + ALL_WITHIN_TOL_WAIT_TIME;
tireState = allWithinTolWaiting;
}
else
{
valveControl(MEASURE);
TireTimeThres = millis() + SETTLING_TIME;
tireState = firstMeasurementOngoing;
}
}
}
void openActiveTireValve()
{
switch (activeTire)
{
case 0:
digitalWrite(PIN_VALVE1, HIGH);
break;
case 1:
digitalWrite(PIN_VALVE2, HIGH);
break;
case 2:
digitalWrite(PIN_VALVE3, HIGH);
break;
case 3:
digitalWrite(PIN_VALVE4, HIGH);
break;
case 4:
digitalWrite(PIN_VALVE1, HIGH);
digitalWrite(PIN_VALVE2, HIGH);
break;
case 5:
digitalWrite(PIN_VALVE3, HIGH);
digitalWrite(PIN_VALVE4, HIGH);
break;
case 6:
digitalWrite(PIN_VALVE1, HIGH);
digitalWrite(PIN_VALVE2, HIGH);
digitalWrite(PIN_VALVE3, HIGH);
digitalWrite(PIN_VALVE4, HIGH);
break;
}
}
void closeAllValves()
{
digitalWrite(PIN_VALVE0, LOW);
digitalWrite(PIN_VALVE1, LOW);
digitalWrite(PIN_VALVE2, LOW);
digitalWrite(PIN_VALVE3, LOW);
digitalWrite(PIN_VALVE4, LOW);
digitalWrite(PIN_VALVE5, LOW);
}
void valveControl(int command)
{
printArrowsAndM(command);
closeAllValves();
isValve5Closed = true;
switch (command)
{
case CLOSE_ALL:
// Empty on purpose
break;
case MEASURE:
openActiveTireValve();
break;
case INFLATE:
isInflating = true;
openActiveTireValve();
digitalWrite(PIN_VALVE5, HIGH);
isValve5Closed = false;
break;
case DEFLATE:
isDeflating = true;
openActiveTireValve();
digitalWrite(PIN_VALVE0, HIGH);
break;
case CALIBRATION:
digitalWrite(PIN_VALVE0, HIGH);
break;
}
}
uint16_t tireOffsetCalibration()
{
valveControl(CALIBRATION);
writeToLCD(LCD_INIT_STRING1, LCD_INIT_STRING2);
delay(2 * SETTLING_TIME);
uint16_t temp = 0;
for (int8_t i = 0; i < NUM_OF_MEASUREMENTS_SHORT_FILTER; i = i + 1)
{
temp = temp + analogRead(PIN_MPX4250);
delay(TIME_BETW_MEAS);
}
uint16_t tireSensorOffset = (temp / NUM_OF_MEASUREMENTS_SHORT_FILTER);
valveControl(CLOSE_ALL);
writeToLCD(LCD_INIT_STRING1, LCD_INIT_STRING2);
delay(2 * SETTLING_TIME);
return tireSensorOffset;
}
void lcdInit1()
{
lcd.begin(16, 2);
lcd.createChar(0, LCD_SYMBOLS[0]);
lcd.createChar(1, LCD_SYMBOLS[1]);
lcd.clear();
writeToLCD(LCD_INIT_STRING1, LCD_INIT_STRING2);
}
void lcdInit2()
{
writeToLCD(LCD_INIT_STRING3, LCD_INIT_STRING4);
}
void writeToLCD(char string1[], char string2[])
{
lcd.setCursor(0, 0);
lcd.print(string1);
lcd.setCursor(0, 1);
lcd.print(string2);
}
void writeParamToEEPROM()
{
// Unfinished function
}
void loadParamFromEEPROM()
{
singleChannelDeflationParam = EEPROM.read(SINGLE_CHANNEL_DEFLATION_PARAM_ADDRESS);
singleChannelInflationParam = EEPROM.read(SINGLE_CHANNEL_INFLATION_PARAM_ADDRESS);
frontChannelsDeflationParam = EEPROM.read(FRONT_CHANNELS_DEFLATION_PARAM_ADDRESS);
frontChannelsInflationParam = EEPROM.read(FRONT_CHANNELS_DEFLATION_PARAM_ADDRESS);
rearChannelsDeflationParam = EEPROM.read(REAR_CHANNELS_DEFLATION_PARAM_ADDRESS);
rearChannelsInflationParam = EEPROM.read(REAR_CHANNELS_DEFLATION_PARAM_ADDRESS);
allChannelsDeflationParam = EEPROM.read(ALL_CHANNELS_DEFLATION_PARAM_ADDRESS);
allChannelsInflationParam = EEPROM.read(ALL_CHANNELS_DEFLATION_PARAM_ADDRESS);
}
void updatingControlParameters()
{
if (isDeflating && isInflating)
{
// Error: should not happen
}
else if (isDeflating && deflationTime > 0 && (firstMeasurementPressure - secondMeasurementPressure) > 0)
{
uint16_t newDeflationParam = deflationTime / log( (float)firstMeasurementPressure / (float)secondMeasurementPressure) / 10;
switch (activeTire)
{
case 0:
case 1:
case 2:
case 3:
singleChannelDeflationParam = newDeflationParam;
break;
case 4:
frontChannelsDeflationParam = newDeflationParam;
break;
case 5:
rearChannelsDeflationParam = newDeflationParam;
break;
case 6:
allChannelsDeflationParam = newDeflationParam;
break;
}
}
else if (isInflating && inflationTime > 0 && (secondMeasurementPressure > firstMeasurementPressure))
{
uint16_t newInflationParam = inflationTime / (float)(secondMeasurementPressure - firstMeasurementPressure) * 100;
switch (activeTire)
{
case 0:
case 1:
case 2:
case 3:
singleChannelInflationParam = newInflationParam;
break;
case 4:
frontChannelsInflationParam = newInflationParam;
break;
case 5:
rearChannelsInflationParam = newInflationParam;
break;
case 6:
allChannelsInflationParam = newInflationParam;
break;
}
}
isDeflating = false;
isInflating = false;
if ( EEPROMTimeThres < millis() )
{
EEPROMTimeThres = millis() + 5000;
writeParamToEEPROM();
}
}
int16_t updateFrontAxleOffset()
{
closeAllValves();
writeToLCD(LCD_INIT_STRING5,LCD_INIT_STRING6);
int16_t offset = 0;
for (uint8_t i = 0; i < 20; i++)
{
offset = map(analogRead(PIN_POT), 0, 1023, -8, 8) * 50; // range from -4 to 4 psi with 0,5 psi resolution
lcd.setCursor(8, 1);
if (offset >= 0)
{
lcd.print(" ");
}
lcd.print((float)offset / 100, 1);
delay(250);
}
return offset;
}