// https://wokwi.com/projects/367927711648108545
// https://forum.arduino.cc/t/relay-does-not-turn-on-when-i-press-the-button/1138384
const byte relayPin[4] = { 2, 3, 4, 5,};
int stopButton = 11;
int startButton = 10;
const int isiticiac = 50;
const int isiticikapat = 40;
int sicaklik_role = 6;
bool canRun;
bool isRunning;
// 37 but later whatever ninety seconds from first rekay closures
# define MAX_PROCESS_TIME 37000UL
// active high or low?
# define REALY_ON HIGH
# define REALY_OFF LOW
// OK to run?
# define ENABLE HIGH
# define DIABLE LOW
void setup() {
Serial.begin(115200);
Serial.println("Jello Whirled!\n");
pinMode(stopButton, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
for (byte ii = 0; ii < 4; ii++) {
pinMode(relayPin[ii], OUTPUT);
digitalWrite(relayPin[ii], REALY_OFF);
}
pinMode(sicaklik_role, OUTPUT);
}
# 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,
};
byte cIndex;
unsigned long dwellTime;
unsigned long lastChange;
unsigned long processStart;
void loop() {
unsigned long now = millis(); // time for entire loop
// delay(20);
// simulated temperarature on A0
double temperature = map(analogRead(A0), 0, 1023, 30, 60);
// Serial.print("Temp °C ");
// Serial.println(temperature);
// delay(63);
// Start düğmesine basıldığında röleleri çalıştır
if (digitalRead(startButton) == LOW) {
if (!canRun) {
canRun = true;
// Serial.println("Röleler çalıştırılıyor...");
Serial.println("enabled...");
}
}
// Stop düğmesine basıldığında röleleri durdur
if (digitalRead(stopButton) == LOW) {
if (canRun) {
Serial.println("...disabled");
canRun = false;
isRunning = false;
processStart = 0; // 0 implies we are not baking now (or yet)
}
}
if (canRun) digitalWrite(sicaklik_role, ENABLE);
else digitalWrite(sicaklik_role, DIABLE);
if (canRun && temperature >= isiticiac) {
if (!isRunning) {
isRunning = true;
Serial.println("enabled and temperature high -> turn on");
}
}
if (canRun && temperature <= isiticikapat) {
if (isRunning) {
isRunning = false;
Serial.println("enabled and temperature low -> turn off");
}
}
if (processStart && now - processStart >= MAX_PROCESS_TIME) {
canRun = false;
isRunning = false;
processStart = 0; // really nothing being timed
Serial.println(" out of time death.");
}
if (isRunning) {
if (now - lastChange > dwellTime) {
if (!processStart) processStart = now;
for (byte ii = 0; ii < 4; ii++)
digitalWrite(relayPin[ii], relayState[ii][cIndex] ? REALY_ON : REALY_OFF);
lastChange = now;
dwellTime = timer[cIndex];
cIndex++; if (cIndex >= NSTEPS) cIndex = 0;
}
}
else { // turn off and reset
lastChange = 0;
dwellTime = 0;
cIndex = 0;
for (byte ii = 0; ii < 4; ii++)
digitalWrite(relayPin[ii], REALY_OFF);
}
static unsigned long lastPrint;
if (processStart && now - lastPrint > 1769) {
Serial.print("IN CYCLE (");
Serial.print(isRunning ? "running) " : "idle) ");
Serial.print(now - processStart);
Serial.println("");
lastPrint = now;
}
}
HIGH ->
LOW ->