Hi
Would anyone be able to help with some errors i have with an array sequence using 2 separate led arrays with separate interval and off times.
The program works but not quite as expected, At startup the 2 separate arrays seems to start from position 2 not 1 so iv'e added extra rows for this and at the end of each sequence there is a a few second delay until it start over again where i want it to go from sequence 6 say after off time straight onto sequence 1, i don'Use code tags to format code for the forum
t think this is the most efficient way to run 2 separate arrays but any help would be appreciated. Thanks
//initialize Global variables
unsigned long CurrentMillis = 0; // mari current millis
unsigned long PreviousMillis = 0; // mari previous millis
unsigned long CurrentMillis1 = 0; // CLS current millis
unsigned long PreviousMillis1 = 0; // CLS previous millis
int TimeState = 0; // Mari Timestates: 0 waiting, 1 open all relays, 2 wait for off perio, 3 set relays
int TimeState1 = 0; // CLS timestates: 0 waiting, 1 open all relays, 2 wait for off perio, 3 set relays
int Mari_interval = 4000; // Mari interval time
int Cls_interval = 7000; // Cls interval time
int Mari_OffTime = 700; // Mari all led Off time when incrementing to the next led
int Cls_OffTime = 700; // CLS all led Off time when incrementing to the next led
int Mari_Seq = 0; // sequence location
int Cls_Seq = 0; // sequence location
int Mari_crossing = 0; // Mari transitio time after reaching max count and srting at the beginning again.
int Cls_crossing = 0; // CLS transitio time after reaching max count and srting at the beginning again.
int Mari_Index = 0; // Mari index position
int Cls_Index = 0; // CLS index position
int MariPins[6] = { 2, 3, 4, 5, 6, 7 }; // Mari on D2,3,4,5,6,7
int ClsPins[3] = {8, 9, 10 }; // CLS on D8,9,10
//7 rows and 9 columns
int Mari_Array[8][6] = {
//1 2 3 4 5 6
{ 0, 0, 0, 0, 0, 0,}, //0 first row is Zero no aspects will be set
{ 0, 0, 0, 0, 0, 0,}, // start at Row 1
{ 1, 0, 0, 0, 0, 0,}, //2
{ 0, 1, 0, 0, 0, 0,}, //3
{ 0, 0, 1, 0, 0, 0,}, //4
{ 0, 0, 0, 1, 0, 0,}, //5
{ 0, 0, 0, 0, 1, 0,}, //6
{ 0, 0, 0, 0, 0, 1,}, //7
};
int Cls_Array[5][3] = {
// columns
//1 2 3
{ 0, 0, 0 }, //0 first row is Zero no aspects will be set
{ 0, 0, 0 }, //1 start at row 1
{ 1, 0, 0,}, //2 the code seems to sart here at startup instead of 1 so an extra row has been added to counteract this.
{ 0, 1, 0 }, //3
{ 0, 0, 1 }, //4
};
// Timer Functions
void Mari_Timers() { // mari timers
CurrentMillis = millis();
if (PreviousMillis > CurrentMillis) //timer roll over condition
{
PreviousMillis = 0;
}
if (TimeState == 0) // if the timer is waiting to roll over
{
if (CurrentMillis - PreviousMillis >= Mari_interval) {
PreviousMillis = CurrentMillis;
Mari_crossing = Mari_crossing + 1;
if (Mari_crossing > 5) {
Mari_crossing = 0;
}
TimeState = 1; // open all relays
}
}
if (TimeState == 2) // waiting for the relay off period to expire
{
if (CurrentMillis - PreviousMillis >= Mari_OffTime) {
PreviousMillis = CurrentMillis;
TimeState = 3; // off time has expired so close selected relays, currently active low
}
}
}
void Cls_Timers() { // Cls timers with the led states for on off times
CurrentMillis1 = millis();
if (PreviousMillis1 > CurrentMillis1) //timer roll over condition
{
PreviousMillis1 = 0;
}
if (TimeState1 == 0) // if the timer is waiting to roll over
{
if (CurrentMillis1 - PreviousMillis1 >= Cls_interval) {
PreviousMillis1 = CurrentMillis1;
Cls_crossing = Cls_crossing + 1;
if (Cls_crossing > 5) {
Cls_crossing = 0;
}
TimeState1 = 1; // open all relays
}
}
if (TimeState1 == 2) // waiting for the relay off period to expire
{
if (CurrentMillis1 - PreviousMillis1 >= Cls_OffTime) {
PreviousMillis1 = CurrentMillis1;
TimeState1 = 3; // off time has expired so close selected relays, currently active low
}
}
}
void Mari_Relays() {
for (int i = 0; i < 6; i++) {
if (TimeState == 2) {
digitalWrite(MariPins[i], HIGH); // turn off all relays during off cycle as the relay board is active low inputs
} else {
digitalWrite(MariPins[i], !Mari_Array[Mari_Index][i]); // set the required relay outputs LOW, currently active Low with used relay boards
}
}
}
void Cls_Relays() {
for (int i = 0; i < 3; i++) {
if (TimeState1 == 2) {
digitalWrite(ClsPins[i], HIGH); // turn off relays during off cycle
} else {
digitalWrite(ClsPins[i], !Cls_Array[Cls_Index][i]); // set the required relays, currently active Low with used relay boards
}
}
}
void setup() {
//Setup Relay Output Pins
for (int i = 0; i < 6; i++) pinMode(MariPins[i], OUTPUT); // set array pins as outputs
for (int i = 0; i < 6; i++) digitalWrite(MariPins[i], HIGH); // set all array outputs low AT STARTUP
Mari_Index = 0;
for (int i = 0; i < 3; i++) pinMode(ClsPins[i], OUTPUT); // set array pins as outputs
for (int i = 0; i < 3; i++) digitalWrite(ClsPins[i], HIGH); // set all array outputs low AT STARTUP
Cls_Index = 0;
}
void loop() {
Mari_Timers(); // Auto sequence structure below for the Mari Timer Manager
if (TimeState == 1) // time interval has passed open all relays
{
Mari_Index = 0;
Mari_Relays();
TimeState = 2;
}
if (TimeState == 3) // time interval has passed set required relays
{
Mari_Seq = Mari_Seq + 1;
if (Mari_Seq >= 7) //max count required
{
Mari_Seq = 0;
}
{
Mari_Index = Mari_Seq + 1; // increment sequence
}
Mari_Relays();
TimeState = 0;
}
Cls_Timers(); // Auto sequence structure below for the Cls Timer Manager
if (TimeState1 == 1) // time interval has passed open all relays
{
Cls_Index = 0; // cls index
Cls_Relays();
TimeState1 = 2;
}
if (TimeState1 == 3) // time interval has passed set required relays
{
Cls_Seq = Cls_Seq + 1;
if (Cls_Seq >= 4) //max count required
{
Cls_Seq = 0;
}
{
Cls_Index = Cls_Seq + 1; // increment sequence
}
Cls_Relays();
TimeState1 = 0;
}
}
// works but only starts at 2 i the array and after the last count of 6 example then there is a delay of the given delay time.