timer

i am trying to build a adjustable timer circuit for my steralizer it needs to be adjustable and start the timer when it reaches a specific temp, i tried using a standard timer code but it causes my temp sensor to stop reading any advice would be greatly apreciated

The first piece of advice is to show the attempt You have made. Attach a wiring diagram, and code, using code tags according to the advice gine in "How to use this Forum" and "How to attach code".

Railroader:
The first piece of advice is to ...

And the second imo would be to explain what...

Mushroom_man:
standard timer code

... means.

sorry guys i should have posted my code my bad here it is what happens is when i turn the encoder or use the button on the encoder the thermo coupler reads but as so as i stop turning it it stops reading
basically each system works well on its own but together the temp reading hangs, will post a picture of my schematics shortly

}

#include <Wire.h>
#include <max6675.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

#define Start 4 // start stop button
#define COOKER  13
int thermoDO = 8;
int thermoCS = 5;
int thermoCLK = 7;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
uint8_t degree[8]  = {140, 146, 146, 140, 128, 128, 128, 128};
int hours = 0;
int minutes = 0;
int seconds = 0;
boolean timeState = false;
#define encoderPinA 2 // right
#define encoderPinB 3 // left
#define encoderButton 6 // switch
#define gnd2 12
#define vcc2 11
#define gnd3 10
int HMS = 1;
int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
boolean A_set = false;
boolean B_set = false;


void setup() {
  pinMode(COOKER, OUTPUT);
  digitalWrite(COOKER , HIGH);
  pinMode (vcc2 , OUTPUT);
  digitalWrite(vcc2, HIGH);
  pinMode(gnd3, OUTPUT);
  digitalWrite(gnd3, LOW);
  pinMode (gnd2 , OUTPUT);
  digitalWrite(gnd2 , LOW);
  pinMode(Start, INPUT_PULLUP);
  pinMode(encoderPinA, INPUT_PULLUP); //enabling pullups
  pinMode(encoderPinB, INPUT_PULLUP);
  pinMode(encoderButton, INPUT_PULLUP);
  attachInterrupt(0, doEncoderA, CHANGE); //pin 2
  attachInterrupt(1, doEncoderB, CHANGE); //pin 3
  Serial.begin(9600); // output
  lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.setCursor(0, 0);
  lcd.print ("COOKER");
  lcd.setCursor(4, 1);
  lcd.print("00:00:00");
  lcd.setCursor(15, 0);
  lcd.print("C");

}

