// Relay / LED Setup
//
// D18 is the Opto Thyristor for the soft start thermistors/relays.
// D19 is the relay for the mains transformer.
// D5 is the Opto SCR for the B+ connection.
const int led = 2;
const int qty_rel = 3;
const int relay[] = {5,18,19};
const int power = 23;
// Power Button Setup
const int button_hold = 750; // Time you have to hold button for power on/off
bool power_on = false;
bool block = false;
bool power_switch = false;
int click;
long click_time;
int click_prev;
void setup() {
// put your setup code here, to run once:
for (int i=0; i<qty_rel; i++) {
pinMode(relay[i], OUTPUT);
}
pinMode(power, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
for(int j=0; j<qty_rel; j++) {
digitalWrite(relay[j], HIGH);
}
}
void loop() {
power_button();
}
void power_button() {
click = digitalRead(power);
if(click == LOW && click_prev == HIGH && !block) {
click_time = millis();
block = true;
} if(click == LOW && !power_switch && (millis()-click_time) > long(button_hold)) {
if (!power_on) {
power_up();
} else {
power_down();
}
} if(click == HIGH && click_prev == LOW) {
block = false;
power_switch = false;
} click_prev = click;
}
void power_up() {
digitalWrite(relay[0], LOW);
delay(500);
digitalWrite(relay[1], LOW);
delay(500);
digitalWrite(relay[2], LOW);
power_on = true;
power_switch = true;
}
void power_down() {
digitalWrite(relay[0], HIGH);
digitalWrite(relay[1], HIGH);
digitalWrite(relay[2], HIGH);
power_on = false;
power_switch = true;
}