Using a rotary encoder with millis to set time, then run it with a button press

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. :wink:

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;
}

+1 Karma given for code tags on your first post.

Not sure what you expect but at least in this forum subsection, no one is going to write code for you. You’ll get some examples but that’s usually about it.

There is a very capable library for handling the encoder. You should use it.

Get that working. Then we can discuss millis() as you haven’t done anything with it to exit the process. As I see things in your current code, you’re overwriting the millis() clock with a digital input. I have no idea what you’re attempting to do there.

There are examples in the IDE that can be easily adapted to what you want to do. Check out “Blink without delay”.

I hope you have experience with industrial electronics, what you are controlling, an induction annealer, probably a lot of watts you will have a lot of noise issues. Your shielding, packaging and grounding will be of primary importance in the actual implementation.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.