#include <LiquidCrystal.h>
#include <Encoder.h>
#include <TimerThree.h>
// define simulator or real arduino
#define simulator
// Rotary Encoder Inputs
#define pinOout 8
#define pinEstop 18
#define pinSW 19
#define pinA 20
#define pinB 21
// rotary encoder min/max values
#define maxPWM 100
#define maxPID 150
#define minVAL 0
//define the control modes
#define PWM 0
#define PID 1
volatile int newPosition = 0;
volatile int oldPosition = 0;
volatile int controlMode = PID; // [0 - PWM] [1 - PID]
volatile bool controlChange = false;
// Best Performance: both pins have interrupt capability
Encoder tempEnc(pinA, pinB);
// select the pins used on the LCD panel
#ifdef simulator
// for simulator
LiquidCrystal lcd(13, 12, 7, 6, 5, 4);
#else
// for real arduino
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#endif
void setup() {
//set the interrupt for the estop
pinMode(pinEstop, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pinEstop), estop, LOW);
// use timer 3 to set refresh rate
Timer3.initialize(100000); // 10 FPS
Timer3.attachInterrupt(updateScreen); // call update screen every 0.1 seconds
// lcd initialization
lcd.begin(16, 2); // start the library
lcd.setCursor(0, 0);
lcd.print("Screen Enc V2"); // print a simple message
delay(1000);
lcd.clear();
// set up interrupts for the switch
pinMode(pinSW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pinSW), updateMode, FALLING); // attach the flag setting the controlChange flag
// Call the updateMode
updateMode();
// Setup Serial Monitor1
Serial.begin(9600);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void estop() {
byte stillPressed = digitalRead(pinEstop);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("**ESTOP**"); // print the ESTOP message
while (stillPressed == 0) {
lcd.setCursor(0, 0);
lcd.print("**ESTOP WHILE**"); // print the ESTOP message
stillPressed = digitalRead(pinEstop);
}
lcd.setCursor(0, 1);
lcd.print("AFTER WHILE"); // print the ESTOP message
asm volatile ("jmp 0"); // assembly function for reset
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateMode() {
//controlChange = 1; //set the control change flag - depricated we change the screen once in this interrupt
if (controlMode == PWM) {
controlMode = PID;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMP: PID"); // print the PID message
lcd.setCursor(0, 1);
lcd.print("TEMP: "); // print the PID message
}
else if (controlMode == PID) {
controlMode = PWM;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMP: PWM"); // print the PWM message
lcd.setCursor(0, 1);
lcd.print("POWR: "); // print a simple message
}
controlChange = true;
tempEnc.write(minVAL);
updateScreen();
}
void updateScreen() {
// PID screen loop
if (controlMode == PID) {
newPosition = tempEnc.read() / 4; //read the encoder value and convert to number of detents
if (newPosition != oldPosition || controlChange == true) {
if (newPosition > maxPID) { //check for overflow
newPosition = maxPID;
tempEnc.write(maxPID * 4);
}
else if (newPosition < minVAL) { // check for underflow
newPosition = minVAL;
tempEnc.write(minVAL);
}
oldPosition = newPosition;
lcd.setCursor(6, 1); // move to the temp position of the second line
lcd.print(" "); // Clear the old value to prevent stray characters
lcd.setCursor(6, 1); // move to the temp position of the second line
lcd.print(newPosition); // print the current value
lcd.print("*C"); // print the unit
controlChange = false;
}
}
//PWM screen loop
else if (controlMode == PWM) {
newPosition = tempEnc.read() / 4; //read the encoder value and convert to number of detents
if (newPosition != oldPosition || controlChange == true) {
if (newPosition > maxPWM) { //check for overflow
newPosition = maxPWM;
tempEnc.write(maxPWM * 4);
}
else if (newPosition < minVAL) { // check for underflow
newPosition = minVAL;
tempEnc.write(minVAL);
}
oldPosition = newPosition;
lcd.setCursor(6, 1); // move to the temp position of the second line
lcd.print(" "); // Clear the old value to prevent stray characters
lcd.setCursor(6, 1); // move to the temp position of the second line
lcd.print(newPosition); // print the current value
lcd.print("%"); // print the unit
controlChange = false;
}
}
}
void loop() {
//updateScreen();
delay(10000); // delay for 10 seconds
}