Trainset info,
Turnout 1 = relay 1 - makes turnout track go left
= relay 2 - makes turnout track go right
Turnout 2 = relay 3 - makes turnout track go left
= relay 4 - makes turnout track go right
Signal 1 = relay 5 - makes signal go green - train 1 starts driving
= relay 6 - makes signal go red - train 1 stops for signal
Signal 2 = relay 7 - makes signal go green - train 2 starts driving
= relay 8 - makes signal go red - train 2 stops for signal
I have a arduino - uno and a 8 relay board. I want to run a program (loop) for 1/2 hour / 1800 seconds. Switching time should be 2 seconds (relay ON)
Can somebody help me with a millis code please.
Loop times should be:
relay 1 = (2),1250,(2),546 - total 1800 seconds
relay 2 = 620,(2),630,(2),546 - total 1800 seconds
relay 3 = 10,(2)1260,(2),526 - total 1800 seconds
relay 4 = 630,(2),640,(2),512 - total time 1800 seconds
relay 5 = 20,(2),1270,(2),505 - total time 1800 seconds
relay 6 = 600,(2),750,(2),446 - total time 1800 seconds
relay 7 = 640,(2),640,(2),516 - total time 1800 seconds
relay 8 = 1240,(2),450,(2),106 - total time 1800 seconds
Regards, Martinus van Walsum [email protected]
1 Answer 1
This is functional. I would do more by making a function to be called with the time and state information. It would reduce the number of if statements. But this is simple to understand. I started with Blink without delay.
I did two relays. For the others, copy and paste, then change the relay numbers and times.
/*
Blink without Delay
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
const int On = 1;
const int Off = 0;
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
const int relay1 = 5;
const int relay2 = 6;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
int secondCount = 0;
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
#define onTime 2 //relay on time
#define relay1_on1 0
#define relay1_on2 (relay1_on1+onTime+1250)
#define relay2_on1 62
#define relay2_on2 (relay2_on1+onTime+630)
void setup() {
// set the digital pin as output:
digitalWrite(relay1, Off);
digitalWrite(relay2, Off);
pinMode(ledPin, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
//------------------------------
// Relay 1
if (secondCount == relay1_on1) {
digitalWrite(relay1, On);
}
if (secondCount == (relay1_on1 + 2)) {
digitalWrite(relay1, Off);
}
if (secondCount == relay1_on2) {
digitalWrite(relay1, On);
}
if (secondCount == (relay1_on2 + 2)) {
digitalWrite(relay1, Off);
}
//------------------------------
// Relay 2
if (secondCount == relay2_on1) {
digitalWrite(relay2, On);
}
if (secondCount == (relay2_on1 + 2)) {
digitalWrite(relay2, Off);
}
if (secondCount == relay2_on2) {
digitalWrite(relay2, On);
}
if (secondCount == (relay2_on2 + 2)) {
digitalWrite(relay2, Off);
}
//------------------------------
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
secondCount++;
if (secondCount == 180) {
secondCount = 0;
}
}
}
BlinkWithoutDelay
example, which shows, how this coding style works? Have you read some of the many tutorials for this on the web? Currently I don't know, how I could help you, without having to write the whole code myself.