void loop() {
  lcd.setCursor(7, 0);
  lcd.print( thermocouple.readCelsius(), 2);
  Serial.print(thermocouple.readCelsius());


  if (digitalRead(encoderButton) == LOW)
  {
    HMS = HMS + 1;
    if (HMS == 4)
    {
      HMS = 1;
    }
    delay(1000);
  }
  rotating = true; // reset the debouncer
  encoderPos = constrain(encoderPos, -1, 1);
  if (lastReportedPos != encoderPos) {

    if (HMS == 1) {
      hours = hours + encoderPos;
      hours = constrain(hours, 0, 48);
    }
    else if (HMS == 2) {
      minutes = minutes + encoderPos;
      minutes = constrain(minutes, 0, 60);
    }
    else if (HMS == 3) {
      seconds = seconds + encoderPos;
      seconds = constrain(seconds, 0, 60);
    }
    // Serial.println(encoderPos); // for testing
    // Serial.print("hours ");
    // Serial.println(hours);
    // Serial.print("minutes ");
    // Serial.println(minutes);
    // Serial.print("seconds ");
    // Serial.println(seconds);
    // Serial.println(" ");
    lcd.setCursor(4, 1);
    if (hours <= 9)
    {
      lcd.print("0");
    }
    lcd.print(hours);
    lcd.print(":");
    if (minutes <= 9)
    {
      lcd.print("0");
    }
    lcd.print(minutes);
    lcd.print(":");
    if (seconds <= 9)
    {
      lcd.print("0");
    }
    lcd.print(seconds);
    encoderPos = 0;
    lastReportedPos = encoderPos;

  }
  if (digitalRead(Start) == LOW) { //start count down timer
    timeState = true;
    //delay(1000);
    while (timeState == true) {
      if (minutes == 0 && hours >= 1) {
        minutes = 60;
        hours = hours - 1;
      }
      if (seconds == 0 && minutes >= 1) {
        seconds = 60;
        minutes = minutes - 1;
      }
      else if (minutes == 0 && hours == 0 && seconds == 0) { //count down alarm
        while (timeState == true) {
          tone(11, 600, 250);
          delay(250);
          tone(11, 800, 250);
          delay(250);

        }
      }
      delay(992); // delay for keping time master setting!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      seconds = seconds - 1;
      //Serial.print("hours "); // for testing
      //Serial.println(hours);
      //Serial.print("minutes ");
      //Serial.println(minutes);
      //Serial.print("seconds ");
      // Serial.println(seconds);
      //Serial.println(" ");

      lcd.setCursor(4, 1);
      if (hours <= 9)
      {
        lcd.print("0");
      }
      lcd.print(hours);
      lcd.print(":");
      if (minutes <= 9)
      {
        lcd.print("0");
      }
      lcd.print(minutes);
      lcd.print(":");
      if (seconds <= 9)
      {
        lcd.print("0");
      }
      lcd.print(seconds);

      if (digitalRead(Start) == LOW) {
        delay(1000);
        timeState = false;
        break;
      }
    }
  }

}

// Interrupt on A changing state
void doEncoderA() {
  // debounce
  if ( rotating ) delay (1); // wait a little until the bouncing is done

  // Test transition, did things really change?
  if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
    A_set = !A_set;

    // adjust counter + if A leads B
    if ( A_set && !B_set )
      encoderPos = 1;

    rotating = false; // no more debouncing until loop() hits again
  }
}

// Interrupt on B changing state
void doEncoderB() {
  if ( rotating ) delay (1);
  if ( digitalRead(encoderPinB) != B_set ) {
    B_set = !B_set;
    // adjust counter - 1 if B leads A
    if ( B_set && !A_set )
      encoderPos = -1;

    rotating = false;
  }
}

[/quote]

finola_marsaili:
And the second imo would be to explain what...

... means.

i downloaded a countdown timer code off a you tube tutorial, i am still quite new to arduino and have had zero coding background in the past

Mushroom_man:
sorry guys i should have posted my code my bad here it is

Please use the Auto Format tool to indent your code and then repost the program. Without that it is impossible to see where different blocks of code start and end.

Also note that the program in Reply #3 is not complete. It may only be missing a } but it could also be missing a lot more.

...R

when i use the auto format tool it says no necessary changes for auto format and its the complete code it is compiling and loading to the board its just that the sensor only reads when you press encoder button in

#include <Wire.h>
#include <max6675.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

#define Start 4 // start stop button
#define COOKER  13
int thermoDO = 8;
int thermoCS = 5;
int thermoCLK = 7;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
uint8_t degree[8]  = {140, 146, 146, 140, 128, 128, 128, 128};
int hours = 0;
int minutes = 0;
int seconds = 0;
boolean timeState = false;
#define encoderPinA 2 // right
#define encoderPinB 3 // left
#define encoderButton 6 // switch
#define gnd2 12
#define vcc2 11
#define gnd3 10
int HMS = 1;
int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
boolean A_set = false;
boolean B_set = false;


