Ok, first things first - I am new here. This is my first post (WAY TOO LONG, I know). I am new to arduino and C++. I was pretty good at BASIC (old guy). I really do try and work things out myself, before asking for help (not always the best option).
I DID read thru the excellent writings of Robin2 "SeveralThingsAtTheSameTime" and UKHeliBob's "Using millis()...". Actually I read them more than a few times. I also read other articles concerning using millis() for timing......So oh wonderful teachers............I did my homework but still need help (this aging thing can be sooo annoying). As one poster mentioned - I guess I AM having difficulty wrapping my head around this concept.
This sketch will be used for two model train lines running parallel. Trains run both directions. Each set of signals should operate independently of each other - eventually, there will be an interchange crossing between the two lines, so they will be integrated after I get the sketch below working correctly.
What I am trying to do:
- set up block signals for a model railroad. These signals are activated by an IR detection sensor embedded in the tracks. For each sensor there is a set of signal lights (RED, YELLOW, GREEN). There is a set of sensors and signals for each direction of travel (left or right). I am only trying to change the light sequence when a train activates a sensor, not stop the trains. I have two parallel tracks that I am putting signals on. Each track currently operates independently of each other. I know I can use a separate Arduino for each track, but am trying to combine the process in one Arduino, because there will eventually be an interchange crossing between the two tracks, requiring the signals to work together.
The attached code works for the two sets of parallel track I am using. Sensors cause the signal lights to change correctly.
What I am having trouble with:
- When one set of sensors is activated, the other set will not activate (why I am trying to use millis() to "do two things at once") until the first sequence is complete.
- I cannot get a "delay" of 5 seconds after the sequence goes to "YELLOW".
I included a description at the top of the sketch.
Any assistance would be greatly appreciated. Ben
/* This sketch will be used for two model train lines running
* parallel. Trains run both directions. Each set of signals
* should operate independently of each other - eventually,
* there will be an interchange crossing between the two lines,
* so they will be integrated after I get the skecth below
* working correctly.
*
* Setup is two sets of IR detection sensors that each set
* triggers GREEN,RED,YELLOW lights - depending on which
* sensor is activated first.
* The valA1, valA2, etc. are reading the values of each sensor
* and the value is what triggers the "if" statements. These Sensors
* usually are outputing a number in the 900's when
* not activated, and in the low 100's when activated.
* GREEN is default ON state (sensors are common anode,
* hence HIGH is off)
* working example: if Left sensor activated,
* RED turns on, GREEN off. RED stays on as long as
* Left sensor is activated
* Once Right sensor is activated, and left is sensor is
* deactivated, YELLOW turns on, RED off
* Once both sensors are deactivated, there should be
* a 5 second delay, then GREEN goes on
*
* this currently works for both sets of sensors but
* only one is triggered at a time, and
* there is no "delay" between yellow and back to green.
* I want a 5 second delay between light going from YELLOW
* back to GREEN.
*/
int sensePin1 = A0;
int sensePin2 = A1;
int RED = 6;
int YELLOW = 5;
int GREEN = 3;
int sensePin3 = A2;
int sensePin4 = A3;
int RED4 = 14;
int YELLOW4 = 15;
int GREEN4 = 16;
int POWER = 8;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const int interval = 5000;
void setup() {
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(POWER, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(YELLOW4, OUTPUT);
pinMode(GREEN4, OUTPUT);
pinMode(POWER, OUTPUT);
}
enum SIGNALSTATES
{
ST_GREEN,
ST_RED1,
ST_RED2,
ST_YELLOW,
ST_GREEN4,
ST_RED4,
ST_RED4a,
ST_YELLOW4
};
SIGNALSTATES signalState = ST_GREEN;
void loop() {
currentMillis = millis();
int valA1 = analogRead(sensePin1);
int valA2 = analogRead(sensePin2);
int valA3 = analogRead(sensePin3);
int valA4 = analogRead(sensePin4);
Serial.println(valA1);
Serial.println(valA2);
Serial.println(valA3);
Serial.println(valA4);
switch (signalState)
{
case ST_GREEN:
signalgreen(valA1, valA2, valA3, valA4);
break;
case ST_RED1:
signalred1(valA1, valA2);
break;
case ST_RED2:
signalred2(valA1, valA2);
break;
case ST_YELLOW:
signalyellow(valA1, valA2);
break;
case ST_GREEN4:
signalgreen4(valA3, valA4);
break;
case ST_RED4:
signalred4(valA3, valA4);
break;
case ST_RED4a:
signalred4a(valA3, valA4);
break;
case ST_YELLOW4:
signalyellow4(valA3, valA4);
break;
}
}
void signalgreen(int valA1, int valA2, int valA3, int valA4) {
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, HIGH);
digitalWrite(POWER, HIGH);
digitalWrite(RED4, HIGH);
digitalWrite(GREEN4, LOW);
digitalWrite(YELLOW4, HIGH);
if (valA1 < 500 && valA2 > 500) {
signalState = ST_RED1;
}
else if (valA1 > 500 && valA2 < 500) {
signalState = ST_RED2;
}
if (valA3 < 500 && valA4 > 500) {
signalState = ST_RED4;
}
else if (valA3 > 500 && valA4 < 500) {
signalState = ST_RED4a;
}
}
void signalred1(int valA1, int valA2) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(POWER, HIGH);
if (valA1 > 500 && valA2 < 500) {
signalState = ST_YELLOW;
}
}
void signalred2(int valA1, int valA2) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(POWER, HIGH);
if (valA1 < 500 && valA2 > 500) {
signalState = ST_YELLOW;
}
}
void signalyellow(int valA1, int valA2) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(POWER, HIGH);
if (valA1 > 500 && valA2 > 500) {
if (currentMillis - previousMillis >= interval)
signalState = ST_GREEN;
previousMillis += currentMillis;
}
}
void signalgreen4(int valA3, int valA4) {
digitalWrite(GREEN4, LOW);
digitalWrite(RED4, HIGH);
digitalWrite(YELLOW4, HIGH);
digitalWrite(POWER, HIGH);
if (valA3 < 500 && valA4 > 500) {
signalState = ST_RED4;
}
else if (valA3 > 500 && valA4 < 500) {
signalState = ST_RED4a;
}
}
void signalred4(int valA3, int valA4) {
digitalWrite(GREEN4, HIGH);
digitalWrite(RED4, LOW);
digitalWrite(YELLOW4, HIGH);
digitalWrite(POWER, HIGH);
if (valA3 > 500 && valA4 < 500) {
signalState = ST_YELLOW4;
}
}
void signalred4a(int valA3, int valA4) {
digitalWrite(GREEN4, HIGH);
digitalWrite(RED4, LOW);
digitalWrite(YELLOW4, HIGH);
digitalWrite(POWER, HIGH);
if (valA3 < 500 && valA4 > 500) {
signalState = ST_YELLOW4;
}
}
void signalyellow4(int valA3, int valA4) {
digitalWrite(GREEN4, HIGH);
digitalWrite(RED4, HIGH);
digitalWrite(YELLOW4, LOW);
digitalWrite(POWER, HIGH);
if (valA3 > 500 && valA4 > 500) {
if (currentMillis - previousMillis >= interval)
signalState = ST_GREEN;
previousMillis += currentMillis;
}
}