First of all, thanks to LarryD for the help with this piece of code.
Now.. I tried implementing the state machine as a separate function to be called x number of times in the loop(). First I tried to just call it in the loop, expecting the function to run over and over again. But it doesn't work. I get the same result if I try to run it without a limitation on the number of times, as I do if I set an amount of times to run.
What happes is that it calls the function, but somehow it gets stuck after turning on the "nop" variable assigned to pin10 and never gets to the point of turning on rly1, 2 and finally 3. Anyone have any idea why?
It would be ideal for me to be able to run the program like this, as I need an Autorun mode, and a secondary manual mode, where buttons control the action.
//State Machine
const byte nop = 10;
const byte rly1 = 11;
const byte rly2 = 12;
const byte rly3 = 13;
const byte button = 2;
int buttonState;
unsigned long currentMillis;
unsigned long RLYmillis;
unsigned long RLYdelay = 3000;
byte currentState;
void setup()
{
pinMode(nop, OUTPUT);
digitalWrite(nop,LOW);
pinMode(rly1, OUTPUT);
digitalWrite(rly1,LOW);
pinMode(rly2, OUTPUT);
digitalWrite(rly2,LOW);
pinMode(rly3, OUTPUT);
digitalWrite(rly3,LOW);
pinMode(button, INPUT);
//RLYmillis = millis(); //Initialize the time
//digitalWrite(nop,HIGH); //start out with this LED on
//currentState = 1; //we start out in this machine state
} // >>>>>>>>>>>>>> E N D O F s e t u p ( ) <<<<<<<<<<<<<<<<<
void loop()
{
automode(2);
}// >>>>>>>>>>>>>> E N D O F l o o p ( ) <<<<<<<<<<<<<<<<<
void automode (int times){
for (int i =0; i<times; i++){
RLYmillis = millis(); //Initialize the time
digitalWrite(nop,HIGH); //start out with this LED on
currentState = 1; //we start out in this machine state
switch (currentState)
{
//***************************
case 1:
if (millis() - RLYmillis >= RLYdelay)
{
digitalWrite(nop,LOW);
digitalWrite(rly1,HIGH);
//Change to the next state
RLYmillis = millis();
currentState = 2;
}
break;
//***************************
case 2:
if (millis() - RLYmillis >= RLYdelay)
{
digitalWrite(rly1,LOW);
digitalWrite(rly2,HIGH);
//Change to the next state
RLYmillis = millis();
currentState = 3;
}
break;
//***************************
case 3:
if (millis() - RLYmillis >= RLYdelay)
{
digitalWrite(rly2,LOW);
digitalWrite(rly3,HIGH);
//Change to the next state
RLYmillis = millis();
currentState = 4;
}
break;
//***************************
case 4:
if (millis() - RLYmillis >= RLYdelay)
{
digitalWrite(rly3,LOW);
digitalWrite(nop,HIGH);
//Change to the next state
RLYmillis = millis();
currentState = 1; //Back to state 1
}
break;
//***************************
default:
{
}
} // END of switch/case
} //Auto
}
//======================================================================
// E N D O F C O D E
//======================================================================