Imagine | Develop | Create
#include <LiquidCrystal.h> #include <EEPROM.h> #define maxPressure 100 #define sensorMultiplier 0.3 #define sensorOffSet -30 #define outputPin A1 #define sensorPin A5 #define upButtonPin 3 #define downButtonPin 10 double sensorValue; double pressure; byte setPressure = 0; unsigned long timeLastSet = 0; unsigned long timeLastOn = 0; bool timeout = false; LiquidCrystal lcd(2, 4, 5, 6, 7, 8); void setup() { lcd.begin(16, 2); Serial.begin(9600); Load(); changed(); pinMode(outputPin,OUTPUT); pinMode(sensorPin,INPUT); pinMode(upButtonPin,INPUT); pinMode(downButtonPin,INPUT); } void loop() { readSensor(); control(); Set(); displayreading(); } #define samplecount 20 int samples[samplecount]; void readSensor() { int indexI = (millis()/(1000/samplecount))%(samplecount); samples[indexI] = analogRead(sensorPin); for(int i = 0; i<samplecount;i++) { sensorValue += samples[i]; } sensorValue = sensorValue/samplecount; pressure = sensorMultiplier*sensorValue+sensorOffSet; } void control() { if(timeLastOn+120000 < millis()) { timeout=false; } if(pressure < setPressure*0.2) { timeLastOn = millis(); digitalWrite(outputPin,HIGH); timeout=false; } else if(pressure > setPressure || timeout) { digitalWrite(outputPin,LOW); timeout=true; } else if(pressure < setPressure*0.6) { timeLastOn = millis(); digitalWrite(outputPin,HIGH); } } void displayreading() { lcd.setCursor(0,0); lcd.print((int)pressure); lcd.print(" PSI "); lcd.setCursor(0,1); lcd.print("set: "); lcd.print(setPressure); lcd.print(" PSI "); } void Set() { if(timeLastSet+500>millis()) return; if(digitalRead(downButtonPin) == HIGH) { setPressure -= maxPressure*0.05; changed(); } if(digitalRead(upButtonPin) == HIGH) { setPressure += maxPressure*0.05; changed(); } } void changed() { timeLastSet = millis(); if(setPressure >= maxPressure) setPressure = maxPressure; if(setPressure <= 0) setPressure = 0; Save(); } int address = 0; void Save() { EEPROM.write(address, setPressure); } void Load() { setPressure = EEPROM.read(address); }