#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h>
//LCD
LiquidCrystal_I2C lcd(0x27 , 16, 2); //LCD I2C Screen is 0x27, 16 characters and 2 rows
//Pins
const int incButton=2; //increase button
const int decButton=3; //decrease button
const int FanPin=5; //this LED represents the fan and would be connected via a MOSFET
//setpoint control
float setPWM=0.0; //default initial PWM on switch-on
float percent=0.0;
const float step=255.0/10.0; // the amount each button press changes the set PWM
const float minPWM=0.0; // limit the settings to a minimum of 0
const float maxPWM=255.0; // limit the settings to a maximum of 255
//PWM limits. These are the maximum and minimum PWM outputs for the peltier and fan
//for debouncing of the set pins as otherwise setting the PWM is difficult
bool lastIncState=HIGH;
bool lastDecState=HIGH;
void setup() {
// put your setup code here, to run once:
pinMode(incButton, INPUT_PULLUP);
pinMode(decButton, INPUT_PULLUP);
pinMode(FanPin, OUTPUT);
analogWrite(FanPin,0);
lcd.init();
lcd.backlight();
}
//main loop - this calls all the subroutines below it.
void loop() {
setThePWM(); //calls the routine that sets the PWM
updateFans(setPWM); //sets the cooling fan
updateLCD(setPWM, percent); //calls the routine that updates the display
delay(100); //adds a 0.1 second delay
}
void setThePWM() { // this subroutine uses the up and down buttons to set the desired PWM
bool incState=digitalRead(incButton);
bool decState=digitalRead(decButton);
// increase value
if (incState == LOW && lastIncState==HIGH) { //decrease button is pressed
setPWM+= step; }
if (decState==LOW && lastDecState==HIGH) { //increase button is pressed
setPWM-=step; }
setPWM=constrain(setPWM, minPWM,maxPWM); //constrains to a fixed min and max PWM
lastIncState=incState;
lastDecState=decState;
}
// Proportional control of the peltier and fans.
void updateFans (float setPWM) {
analogWrite(FanPin,setPWM);
}
void updateLCD(float setPWM, float percent) { //this sends data to the LCD
percent=setPWM/maxPWM*100;
lcd.setCursor(0,0);
lcd.print("Set: ");
lcd.print(setPWM,1);
lcd.print(" PWM ");
lcd.setCursor(0,1); // displays the percentage of the PWM (0-100%)
lcd.print("Now: ");
lcd.print(percent,1);
lcd.print(" % ");
}