Skip to content

Instantly share code, notes, and snippets.

@LajtEU
Created April 25, 2021 21:01
Show Gist options
  • Save LajtEU/a1a877da4ea3897dff9029ed259f2782 to your computer and use it in GitHub Desktop.
Save LajtEU/a1a877da4ea3897dff9029ed259f2782 to your computer and use it in GitHub Desktop.
Variable Isolation Transformer
byte state230V = 0;
byte oldstate230V = 0;
byte stateREG = 0;
byte oldstateREG = 0;
byte stateCURR = 0;
byte oldstateCURR = 0;
byte stateEARTH = 0;
byte oldstateEARTH = 0;
byte stateOUT = 0;
byte oldstateOUT = 0;
byte currblinker = 0;
byte blinkcount = 0;
int ledState = HIGH;
unsigned long previousMillis = 0;
const int interval = 300;
const int interruptPin = 2;
const int OUT = 13;
volatile int currInterrupt = 0;
void setup() {
pinMode(interruptPin, INPUT); // Current Protection Sensor, Pullup resistor installed on current sensor board
pinMode(3, INPUT_PULLUP); // Input button 230V-115V
pinMode(4, INPUT_PULLUP); // Innput button Regulation Transformer
pinMode(5, INPUT_PULLUP); // Input button Current Protection
pinMode(6, INPUT_PULLUP); // Input button Gruound/Earth OFF
pinMode(7, INPUT_PULLUP); // Input button for Main Output
pinMode(8, OUTPUT); // 230V-115V relay
pinMode(9, OUTPUT); // Variac relay
pinMode(10, OUTPUT); // Ground/Earth OFF relay
pinMode(11, OUTPUT); // Current Limit LED
pinMode(12, OUTPUT); // Ground/Earth LED
pinMode(OUT, OUTPUT); // Main Output relay on pin 13
digitalWrite(8, LOW); // 230V-115V relay default state OFF
digitalWrite(9, LOW); // Variac relay default state OFF
digitalWrite(10, LOW); // Ground/Earth disconnect relay default state OFF
digitalWrite(11, LOW); // Current Limit LED default state OFF
digitalWrite(12, LOW); // Ground/Earth LED default state OFF
digitalWrite(13, LOW); // Main Output relay default state OFF
}
void loop() {
//230-115V Input button
//
if(digitalRead(3) == LOW) { //If 230/115 input pin is pressed (LOW)
state230V = 1 - state230V;
}
if(state230V == 1 && oldstate230V == 0) {
if(digitalRead(OUT) == HIGH) {
digitalWrite(OUT, LOW);
stateOUT = 1 - stateOUT;
oldstateOUT = stateOUT;
}
digitalWrite(8, HIGH);
delay(400);
}
if(state230V == 0 && oldstate230V == 1) {
if(digitalRead(OUT) == HIGH) {
digitalWrite(OUT, LOW);
stateOUT = 1 - stateOUT;
oldstateOUT = stateOUT;
}
digitalWrite(8, LOW);
delay(400);
}
oldstate230V = state230V;
//Variac Input button
//
if(digitalRead(4) == LOW) { //If Variac input pin is pressed (LOW)
stateREG = 1 - stateREG;
}
if(stateREG == 1 && oldstateREG == 0) {
if(digitalRead(OUT) == HIGH) {
digitalWrite(OUT, LOW);
stateOUT = 1 - stateOUT;
oldstateOUT = stateOUT;
}
digitalWrite(9, HIGH);
delay(400);
}
if(stateREG == 0 && oldstateREG == 1) {
if(digitalRead(OUT) == HIGH) {
digitalWrite(OUT, LOW);
stateOUT = 1 - stateOUT;
oldstateOUT = stateOUT;
}
digitalWrite(9, LOW);
delay(400);
}
oldstateREG = stateREG;
//Main Output - Input button
//
if(digitalRead(7) == LOW) { //If Output ON/OFF input pin is pressed (LOW)
stateOUT = 1 - stateOUT;
}
if(stateOUT == 1 && oldstateOUT == 0) {
digitalWrite(OUT, HIGH);
delay(400);
}
if(stateOUT == 0 && oldstateOUT == 1) {
digitalWrite(OUT, LOW);
delay(400);
}
oldstateOUT = stateOUT;
//Earth(Grounding) Input button
//
if(digitalRead(6) == LOW) { //If GND OFF input pin is pressed (LOW)
stateEARTH = 1 - stateEARTH;
}
if(stateEARTH == 1 && oldstateEARTH == 0) {
digitalWrite(10, HIGH);
digitalWrite(12, HIGH);
delay(400);
}
if(stateEARTH == 0 && oldstateEARTH == 1) {
digitalWrite(10, LOW);
digitalWrite(12, LOW);
delay(400);
}
oldstateEARTH = stateEARTH;
//Current Protection Input button
//
if(digitalRead(5) == LOW) { //If Current Limit input pin is pressed (LOW)
stateCURR = 1 - stateCURR;
}
if(stateCURR == 1 && oldstateCURR == 0) {
currInterrupt = 1;
digitalWrite(11, HIGH);
attachInterrupt(digitalPinToInterrupt(interruptPin), currprot , LOW);
delay(400);
}
if(stateCURR == 0 && oldstateCURR == 1) {
currInterrupt = 0;
digitalWrite(11, LOW);
detachInterrupt(digitalPinToInterrupt(interruptPin));
delay(400);
}
oldstateCURR = stateCURR;
//Curent LED blinking routine
//
if (currblinker == 1 && blinkcount <= 7) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // save the last time you blinked the LED
if (ledState == HIGH) {
ledState = LOW;
} else {
ledState = HIGH;
}
// set the LED with the ledState of the variable:
digitalWrite(11, ledState);
blinkcount = blinkcount + 1;
}
}
else {
currblinker = 0;
blinkcount = 0;
}
}
//Current Protection Sensor Interrupt Routine
//
void currprot() {
if(digitalRead(OUT) == HIGH) {
digitalWrite(OUT, LOW);
stateOUT = 1 - stateOUT;
oldstateOUT = stateOUT;
currblinker = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment