Cycling through cascaded Loops???

Good day all.

I have been here before and I have had some great success, even if it has taken some time. I am still a newbie, but I am working very hard at learning, when my day job doesn’t get in the way. My issue now is that I have taken the advice of those who have responded, great though it was, I am once again stuck. The following is a set of instructions that I need to look at and base a decision on a counter to determine which loop to run through. As you can see I am using a set of cascade “if” statements. I am stuck on two things:
1 - Is my syntax for the cascading “if” statements correct?
2 - I can’t seem to get the follow-on “voids” to run properly based on the count.
3 - I appears to be also continuously running through the start, gate and abort cycle, but does not allow the “start” loop to cylce for the requisite 5350 millis (5.35 sec).

After several hours of debugging if is now late here in Canada. I am hoping that someone can take a look at the code and tell me if I am making simple mistakes and what that may be? Again, a newbie here and I am truely am trying to learn. Any advice or constructive comments would be greatly appreciated!

Thanks,
Mike

// Definition of variables that change

int outPin = 1;			      // communication line to the MP3 Trigger
int startpbVal = 0;                   // variable for reading the Start PB state
int startpbState = 0;                 // variable for holding the start PB state 
int gateupVal = 0;                    // variable for reading the Start PB state
int gateupState = 0;                  // variable for holding the start PB state 
volatile int startpbcount = 0;        // initialize start counter at 0
volatile int gateupcount = 0;         // initialize gate up counter at 0
unsigned long startPBtimer;	      // this holds the time from when the startPB was first pushed

// Pin assignments
const int startpbPin = 2;                   // Start pushbutton
const int gateupPin = 3;                    // Initialization/Reset sequence pushbutton
const int grnledPin = 5;                    // Green LED on the light tree
const int yelled2Pin = 6;                   // Yellow #2 LED on the light tree
const int yelled1Pin = 7;                   // Yellow #1 LED on the light tree
const int redledPin = 8;                    // Red LED on the light tree
const int upsolPin = 9;                     // Solenoid that holds the gate up
const int dwsolPin = 10;                    // Solenoid that holds the gate down
const unsigned long startMP3 = 5350;
long randNumber;                            // sets variable rand "random" to a long integer.

void setup() {    

  Serial.begin(9600);                       //Initialize serial communications
  pinMode(outPin, OUTPUT);                  //transmit info to MP3 Trigger to play specified track
  pinMode(redledPin, OUTPUT);               //Output to Red LED transistor for LED on light tree
  pinMode(yelled1Pin, OUTPUT);              //Output to Yellow1 LED transistor for LED on light tree   
  pinMode(yelled2Pin, OUTPUT);              //Output to Yellow2 LED transistor for LED on light tree
  pinMode(grnledPin, OUTPUT);               //Output to Green LED transistor for LED on light tree
  pinMode(upsolPin, OUTPUT);                //Output to Gate Up Solenoid transistor 
  pinMode(dwsolPin, OUTPUT);                //Output to Gate Down Solenoid transistor
  pinMode(startpbPin, INPUT);               //start and abort pushbutton (abort not functioning yet)
  pinMode(gateupPin, INPUT);                //gate up PB which functions only once for now.
  startPBtimer = millis();                  //sets the startPBtimer to record milliseconds
  digitalWrite(upsolPin, LOW);              //Part of the command to drive the solenoid down so that the gate doesn't go up out of control
  digitalWrite(dwsolPin, HIGH);             //part of the command to drive the solenoid down so that the gate doesn't go up out of control
}

 void loop() {
  
   startpbVal = digitalRead(startpbPin);      // read input value and store it in startpbVal
   if (startpbVal != startpbState){           // start button state has changed!
	  startpbcount = startpbcount++;}     // add one to the startpbcount
   if (startpbcount = 1){		      // checks for first startPB press 
       start();  			      // goes to start subroutine
     }
   
   if (startpbcount = 2){
     if (startPBtimer >= 5350){		      //checks for first startPB variable = 2 and that the timer is equal or above the required delay to play the MP3 			
        gate();                               // goes to the gate drop subroutine
     }
   }
  if (startpbcount = 3){
     if ( (millis()-startPBtimer)<= startMP3){  // checks for second startPB press 
       abort();					// and goes to abort subroutine
      }
   }
  
    gateupVal = digitalRead(gateupPin);          //read input value and store it in gateupVal
   if (gateupVal != gateupState){           	//start button state has changed!
        gateupcount= gateupcount++;              // adds one to the gate up counter
   if (startpbcount <1){
     if (gateupcount = 1){                     //checks to see if the startPB has been pushed and if the gateUP PB has been pushed 
        gateup();		               //goes to gateup subroutine
    }
   }
  }
 }
void gateup() - removed because I exceed the character limits
  
void start(){
//starts the start commands MP3
   Serial.begin(38400); 
   startPBtimer = millis();	// this captures when the start PB was first pushed. Used in the abort sequence.
   Serial.write('t');		//lowercase "t" must be in single quotes…
   Serial.write(1);		//commands MP3 trigger to play track001
   startpbcount = startpbcount++;  // adds one to the start PB counter to allow the "Gate" void to run (from the void loop)
   return;			//return from start routine
}

void gate() - I know it works and it exceeds the limits of the post..except after the code I have left the resets for the variables.
....
   startpbcount = 0;			        //resets the startpbcount  counter back to zero so that the sequence can start again.
   startpbVal = 0;                              // variable for holding the start PB state  
   startpbState = 0;                            // State for holding the start PB state
   return;				        //return from gate drop routine
}

void abort() {
   Serial.begin(38400); 
   Serial.write('t');                    //command to mp3 trigger to initialize
   Serial.write(4);                      //command to mp3 trigger to play track 4
   digitalWrite(redledPin, HIGH);        //while playing abort tone, hold red light on
   delay(1000);
   startpbcount = 0;			        //resets the startpbcount  counter back to zero so that the sequence can start again.
   startpbVal = 0;                              // variable for holding the start PB state  
   startpbState = 0;                            // State for holding the start PB state;
   digitalWrite(redledPin, LOW);         //turns the red LED off
   return;                               //exit abort routine
}

All these are wrong:

   if (startpbcount = 2){

The test for equals is "==" as in:

   if (startpbcount == 2){

You have a few of those.


   Serial.begin(38400);

Why is that everywhere? Once in setup() is enough.


   return;         //return from start routine

Those returns are unnecessary. Functions always return. What else can they do?

I usually break things up into functions, make it easier to focus on the task, and to recycle

Those returns are unnecessary. Functions always return. What else can they do?

That is kind of old hat proper thinking, functions didnt always return, but yea you dont need them (for a long time now, unless actually returning a value)

Thanks Nick!

I guess I was confused (blinded) by variables and not thinking about the evaluation.

Thanks again!