Help with a sub function

I’m missing something in my buzzer routine at the end of my code.

So far most everything is working as well as I’d hoped with my first FSM. I’m trying to set up a door alarm for when the door closes. The idea is that the tone will sound five times, when the countdown reaches zero the code goes back to where it came from.

What it is doing however is simply turning on the buzzer then going back to run the machine. Some how it never gets to the part that cycles the buzz routine THEN returns to the code.

Thanks for the help.

// Use names for states so it's easier to
// understand what the code is doing
// Possible states
const int S_DOOROPEN = 0;
const int S_WAIT4DARK = 1;
const int S_DOORCLOSED = 2;
const int S_WAIT4DAY = 3;
const int S_OVERRIDE = 4;

// Pin numbers

const int Door12VC = 11;   // Accuates Door Open 12v constant:
const int Door12VS = 12;   // Accuates Door Open 12V Switched:
const int SolarSw = A0;  // Solar switch - daylight sensor:
const int SMpwr = 9;     //feedpower to SM Pot
const int SMFeedback = A1; //read SMpot output
const int Buzz = 3;       // Audible feedback:
const int DSMan = 8;      // Manual Door Switch:
unsigned int solarLvl = 0U; // variable to Store daylight value
static unsigned long ts;   // To store the current time  for delays
int SM = 0;               //Sunlightmultiplier from potentiometer
int DoorMaxTrvlTime = 5000; //5 sec for door to open_close:
int state = S_DOORCLOSED;  //stores the current state
int CD = 5;               //build count down for buzzer time


void setup()
{
  Serial.begin(9600);  // Set baud rate to communicate
  pinMode(Door12VC, OUTPUT);
  pinMode(Door12VS, OUTPUT);
  pinMode(SolarSw, INPUT);
  pinMode(SMpwr, OUTPUT);
  pinMode(SMFeedback, INPUT);
  pinMode(Buzz, OUTPUT);
  pinMode(DSMan, INPUT);

  // makes sure door is closed when starting
  int Tone;
  Tone = buzzerroutine();
  digitalWrite( Door12VC, LOW);
  digitalWrite( Door12VS, LOW);
  delay(DoorMaxTrvlTime);// time to extend door closer
  digitalWrite( Door12VC, HIGH);
  digitalWrite( Door12VS, HIGH); //turn off closer relays
  // The keyword "static" makes sure the variable
  // isn't destroyed after each loop
  static int state = S_DOORCLOSED ;
}

void loop()
{
  digitalRead(DSMan);
  if (DSMan == LOW) (state = S_OVERRIDE);
  solarLvl = analogRead(SolarSw);   // Stores value of sunlight:
  digitalWrite(SMpwr, HIGH); //Power solar multiplier
  analogRead(SMFeedback);
  digitalWrite(SMpwr, LOW); //depower multiplier
  solarLvl = (solarLvl * SMFeedback);
  Serial.print("Solar Level with Multiplier = ");
  Serial.println(solarLvl);
  delay(500);

  switch (state)
  {
    case S_DOORCLOSED:
      // doorclosed
      int Tone;
      Tone = buzzerroutine();
      digitalWrite( Door12VC, LOW);
      digitalWrite( Door12VS, LOW);
      delay(DoorMaxTrvlTime);// time to extend door closer
      digitalWrite( Door12VC, HIGH);
      digitalWrite( Door12VS, HIGH); //turn off closer relays

      state = S_WAIT4DAY;

      break;

    case S_WAIT4DAY:
      // wait for solarlevel to go above setpoint
      if (solarLvl > 200) {
        state = S_DOOROPEN;
      }

      break;

    case S_DOOROPEN:
      // dooropen
      digitalWrite( Door12VC, LOW);
      digitalWrite( Door12VS, HIGH);
      delay(DoorMaxTrvlTime);// time to retract door closer
      digitalWrite( Door12VC, HIGH);
      digitalWrite( Door12VS, HIGH); //turn off closer relays

      state = S_WAIT4DARK;  // Move to the next state

      break;

    case S_WAIT4DARK:
      // Wait for solar level to drop below set point.
      if (solarLvl < 100)
      {
        state = S_DOORCLOSED;
      }

      break;

    case S_OVERRIDE:
      // Cycle door based on push button switch.
      //what to do for override if the door is closed
      digitalRead(DSMan);
      if ((DSMan == LOW) && (state == S_DOORCLOSED || state == S_WAIT4DAY));
      { // dooropen

        digitalWrite( Door12VC, LOW);
        digitalWrite( Door12VS, HIGH);
        delay(DoorMaxTrvlTime);// time to retract door closer
        digitalWrite( Door12VC, HIGH);
        digitalWrite( Door12VS, HIGH); //turn off closer relays;
        digitalRead(DSMan); //check for cancel of the override
        if (DSMan == LOW) {
          (state = S_WAIT4DARK);
        }
        else {
          (state = S_OVERRIDE);
        }
      }

      //what to do for override if the door is open
      if ((DSMan == LOW) && (state == S_DOOROPEN || state == S_WAIT4DARK));
      { // doorclose
        int Tone;
        Tone = buzzerroutine();
        digitalWrite( Door12VC, LOW);
        digitalWrite( Door12VS, LOW);
        delay(DoorMaxTrvlTime);// time to retract door closer
        digitalWrite( Door12VC, HIGH);
        digitalWrite( Door12VS, HIGH); //turn off closer relays
        digitalRead(DSMan);// check for cancel of the override
        if (DSMan == LOW) {
          (state = S_WAIT4DAY);
        }
        else {
          (state = S_OVERRIDE);
        }
      }

  }

}

// Subroutine for Buzzer
int buzzerroutine()
{
  ts = millis();                        // record current time
  digitalWrite(Buzz, HIGH);             // sound the buzzer
  if ((millis >= (ts + 5000)) && (CD >= 1)) //sets time for alarm to be on
  {
    digitalWrite(Buzz, LOW); // silence the buzzer
    --CD;// decrease counter
    delay(1000); //Pause between tones 
  }
    else if ((millis >= (ts + 5000)) && (CD == 0))
    {
      digitalWrite(Buzz, LOW); // silence the buzzer
      CD = 5; //reset the counter
      return;
    }
    
  }



//}  end of loop
  else if ((millis >= (ts + 5000)) && (CD == 0))

Are you missing something from the call to the millis() function ?

Line 49 is useless as it creates a local variable that is never used.

The code in case S_OVERRIDE: won't work because it expects state to have other values that it can never have - because its value is S_OVERRIDE:

I wonder if this is a situation where IF/ELSE might be more suitable than SWITCH/CASE as it would allow for compound tests such as if (state == S_OVERRIDE || state == XXX || state = YYY) although that can bring its own complications.

...R

  // The keyword "static" makes sure the variable
  // isn't destroyed after each loop
  static int state = S_DOORCLOSED ;

Each loop through setup() where it is located? Really? You declared "state" elsewhere. I don't think this is doing what you expect. It creates a variable "state", local to setup(), that is never used.