Hey all! I've been up all night trying to figure this out- I should mention my skill set is novice and this is the most complex thing I've attempted yet.
I'm programming some code for an induction annealer using the Arduino Uno. So far I've managed to program a basic version- set the time with a rotary encoder and press a button to start the countdown. I have the encoder going through interrupt because I was getting some jitter/false inputs if I spun the encoder fast.
What I'd like to do is have the ability to cancel the countdown/annealing process, and I'd also like to be able to display that countdown with an lcd.print
I've figured out that using a delay will prevent me from doing that. I dived into researching millis and I'm completely lost! I've searched over and over on google and haven't quite found what I'm attempting to do with millis.
I have an LED acting as a visual for what will be a SSR (Solid State Relay) and a small relay in there too because I was having fun with it. Other than that I probably have stuff in there I don't need, I've just been trying to figure it all out.
Here's the code
#include <LiquidCrystal.h>
#define CLK 2
#define DT 3
const int ledPin = 13;
const int buttonPin = 10;
const int relay = 9;
float Time = 5000;
const int rs = 12, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir = "";
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(relay, OUTPUT);
lcd.begin(16, 2);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
lastStateCLK = digitalRead(CLK);
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void loop() {
int startReadingButton = digitalRead(buttonPin);
displayCurrentTime();
if (startReadingButton == HIGH) {
displayCurrentTime();
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("anneal");
digitalWrite(ledPin, HIGH);
digitalWrite(relay, HIGH);
delay(Time);
digitalWrite(ledPin, LOW);
digitalWrite(relay, LOW);
} else {
displayCurrentTime();
lcd.setCursor(0, 2);
lcd.print("no anneal");
digitalWrite(ledPin, LOW);
}
currentStateCLK = millis();
}
void displayCurrentTime() {
lcd.setCursor(3, 0);
lcd.print("Time: ");
lcd.print(Time / 1000);
lcd.print("s");
}
void updateEncoder() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
Time = Time - 100;
delay(20);
} else {
// Encoder is rotating CW so increment
Time = Time + 100;
delay(20);
}
}
lastStateCLK = currentStateCLK;
}