void setup() {
  pinMode(COOKER, OUTPUT);
  digitalWrite(COOKER , HIGH);
  pinMode (vcc2 , OUTPUT);
  digitalWrite(vcc2, HIGH);
  pinMode(gnd3, OUTPUT);
  digitalWrite(gnd3, LOW);
  pinMode (gnd2 , OUTPUT);
  digitalWrite(gnd2 , LOW);
  pinMode(Start, INPUT_PULLUP);
  pinMode(encoderPinA, INPUT_PULLUP); //enabling pullups
  pinMode(encoderPinB, INPUT_PULLUP);
  pinMode(encoderButton, INPUT_PULLUP);
  attachInterrupt(0, doEncoderA, CHANGE); //pin 2
  attachInterrupt(1, doEncoderB, CHANGE); //pin 3
  Serial.begin(9600); // output
  lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.setCursor(0, 0);
  lcd.print ("COOKER");
  lcd.setCursor(4, 1);
  lcd.print("00:00:00");
  lcd.setCursor(15, 0);
  lcd.print("C");

}

void loop() {
  lcd.setCursor(7, 0);
  lcd.print( thermocouple.readCelsius(), 2);
  Serial.print(thermocouple.readCelsius());


  if (digitalRead(encoderButton) == LOW)
  {
    HMS = HMS + 1;
    if (HMS == 4)
    {
      HMS = 1;
    }
    delay(1000);
  }
  rotating = true; // reset the debouncer
  encoderPos = constrain(encoderPos, -1, 1);
  if (lastReportedPos != encoderPos) {

    if (HMS == 1) {
      hours = hours + encoderPos;
      hours = constrain(hours, 0, 48);
    }
    else if (HMS == 2) {
      minutes = minutes + encoderPos;
      minutes = constrain(minutes, 0, 60);
    }
    else if (HMS == 3) {
      seconds = seconds + encoderPos;
      seconds = constrain(seconds, 0, 60);
    }
    // Serial.println(encoderPos); // for testing
    // Serial.print("hours ");
    // Serial.println(hours);
    // Serial.print("minutes ");
    // Serial.println(minutes);
    // Serial.print("seconds ");
    // Serial.println(seconds);
    // Serial.println(" ");
    lcd.setCursor(4, 1);
    if (hours <= 9)
    {
      lcd.print("0");
    }
    lcd.print(hours);
    lcd.print(":");
    if (minutes <= 9)
    {
      lcd.print("0");
    }
    lcd.print(minutes);
    lcd.print(":");
    if (seconds <= 9)
    {
      lcd.print("0");
    }
    lcd.print(seconds);
    encoderPos = 0;
    lastReportedPos = encoderPos;

  }
  if (digitalRead(Start) == LOW) { //start count down timer
    timeState = true;
    //delay(1000);
    while (timeState == true) {
      if (minutes == 0 && hours >= 1) {
        minutes = 60;
        hours = hours - 1;
      }
      if (seconds == 0 && minutes >= 1) {
        seconds = 60;
        minutes = minutes - 1;
      }
      else if (minutes == 0 && hours == 0 && seconds == 0) { //count down alarm
        while (timeState == true) {
          tone(11, 600, 250);
          delay(250);
          tone(11, 800, 250);
          delay(250);

        }
      }
      delay(992); // delay for keping time master setting!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      seconds = seconds - 1;
      //Serial.print("hours "); // for testing
      //Serial.println(hours);
      //Serial.print("minutes ");
      //Serial.println(minutes);
      //Serial.print("seconds ");
      // Serial.println(seconds);
      //Serial.println(" ");

      lcd.setCursor(4, 1);
      if (hours <= 9)
      {
        lcd.print("0");
      }
      lcd.print(hours);
      lcd.print(":");
      if (minutes <= 9)
      {
        lcd.print("0");
      }
      lcd.print(minutes);
      lcd.print(":");
      if (seconds <= 9)
      {
        lcd.print("0");
      }
      lcd.print(seconds);

      if (digitalRead(Start) == LOW) {
        delay(1000);
        timeState = false;
        break;
      }
    }
  }

}

// Interrupt on A changing state
void doEncoderA() {
  // debounce
  if ( rotating ) delay (1); // wait a little until the bouncing is done

  // Test transition, did things really change?
  if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
    A_set = !A_set;

    // adjust counter + if A leads B
    if ( A_set && !B_set )
      encoderPos = 1;

    rotating = false; // no more debouncing until loop() hits again
  }
}

