Hello,
I am a beginner in writing code in Arduino, so I will be glad to see your help with my project. In short, I don't know exactly how to program millis function so it would work.
Short description of what I would like my program to do:
I am using 1x 24VAC relay and 2x 2 Relay modules. With 2x 2 Relay modules I control the connections to 24VAC relay. At first, the 24V relay is in off state and I would like to measure its coil resistance at that time. Before relay turns on, I would like to disconnect the connections from coil to arduino with Module relays, turn relay on and then check if +5V is passed through the poles of 24V relay. 24V Relay is off for 300ms and then on for 300ms, in a loop.
What relays do:
- Relay 2 turns on to provide connection between A1 of 24V relay (first side of coil) and +5V of Arduino
- Relay 4 turns on to provide connection between A2 of 24V relay (second side of coil) and Analog A0 of Arduino
- Relay 1 turns on to provide connection between A1 of 24V relay (first side of coil) and one connection of 24AC
- Relay 3 turns on to provide connection between A1 of 24V relay (second side of coil) and one connection of 24AC
I would like my code to run like this:
- 000ms: Program start
- 100ms: Turn relay 2 and 4 on
- 150ms: Measure resistance of 24V Relay's coil
- 200ms: Turn relay 2 and 4 off
- 300ms: Turn relay 1 and 3 on
- 450ms: Check if poles of 24V Relay are all on
- 600ms: Turn relay 1 and 3 off
- 600ms: Restart the program
My code so far:
// inputs and outputs
#define relay1 8
#define relay2 9
#define relay3 10
#define relay4 11
#define Switch1Pole1 47
#define Switch1Pole2 49
#define Switch1Pole3 51
#define Switch1Pole4 53
int readResistance = 0;
// constants
const unsigned long interval1 = 100;
const unsigned long interval2 = 150;
const unsigned long interval3 = 200;
const unsigned long interval4 = 300;
const unsigned long interval5 = 450;
const unsigned long interval6 = 550;
const unsigned long interval7 = 600;
// variables
unsigned long TimeBefore1 = 0;
unsigned long TimeBefore2 = 0;
unsigned long TimeBefore3 = 0;
unsigned long TimeBefore4 = 0;
unsigned long TimeBefore5 = 0;
unsigned long TimeBefore6 = 0;
int pinstate1 = 0;
int pinstate2 = 0;
int pinstate3 = 0;
int pinstate4 = 0;
unsigned int counter1 = 0;
unsigned int counter2 = 0;
unsigned int counter3 = 0;
unsigned int counter4 = 0;
int analogPin = 0;
int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 900;
float R2 = 0;
float buffer = 0;
//program
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
pinMode(Switch1Pole1, INPUT_PULLUP);
pinMode(Switch1Pole2, INPUT_PULLUP);
pinMode(Switch1Pole3, INPUT_PULLUP);
pinMode(Switch1Pole4, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
unsigned long TimeNow = millis();
if (TimeNow - TimeBefore1 >= interval1) {
TimeBefore1 = TimeNow+500;
digitalWrite(relay2, LOW);
digitalWrite(relay4, LOW);
}
if (TimeNow - TimeBefore2 >= interval2) {
TimeBefore2 = TimeNow+450;
raw = analogRead(analogPin);
if(raw){
buffer = raw * Vin;
Vout = (buffer)/1024.0;
buffer = (Vin/Vout) - 1;
R2= R1 * buffer;
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("R2: ");
Serial.println(R2);
}
}
if (TimeNow - TimeBefore3 >= interval3) {
TimeBefore3 = TimeNow+400;
digitalWrite(relay2, HIGH);
digitalWrite(relay4, HIGH);
}
if (TimeNow - TimeBefore4 >= interval4) {
TimeBefore4 = TimeNow+300;
digitalWrite(relay1, LOW);
digitalWrite(relay3, LOW);
}
if (TimeNow - TimeBefore5 >= interval5) {
TimeBefore5 = TimeNow+150;
if (digitalRead(Switch1Pole1) == HIGH) {
pinstate1 = 1;
counter1 = counter1 + 1;
}
else {
pinstate1 = 0;
}
Serial.print("Pinstate: ");
Serial.println(pinstate1);
Serial.print("Counter: ");
Serial.println(counter1);
}
if (TimeNow - TimeBefore6 >= interval7) {
TimeBefore6 = TimeNow;
digitalWrite(relay1, HIGH);
digitalWrite(relay3, HIGH);
}
}