Alarm programming guidance

Hello

I am starting an Alarm project which at the moment has one door and one reset switch. I have completed
most of the individual blocks of code, sound alarm, check door, reset alarm etc. Now I want to bolt this
all together and was wondering how best to approach this. I have attached a flow diagram of the circuit and
a brief sketch of how I see this working.

void loop()

{

on_off();

}


void on_off()

{

	switch (armed_button_pressed) {


	case yes;

	
	pre_check()

     

	case no;

}


void pre_check()

{

switch (check_door_open) {

		case yes;	

			case no;

				armed()	
}

Void armed()

{

switch (check_door_open) {

		case yes;

			alarm()
				

			case no;

				
}

Void alarm()

{

switch (key_entered) {

		case yes;

			on_off()
				

			case no;

		
}

Regards
Geoff

alarm.jpg

I'm guessing that sketch doesn't compile.

Rather that a flowchart I would try a state diagram. Have a state variable that keeps track of the current state and a set of inputs that can change the state.

enum states = {STATE_IDLE, STATE_ARMING, STATE_ARMED, STATE_ALARMING} state;

void setup() {state=STATE_IDLE;}

void loop()
    {
    switch (state)
        {
    case STATE_IDLE:
        if (digitalRead(ARMING_BUTTON)) state = STATE_ARMING;
        break;

    case STATE_ARMING:
        if (digitalRead(DOOR_OPEN_SENSOR)))
              {
              //  Display the "Please close the door" message?
              }
       if (digitalRead(RESET_BUTTON)) state = STATE_IDLE;
       break;

    case STATE_ARMED:
        if (digitalRead(DOOR_OPEN_SENSOR)))
              {
              digitalWrite(ALARM_PIN, HIGH);
              state = STATE_ALARMING;
              }
        if (digitalRead(RESET_BUTTON)) state = STATE_IDLE;
        break;

    case STATE_ALARMING:
         if (digitalRead(RESET_BUTTON))
              {
              digitalWrite(ALARM_PIN, LOW);
              state = STATE_IDLE;
              }
        break;
        }
    }

Hi John

Thanks for the heads up, this gives me a good start.

Cheers

Geoff

I forgot to transition to the ARMED state if the door was closed:

        if (digitalRead(DOOR_OPEN_SENSOR)))
              {
              //  Display the "Please close the door" message?
              }
        else
            state = STATE_ARMED;

You see how it works. In each state there is a limited set of inputs that will cause action and that action will usually include changing to a different state. For example, if you wanted to have an exit delay (time between pressing the ARM button and arming) you would store the current time and create a new state where you do nothing until the time has expired (or the reset button is pressed).

I'm with you John, I'm sure I'll have more questions once I start to merge all the different states I have. You have shown me the way.

Thanks

Geoff