With millis() i have to wait after loop finish

Hi,

I want the code below to work like this;

1- Press the start button and sicaklik_role is activated and the cycle starts after the thermocouple reaches 40 degrees. (I'm going briefly)
2- After the cycle is finished, the warning_led turns HIGH and a message about the end of the cycle appears on the serial port screen. ( Serial.println("Döngü tamamlandı."); )

So what do I want next, after the loop ends, I want the loop to run for the specified time (10 seconds) when I press the start button again after the uyari_led goes to HIGH state. So I want it to be like starting over. After the loop is finished, if I press the stop button and then press the start button again, the uyari_led goes HIGH and I get a message that the loop is over on the serial port screen. But if I press the stop button and wait for 20 seconds and press the start button again, the loop starts from the beginning as I want. I've been trying to solve this for a few days but I can't. Can anyone help?

#include <max6675.h>
#include <DHT.h>

#define DHTPIN 13         
#define DHTTYPE DHT22

int relay1Pin = 2;
int relay2Pin = 3;
int relay3Pin = 4;
int relay4Pin = 5;
int sicaklik_role = 6;
int thermoDO = 7;
int thermoCS = 8;
int thermoCLK = 9;

int stopButton = 11;
int startButton = 10;

int fan_role = 12;

int uyari_led = A0;

const int isiticiac = 40;
const int isiticikapat = 50;
const int donmeye_basla = 40;

bool isRunning = false;
byte runState = 0;

unsigned long lastChange = 0;
unsigned long intervalChange = 10000;

unsigned long lastDebounce = 0;
int intervalDebounce = 50;

bool isitici_bekleme = false;
bool nem_alici = false;
bool isinmadan_baslama = false;


MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

DHT dht(DHTPIN, DHTTYPE);

unsigned long startTime = 0; // Başlangıç zamanı

void setup() {
  pinMode(uyari_led, OUTPUT);
  pinMode(fan_role, OUTPUT);
  pinMode(stopButton, INPUT_PULLUP);
  pinMode(startButton, INPUT_PULLUP);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT);
  pinMode(relay4Pin, OUTPUT);
  pinMode(sicaklik_role, OUTPUT);
  digitalWrite(fan_role, HIGH);
  digitalWrite(sicaklik_role, HIGH);
  digitalWrite(relay1Pin, HIGH);
  digitalWrite(relay2Pin, HIGH);
  digitalWrite(relay3Pin, HIGH);
  digitalWrite(relay4Pin, HIGH);

  dht.begin();


  Serial.begin(9600);
    startTime = millis(); // Başlangıç zamanını ayarla

}

void loop() {
  float nem = dht.readHumidity();
  double temperature = thermocouple.readCelsius();

  Serial.print("Ortam nem: ");
  Serial.print(nem);
  Serial.print("%");
  
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
  delay(300);

    
  if (digitalRead(startButton) == LOW) {
    digitalWrite(sicaklik_role, LOW);
    digitalWrite(uyari_led, LOW);
    isitici_bekleme = true;
    nem_alici = true;
  }   else if (temperature >= isiticikapat) {
    digitalWrite(sicaklik_role, HIGH);
  } else if (isitici_bekleme && temperature <= isiticiac) {
    digitalWrite(sicaklik_role, LOW);
  }
    if (isitici_bekleme && temperature <= donmeye_basla) {
    startTime = millis();
    Serial.println("en baştan başladı");
   }
  


  if (nem_alici && nem > 60){
    digitalWrite(fan_role, LOW);
  }
  else if (nem < 60) {
    digitalWrite(fan_role, HIGH);
  }
    
  if (isitici_bekleme && temperature >= donmeye_basla) {
    if (millis() - lastDebounce >= intervalDebounce) {
      lastDebounce = millis();
      if (!isRunning) {
        isRunning = true;
        runState = 0;
        Serial.println("Röleler çalıştırılıyor...");
      }
    }
  }

  if (digitalRead(stopButton) == LOW) {
    isitici_bekleme = false;
    isRunning = false;
    nem_alici = false;
    isinmadan_baslama = false;
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, HIGH);
    digitalWrite(relay3Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
    digitalWrite(sicaklik_role, HIGH);
    digitalWrite(fan_role, HIGH);
    digitalWrite(uyari_led, LOW);
    Serial.println("Röleler durduruldu.");
    if (millis() - lastDebounce >= intervalDebounce) {
      lastDebounce = millis();
      if (isRunning) {
        isRunning = false;
      }
    }
  }

  if (isRunning) {
    switch (runState) {
      case 0:
        digitalWrite(relay1Pin, LOW);
        digitalWrite(relay2Pin, LOW);
        runState = 1;
        intervalChange = 10000;
        lastChange = millis();
        break;

      case 1:
        if (millis() - lastChange >= intervalChange) {
          runState = 2;
        }
        break;
      case 2:
        digitalWrite(relay1Pin, HIGH);
        digitalWrite(relay2Pin, HIGH);
        runState = 3;
        intervalChange = 5000;
        lastChange = millis();
        break;
      case 3:
        if (millis() - lastChange >= intervalChange) {
          runState = 4;
        }
        break;
      case 4:
        digitalWrite(relay3Pin, LOW);
        digitalWrite(relay4Pin, LOW);
        runState = 5;
        intervalChange = 10000;
        lastChange = millis();
        break;
      case 5:
        if (millis() - lastChange >= intervalChange) {
          runState = 6;
        }
        break;
      case 6:
        digitalWrite(relay3Pin, HIGH);
        digitalWrite(relay4Pin, HIGH);
        runState = 7;
        intervalChange = 5000;
        lastChange = millis();
        break;
      case 7:
        if (millis() - lastChange >= intervalChange) {
          runState = 0;
        }
        break;
    }
  }
 
    if (isitici_bekleme && millis() - startTime >= 10L * 1000L) {
    isitici_bekleme = false;
    isRunning = false;
    nem_alici = false;
    isinmadan_baslama = false;
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, HIGH);
    digitalWrite(relay3Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
    digitalWrite(sicaklik_role, HIGH);
    digitalWrite(fan_role, HIGH);
    digitalWrite(uyari_led, HIGH);
    Serial.println("Döngü tamamlandı.");
    
    }
}

There is some previous discussion of @kaandagci's project here:

It appears to be a hardware problem, post an annotated schematic as you have wired it. Be sure to include all connections, power, ground, and power sources. Also include links to technical information on each of the hardware items.

I suggest that, before getting too deep into code, that you design a finite state machine model to describe the process you are attempting to implement. Here is an example diagram: Finite-state machine for embedded systems | Control Engineering. Give meaningful names to the states like "start", "initial warmup" etc. Show also the conditions for transitioning from one state to another such as a button press, a timeout, a temperature threshold etc. and the actions to be taken on entering a state such as "heater on", "fan off" etc. If you get that right, the code will be a simple 1:1 representation of your state diagram.

EDIT

I see what it is now from this post Relay does not turn on when I press the button - #7 by kaandagci . It is (like) a bread making machine. The complete process appears to be a perfect candidate for a finite state machine solution.
When you are defining the main states of the process, it is best to start by using terms related to the actual process such as warming, fermentation, mixing, makeup, proofing, baking, cooling, etc. instead of using terms related to your implementation such as relay1 on, motor clockwise.

this is great. I will take that into account. Thanks

DHT22: DHT22 Sensor Pinout, Specs, Equivalents, Circuit & Datasheet

2x 4 relay module: 5V Four-Channel Relay Module - Pin Diagram, Specifications, Applications, Working

K-type MAX6675 thermocouple

2x button

1 Led

Arduino Nano

ywrobot breadboard power supply:
https://www.petervis.com/Raspberry_PI/Breadboard_Power_Supply/YwRobot_Breadboard_Power_Supply.html

this is how it works: 22 Haziran 2023 - YouTube

some quesiton/suggestions were ignored in Relay does not turn on when I press the button

i think he wants the switch to en/disable the system

when on, the system monitors temperature and toggles the relays when > Threshold. the relays should go off when temperature < Threshold but the system remains enabled until turned off

Sorry. I tried so many things, but most of them were a waste of time. I was so busy that I wanted to take a break.

after temperature > threshold, the cycle starts and if temperature < threshold, only "sicaklik_role" closes, the other cycle continues. sicaklik_role is connected to a heater.

@kaandagci has a moving target project. it has never been clear what she actually wants or needs, and, every so often, that seems to move along with a new feature or additional word salad description of functionality.

On the other thread(s) many ideas and solutions and simulations have been offered. She appears to be using a very early example code.

@kaandagci youd get a bit faster forward movement if you would completely describe the full behaviour of the target project.

We could help better if you were to place your code in the simulator. Several simulations of various attempt to help you are on offer at the other thread you abandoned for this new one. You could use all the same wiring that has been done, several times on the threads, and just drop your code into it and adjust it if necessary.

We then could run your code, and perhaps begin to figure out why what has been offered is not what you want.

@gcjr's description, with the exception of using one switch instead of two buttons, a minor point, is the best I can make of what you want. I posted a solution that does exactly that; you do not appear to have even tried it, and you certainly have not bothered to sayin what way it falls short.

a7

I tried your codes, but none of them worked the way you wanted or the way I wanted. Now I'm going to throw the code you wrote into the arduino and upload that video to youtube. You will understand me better.

1- Start button is pressed and sicaklik_role becomes active. (it is connected to a heater and when sicaklik_role is active the heater starts to work)

2- While the heater is running, the thermocouple measures the temperature and when it reaches 40 degrees, the cycle starts, but sicaklik_role does not turn off. When the sicaklik_role reaches 50 degrees, it closes and the loop continues. In this way, the temperature will remain between 40-50 degrees.

3- The loop continues to run for the specified time (10 seconds as it is currently in the testing phase).

4- loop ends and uyari_led becomes active. In this way, it warns us.

5- it's all happening so far, it's great. But after the loop ends and the uyari_led lights up, when I press the start button again, the loop does not start from the beginning or if I press the stop button and then start again, it does not work again. (I showed this in the youtube video) Link: 22 Haziran 2023 - YouTube

My problem is the place at level 5. If I solve that too, my work will be finished and it will be ready for installation.

Loop must run continuously!

The video is not helpful. You press some buttons and some relays chatter.

Better: put your code into the simulator. The wiring is already done. Then we could run it, and see where you even tried at all to limit the running time to 10 seconds.

At least post the code where you've tried to add the latest feature.

a7

I made the schematic with fritzing, not in a simulator. The problem seems pretty obvious. If you have noticed at the end, even though I press the stop start keys, the led flashes and it never starts again. If I stay in the stop state for 20 seconds and press the start button then it starts again. I am aware that I am making a mistake somewhere in the code, but I can't find where.

Simülatör,

WOKWI simülatörü

Bu bir

İnternet sitesi

bunu gösterdiğini

Arduino-Simülasyon-Web Sitesi

bu bağlantıya tıklayarak tarayıcınızda:Simulation_for_kaandagci - Wokwi ESP32, STM32, Arduino Simulator

Geçerli kodunuzu panoya kopyalayın. Ardından mevcut kodunuzu WOKWI simülatörüne yapıştırın.

post in english

Simulator,

WOKWI simulator

This is a

Web site

showing that

Arduino-Simulation-Website

in your browser by clicking this link: Simulation_for_kaandagci - Wokwi ESP32, STM32, Arduino Simulator
Copy your current code to the clipboard. Then paste your current code into the WOKWI simulator.

Please use @StefanL38's link and insert your code instead.

Or use this

and insert your code instead.

You may have to make adjustments, mostly it should be obvious where your relays go and your buttons and so forth.

In my simulation, I use a slide fader to adjust the temperature. But you can do that with the simulated thermal sensor also - I don't happen to have one, and I also find it more conveneitn to use the sldie fader anyway.

Either way you proceed will allow us to run your code.

a7