// Interrupt on B changing state
void doEncoderB() {
  if ( rotating ) delay (1);
  if ( digitalRead(encoderPinB) != B_set ) {
    B_set = !B_set;
    // adjust counter - 1 if B leads A
    if ( B_set && !A_set )
      encoderPos = -1;

    rotating = false;
  }
}

here is my schematics of my project

Doc1.pdf (169 KB)

Mushroom_man:
when i use the auto format tool it says no necessary changes for auto format

That means it did not find any errors. But if you look at the code it is laid much more neatly than the version that was in your Original Post.

Now you need to tell us in detail what your program actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

By the way you should not have any delay() in an Interrupt Service Routine. The code in an ISR should be designed to complete as quickly as possible - 100 microsecs would be a long time.

...R

alright so what i want is a adjustable count down timer, as i need it to run for different times as well as i need it to only start the timer when it reaches a preset tem of around 115 deg or there around, but the problem is the temp sensor only reads when the start button or the encoder is set, the timer itself works nicely,

the problem is the temp sensor only reads when the start button or the encoder is set, the timer itself works nicely,

I would suggest starting from the small piece of code where the temps sensor reads correctly.

Then start adding small pieces back in until you see what breaks.

Mushroom_man:
but the problem is the temp sensor only reads when the start button or the encoder is set, the timer itself works nicely,

Your code makes no attempt to check the temperature

void loop() {
  lcd.setCursor(7, 0);
  lcd.print( thermocouple.readCelsius(), 2);
  Serial.print(thermocouple.readCelsius());


  if (digitalRead(encoderButton) == LOW)
  {

You need to save the temperature reading to a variable and then replace

  if (digitalRead(encoderButton) == LOW)

with something like

if (tempValue >= 115) {

Note that this is bad practice

  lcd.print( thermocouple.readCelsius(), 2);
  Serial.print(thermocouple.readCelsius());

because you are taking two separate temperature readings and they may be different. Much better to do it this way so both outputs must show the same value

  tempValue = thermocouple.readCelsius();
  lcd.print( tempValue, 2);
  Serial.print(tempValue);

This style is not a good idea either

  if (digitalRead(encoderButton) == LOW)

because you have no means to check the value that is being used by the IF clause. Again it's better to do it like this

encButtonVal = digitalRead(encoderButton);
if (encButtonVal == LOW)

That way you print encButtonVal if needed for debugging.

...R

Your code makes no attempt to check the temperature

void loop() {
  lcd.setCursor(7, 0);
  lcd.print( thermocouple.readCelsius(), 2);
  Serial.print(thermocouple.readCelsius());

With MAX6675.h, the Adafruit library, .readCelsius() returns a double.

I think the OP's original complaint was that he couln't read the temperature unless something else, apparently unrelated, was going on. The issue was not actions upon a conditional test.

Perhaps I misunderstood.

Eh? Is there any real double for Arduinos? Isn't double working exactly like float, having the same representation?

Eh? Is there any real double for Arduinos? Isn't double working exactly like float, having the same representation?

Yes, correct.

Railroader:
Eh? Is there any real double for Arduinos? Isn't double working exactly like float, having the same representation?

It depends. Some Arduino architectures (e.g. Due) do have 'proper' double.

wildbill:
It depends. Some Arduino architectures (e.g. Due) do have 'proper' double.

Whow! I've been using as much as 64 bit long signed or unsigned integers in the past.

Robin2:
That means it did not find any errors. But if you look at the code it is laid much more neatly than the version that was in your Original Post.

I think your concern was not just the indenting, which auto-format sorts out, but the inappropriate line spacing. Double-spacing should only be used to separate separate functions or major functional blocks, otherwise it is just plain confusing and impedes viewing within the available window. :roll_eyes: