so if i want to use it as toggle is it possible? if possible please can someone help me I can also be used as a toggle.
Your frizzy picture is fuzzy and is definitely an annotated schematic.
until now your code is a hard to analyse secret because of using numbers and turkish words
I do
not
understand in detail what your code is doing
and I will
not
speculate about it
I want to get a
clear picture
and a throroughly understanding of your code
this requires
ENGLISCH WORDS
Yes it is possible
I have pre-conditions before I help to modify it as a toggle.
These pre-condition are multiple things:
- renaming your relays from
int relay1Pin = 2;
int relay2Pin = 3;
int relay3Pin = 4;
int relay4Pin = 5;
renaming to names that describe spot-on what happends if the relays switches
renaming your two dht-sensors with names that describe spot-on where the sensors are measuring
renaming the humidity and temperatures to say spot-on what temperature / humidity they are measuring
replacing the numbers 0,1,2,3,4,5 with constant names that describe spot-on
what each case is doing
if (isRunning) {
switch (runState) {
case 0: // <<<== instead of a "0" write ENGLISCH words that say what is happening in case 0
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
runState = 1;
intervalChange = 10000;
lastChange = millis();
break;
case 1: // <<<== instead of a "1" write ENGLISCH words that say what is happening in case 0
if (millis() - lastChange >= intervalChange) {
Here is your code with some words already translated to english
#include <DHT.h>
#define DHTPIN 7
#define DHTTYPE DHT22
#define DHTPIN1 8
#define DHTTYPE1 DHT22
int relay1Pin = 2;
int relay2Pin = 3;
int relay3Pin = 4;
int relay4Pin = 5;
int heat_relay_sicaklik_role = 6;
int stopButton = 10;
int startButton = 11;
int fan_role = 12;
int warning_led_uyari_led = A0;
const int heaterac_isiticiac = 40;
const int heaterclose_isiticikapat = 50;
const int start_freeze_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 heater_standby_isitici_bekleme = false;
bool dehumidifier_nem_alici = false;
bool start_without_warming_up_isinmadan_baslama = false;
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht(DHTPIN, DHTTYPE);
unsigned long startTime = 0; // Başlangıç zamanı
void setup() {
pinMode(warning_led_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(heat_relay_sicaklik_role, OUTPUT);
digitalWrite(fan_role, HIGH);
digitalWrite(heat_relay_sicaklik_role, HIGH);
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, HIGH);
digitalWrite(relay3Pin, HIGH);
digitalWrite(relay4Pin, HIGH);
dht.begin();
dht1.begin();
Serial.begin(9600);
startTime = millis(); // Başlangıç zamanını ayarla
}
void loop() {
float nem = dht.readHumidity();
float temperature = dht1.readTemperature();
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(heat_relay_sicaklik_role, LOW);
digitalWrite(warning_led_uyari_led, LOW);
heater_standby_isitici_bekleme = true;
dehumidifier_nem_alici = true;
} else if (temperature >= heaterclose_isiticikapat) {
digitalWrite(heat_relay_sicaklik_role, HIGH);
} else if (heater_standby_isitici_bekleme && temperature <= heaterac_isiticiac) {
digitalWrite(heat_relay_sicaklik_role, LOW);
}
if (heater_standby_isitici_bekleme && temperature <= start_freeze_donmeye_basla) {
startTime = millis();
Serial.println("start over en baştan başladı");
}
if (dehumidifier_nem_alici && nem > 60){
digitalWrite(fan_role, LOW);
}
else if (nem < 60) {
digitalWrite(fan_role, HIGH);
}
if (heater_standby_isitici_bekleme && temperature >= start_freeze_donmeye_basla) {
if (millis() - lastDebounce >= intervalDebounce) {
lastDebounce = millis();
if (!isRunning) {
isRunning = true;
runState = 0;
Serial.println("Relays running_Röleler çalistiriliyor...");
}
}
}
if (digitalRead(stopButton) == LOW) {
heater_standby_isitici_bekleme = false;
isRunning = false;
dehumidifier_nem_alici = false;
start_without_warming_up_isinmadan_baslama = false;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, HIGH);
digitalWrite(relay3Pin, HIGH);
digitalWrite(relay4Pin, HIGH);
digitalWrite(heat_relay_sicaklik_role, HIGH);
digitalWrite(fan_role, HIGH);
digitalWrite(warning_led_uyari_led, LOW);
Serial.println("Relays stopped_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 (heater_standby_isitici_bekleme && millis() - startTime >= 10L * 1000L) {
heater_standby_isitici_bekleme = false;
isRunning = false;
dehumidifier_nem_alici = false;
start_without_warming_up_isinmadan_baslama = false;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, HIGH);
digitalWrite(relay3Pin, HIGH);
digitalWrite(relay4Pin, HIGH);
digitalWrite(heat_relay_sicaklik_role, HIGH);
digitalWrite(fan_role, HIGH);
digitalWrite(warning_led_uyari_led, HIGH);
Serial.println("The_loop_is_complete_Döngü tamamlandi.");
}
}
best regards Stefan
Hi Stefan,
I will translate everything completely into English and share it again. Also, I don't really know what those cases are for, someone shared that code and I kept using it because it worked for me.
i got the code from here: When I press the button, the relay works but does not stop - #8 by qubits-us
#include <max6675.h>
#include <DHT.h>
#define DHTPIN 13
#define DHTTYPE DHT22
int motor_right = 2;
int motor_right1 = 3;
int motor_left = 4;
int motor_left1 = 5;
int heater_relay = 6;
int thermoDO = 7;
int thermoCS = 8;
int thermoCLK = 9;
int stopButton = 11;
int startButton = 10;
int dehumidifier_fan_relay = 12;
int warning_led = A0;
const int heater_on = 40;
const int heater_off = 50;
const int motor_start_spin = 40;
bool isRunning = false;
byte runState = 0;
unsigned long lastChange = 0;
unsigned long intervalChange = 10000;
unsigned long lastDebounce = 0;
int intervalDebounce = 50;
bool wait_heater = false;
bool dehumidifier = false;
bool dont_start_without_warming_up = false;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
DHT dht(DHTPIN, DHTTYPE);
unsigned long startTime = 0; // Başlangıç zamanı
void setup() {
pinMode(warning_led, OUTPUT);
pinMode(dehumidifier_fan_relay, OUTPUT);
pinMode(stopButton, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
pinMode(motor_right, OUTPUT);
pinMode(motor_right1, OUTPUT);
pinMode(motor_left, OUTPUT);
pinMode(motor_left1, OUTPUT);
pinMode(heater_relay, OUTPUT);
digitalWrite(dehumidifier_fan_relay, HIGH);
digitalWrite(heater_relay, HIGH);
digitalWrite(motor_right, HIGH);
digitalWrite(motor_right1, HIGH);
digitalWrite(motor_left, HIGH);
digitalWrite(motor_left1, HIGH);
dht.begin();
Serial.begin(9600);
startTime = millis();
}
void loop() {
float humidity = dht.readHumidity();
double temperature = thermocouple.readCelsius();
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(300);
if (digitalRead(startButton) == LOW) {
digitalWrite(heater_relay, LOW);
digitalWrite(warning_led, LOW);
wait_heater = true;
dehumidifier = true;
} else if (temperature >= heater_off) {
digitalWrite(heater_relay, HIGH);
} else if (wait_heater && temperature <= heater_on) {
digitalWrite(heater_relay, LOW);
}
if (wait_heater && temperature <= motor_start_spin) {
startTime = millis();
}
if (dehumidifier && humidity > 60){
digitalWrite(dehumidifier_fan_relay, LOW);
}
else if (humidity < 60) {
digitalWrite(dehumidifier_fan_relay, HIGH);
}
if (wait_heater && temperature >= motor_start_spin) {
if (millis() - lastDebounce >= intervalDebounce) {
lastDebounce = millis();
if (!isRunning) {
isRunning = true;
runState = 0;
Serial.println("Relay's working");
}
}
}
if (digitalRead(stopButton) == LOW) {
wait_heater = false;
isRunning = false;
dehumidifier = false;
dont_start_without_warming_up = false;
digitalWrite(motor_right, HIGH);
digitalWrite(motor_right1, HIGH);
digitalWrite(motor_left, HIGH);
digitalWrite(motor_left1, HIGH);
digitalWrite(heater_relay, HIGH);
digitalWrite(dehumidifier_fan_relay, HIGH);
digitalWrite(warning_led, LOW);
Serial.println("Relay' are stopped");
if (millis() - lastDebounce >= intervalDebounce) {
lastDebounce = millis();
if (isRunning) {
isRunning = false;
}
}
}
if (isRunning) {
switch (runState) {
case 0:
digitalWrite(motor_right, LOW);
digitalWrite(motor_right1, LOW);
runState = 1;
intervalChange = 10000;
lastChange = millis();
break;
case 1:
if (millis() - lastChange >= intervalChange) {
runState = 2;
}
break;
case 2:
digitalWrite(motor_right, HIGH);
digitalWrite(motor_right1, HIGH);
runState = 3;
intervalChange = 5000;
lastChange = millis();
break;
case 3:
if (millis() - lastChange >= intervalChange) {
runState = 4;
}
break;
case 4:
digitalWrite(motor_left, LOW);
digitalWrite(motor_left1, LOW);
runState = 5;
intervalChange = 10000;
lastChange = millis();
break;
case 5:
if (millis() - lastChange >= intervalChange) {
runState = 6;
}
break;
case 6:
digitalWrite(motor_left, HIGH);
digitalWrite(motor_left1, HIGH);
runState = 7;
intervalChange = 5000;
lastChange = millis();
break;
case 7:
if (millis() - lastChange >= intervalChange) {
runState = 0;
}
break;
}
}
if (wait_heater && millis() - startTime >= 10L * 1000L) {
wait_heater = false;
isRunning = false;
dehumidifier = false;
dont_start_without_warming_up = false;
digitalWrite(motor_right, HIGH);
digitalWrite(motor_right1, HIGH);
digitalWrite(motor_left, HIGH);
digitalWrite(motor_left1, HIGH);
digitalWrite(heater_relay, HIGH);
digitalWrite(dehumidifier_fan_relay, HIGH);
digitalWrite(warning_led, HIGH);
Serial.println("Loop Done.");
}
}
Sorry if I made a mistake in the translation. This code is translated into English. Also, if I can use it as a toggle, I'm open to any help that suits me.
Thanks.
Well if you don't understand the code in detail you wil rely on other users help for each and every detail if you want to change something.
IMHO this would be a bad situation
@the author of this code:
not a good code-version to use numbers instead of spot-on self-explaining constant-names.
@kaandagci describe in normal words what do the cases
0:
1:
2:
etc.
do
best regards Stefan
I'm sorry, but I didn't understand anything you said.
@kaandagci now you know how we feel.
I was able to add timing to the sketch I wrote which does exactly what I think you want except you think it doesn't.
What is unclear is the roll of the timer.
Please confirm:
-
One button enables your relay process. One button disables it.
-
When enabled, the relays process will be controlled by temperature
- too hot, turn off relays process
- too cold, turn on relays process
-
if time since relays first operated is > N minutes, terminate:
- all relays off
- relays process disabled
-
the Stop button can be used at any time to disable and reset the sketch
You have added stuff to your code which is not yet adding any functionality. The code also has large sections you don't understand and don't appear to need or want to. Reading your code at this time gives me a headache, so I am sticking with my own version.
But… I think you are not correctly setting the starting time of the baking process, or are not maintaining it correctly.
I set the start time when I see the first relay process step taken. I only reset that time when the entire process is halted by the Stop button or it times out due to timing out.
So I can tell in the loop how long the relays process has been operating.
Is the purpose of the timer to end after some time? What precisely defines the beginning of that time?
a7
Yes, the point of the timer is that it expires after a while. As an example, let me explain it through people, when I say start, it starts to warm up first and the person warms up. Run for 10 minutes after the warm-up is complete. After running for 10 minutes, stop and wait. If I say run again, it warms up, if it's warm, it runs again for 10 minutes and finally stops. Briefly like this.
If it is the beginning of time, if the start button is pressed and the temperature reaches the temperature we set (40 degrees), then the time starts.
My problem is here: With millis() i have to wait after loop finish - #14 by kaandagci
If you can write the code in a simpler way and it will work the same, why not.
Thanks.
what does this mean?
start timer after temperature >= threshold after button is pressed?
what causes temperature to increase?
what does timer control?
Only you can say how my simulation of your project differs from my description which agreed with all of your words:
I changed the relay pattern, it is easily modified.
HTH
a7
Not if you were learning things…
One day, you’ll be back here helping others !
Thank you so much. This looks great. But sorry, I'm a bit new to arduino. How can I set the operating times of these relays? In my previous code intervalChange = 5000; There were such codes and they were giving tasks like they would stay open this long and remain closed this much. How can we set it here?
You have to start reading the code. I found this
# define NSTEPS 6
// 1s for realy energixed during
const byte relayState[4][NSTEPS] = {
{1, 0, 0, 0, 0, 0,},
{0, 0, 1, 0, 0, 0,},
{0, 0, 0, 0, 1, 0,},
{0, 0, 0, 0, 1, 0,},
};
// time to spend in each step
unsigned long timer[NSTEPS] = {
333, 277, 333, 277, 1000, 1444,
};
which I believe shows the state of all four relays over six steps and each step gets its own time period.
Your original sequence can be encoded in a similar fashion, figure it out.
HTH
BTW did I ask if it is OK to just turn off the relays no matter where in the sequence they might be when stopped, timed out or shut off due to temperature?
a7
I'm guessing that this is a school project and you have been given a very clear component list and a very clear process description as part of the assignment.
Obviously, you are reluctant to publish the entire assignment. The problem is, for those helping you, it is necessary to have an overview of what you are trying to achieve. What you have done is broken the whole process down and presented the bits where you are having difficulties. You've said that it is "like" a bread making process and it looks like your hardware is there to model the mixing process to create dough (the relays which would control the motors), the fermentation process where the heater and thermocouple are used to maintain a temperature range, a fan and humidity sensor used at some stage in the process to maintain the humidity of the dough. You have a start button to initiate the whole process and a stop button which appears only to be used as a emergency stop to abort the whole process. There is also a warning light. Of course, it could be another process altogether.
Anyway, the process appears to be, partially based on the hardware you have shown and partly reverse engineered out of what you have said / presented, something like this:
- Start - button press to initiate the entire process
- Warm up ingredients (flour, water, yeast etc.) by switching the heater on. Switch on warning light if the ingredients have not reached a threshold temperature within a deadline. Once the temperature has been achieved, start the mixing process
- Mixing Dough. Here the motors agitate left and right in a timed sequence (10 seconds on, 5 seconds off). The mixing process has a duration of X seconds.
- Fermentation. Here, the heater is switched on to warm the dough to A degrees Centigrade then the dough is allowed to cool to N degrees. Once cooled, the temperature is maintained (by switching the heater on and off) over a timed period.
- That is it.
During the entire process, the humidity is controlled by operating the fan according the humidity sensor value.
At any time during the process, you can press the stop button to abort it.
Is that about right for what you are trying to achieve? If not then add corrections to it.
But, there are some questions: What do you mean here by toggling. Is it that you want to repeat a part of the process based on some condition or what ?
There is an error in the warning light part. the warning light only works after the whole cycle has finished. that is, the motors turn left and right, the cycle ends at the end of x time and the warning light turns on. The purpose of the warning light is to let you know that the cycle is over. I have explained the whole process many times and I will explain it again:
Start button is pressed. The relay controlling the heater becomes active. The cycle never starts and does not count until the heater reaches a certain temperature. If the temperature wants to reach the desired temperature after 2 hours, the cycle starts after 2 hours. After the cycle starts, the motors turn left for 10 seconds and stay off for 5 seconds. The reason is that if we try to reverse the engine directly after 10 seconds, we can burn the engine. Yes, the motor turned left and right for x times. After x time has elapsed, it activates the beacon to let you know the loop is over. after that, the person either presses start and restarts the cycle, or presses the stop button and restarts it after 30 hours.
In addition, the fan relay, which will keep the humidity at a certain level during the cycle, will operate independently. that is, it will not be conditioned to commands such as open for 10 seconds and close for 10 seconds. If the humidity is x, the fan relay works, if it is y, the fan relay is closed.
I'm sorry, i did not understand. Can you be a little more descriptive? Can you explain a little easier because I don't know the code function you wrote?
@kaandagci kullanıcısının ya düşük seviyeli trol dediğim biri ya da programlama öğrenmek konusunda çok tembel olduğu izlenimine sahibim kendisi
@kaandagci kullanıcısı programın ne yapmasını istediğini çok detaylı ve anlaşılması çok iyi bir şekilde anlatana kadar daha fazla cevap vermemenizi öneririm.
kişisel izlenimim kullanıcı @kaandagci, programlamayı kendi başına öğrenmekten kaçınmak için kendi kodunu başkalarına yazdırarak ayrıntılı olmayan yanıtlar gönderiyor.
Bu yüzden benim önerim, en azından çok ayrıntılı açıklama yayınlanana kadar beklemek.
all words in english
I have the impression user @kaandagci is either what I call alow-level-troll or very lazy about learning programming himself
I suggest to not answer any more until user @kaandagci was able to give a very detailed and very good to understand descirption of what he wants the program to do.
me personal I have the impression user @kaandagci posts non-detailed answers trying to make other people write his code so he can avoid learning programming himself.
So my suggestion is to wait at least until the very detailed description is posted.
finally I was able to read your mind. Here is the code that does exactly what you want the code to do
This posting translates automatic to turkish
It is quite a complex process. It is best to think of the process as a number of discrete states where it stays in one state until it explicitly changed into another state but loops through continuously testing which state it is in. Does this pseudo code represent more or less what you want to achieve ?
State machine loop {
All States (except stopped) :
Check humidity and control fan as necessary.
State: Stopped
If ( start button is pressed) {
switch heater on
change state to Initial_Warming.
}
State: Initial_Warming
If ( temperature N degrees reached ) {
switch heater off
change state to Mixing
}
State: Mixing
// this could be represented by a separate state for each motor change
motor on 10 seconds right
motor off 5 seconds
motor on 10 seconds left
motor off 5 seconds
if in current state > X seconds {
switch warning light on.
change state to Operator_Wait
}
State: Operator_Wait
if (start button pressed) change state to Initial_Warming.
If (stop button pressed) change state to stopped.
}
What about the 30 hours you mentioned ? Is that a manual wait or where does it fit in ?
In what states (if any) is the temperature maintained at a constant temperature ?