How to create a loop that you can only get out of using the reset button?

Hey guys,

Im new to arduino and C++ so please be gentle with me :slight_smile:

I am building a fire alarm system using an Arduino Uno and a 16x2 LCD Display.
I have 1 switch connected to pin 8 and a piezobuzzer on pin 7. My LCD display is connected to pins 12, 11, 5, 4, 3, 2. I want to do this:

When the switch on pin 8 is closed I want the system to go into a loop and I want the system to stay in this loop even if the switch is open again. Then, the only way to get out of this loop is by using the reset button on the Arduino.

I thought about using the IF and ELSE statements but when the switch is open again the system goes out of the loop and I want it to stay in the loop until the reset button is pressed.

In the loop I will put something like this:

digitalWrite(7, HIGH);            // Turning on the Piezo Buzzer
lcd.clear();
lcd.print("Alarm Active");       // Printing an alarm message on the LCD

delay(500)                          //  Giving the LCD some time to process the characters.

I've read something about booleans but I don't understand it very well.

I hope you guys have can help me or have any idea for the script!

Thanks!

Hi BramLaurens

I assume you want that code fragment to execute just once when the switch is closed? And then you want the Arduino to do nothing more, just loop forever until it is reset?

Try something like this. Assumes your switch connects pin 8 to GND when you close it.

// in setup()
pinMode(8, INPUT_PULLUP);

// in the part of your program where you want to check the state of the switch
if (digitalRead(8) == LOW)
{
  digitalWrite(7, HIGH);         
  lcd.clear();
  lcd.print("Alarm Active");       
  delay(500);
  while (true)
  {
    // do nothing - loop here until Arduino is reset
  }
}

Regards

Ray

Thanks for the quick reply!
Will try it!

I'd say though, that resetting the Arduino is a bit of a drastic way to turn the alarm off....

Haha, I know! But im going to make a switch between ground and reset to make a reset button!

But I have one more question, I added a switch on pin A2 to silence the alarm (just silence, not reset!)
When the silence switch is closed I want this to loop:

    digitalWrite(9, LOW);          // Turn off the piezo      
    lcd.clear();
    lcd.print("ALARM ACTIVE");      
    lcd.setCursor(0,1); 
    lcd.print("Audible Silence");
    delay(500);

But when the silence switch is open again, I want the system to go back into alarm mode

I tried many things but they all failed.

Would be awesome if this worked!

Thanks!

You need to write down all the logical steps that you want to happen - each one on a different line so that you can see how one might affect the other.

I can't help feeling that your first concept (of going into an endless loop) is quite unsuited to your subsequent requirements.

Have a look at the Thread planning and implementing a program as it shows how different things can be organized within a single program.

The Arduino system has an in-built loop that can only be interrupted by the reset button - it is the function loop(). Make use of it.

...R

I know its quite unsuited but I just can’t think of a way to do this:

  1. when pin 8 is connected to ground the system will enter a loop.
  2. I want this loop to be interruptable in 2 ways:
  3. closing a switch connected to pin A2 and ground, this makes the alarm system go into silence but
    when the switch is opened, the system will go into alarm mode again.
  4. pressing the reset button, this will make the system go back into normal state.

How can I use the loop() function to make this work?

Thanks!

if (digitalRead(Your input) == LOW)
  {lcd.write ("HELP! HELP!");
   while (true);
  }

It might be an idea to look into the concept of a Finite State Machine and try to diagram all the states and transitions.

Forum members Nick Gammon and Grumpy Mike both have excellent explanations of the FSM.

BramLaurens:
How can I use the loop() function to make this work?

There should be plenty of ideas in the link I gave you. It is all about using loop() as an endless activity.

...R

Thank you guys very much! I finally got it working using the loop function and other functions!
The finite-state machine is a very good idea to put in the project too! Im gonna take a look at it and see what I can do with it! The planning and implementing thread helped me very much!

This is the code that worked:

#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sounderTransistor = 7;

void setup()                           //=====================Setting up the system=====================//
{
  lcd.begin(16, 2);
  lcd.clear();
  pinMode(8, INPUT_PULLUP);
  pinMode(9, OUTPUT); 
  pinMode(7, OUTPUT);
  pinMode(A5, INPUT_PULLUP);
  pinMode(A4, INPUT_PULLUP);
  pinMode(A3, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  
  
   //=====================Giving a short beep / visual signal when the system is done setting up=====================//

  delay(1000);
  lcd.clear();
  lcd.print("System Reset");
  delay(1000);
  digitalWrite(9, HIGH);
  delay(100);
  digitalWrite(9, LOW);
  delay(50);
  digitalWrite(9, HIGH);
  delay(100);
  digitalWrite(9, LOW);
  
  
}

void loop()
{
  //==========================================================initializing the switches and inputs=======================================================//
  int smokeSwitch = digitalRead(8);
  int manualSwitch = digitalRead(A5);
  int drillSwitch = digitalRead(A4);
  int resetSwitch = digitalRead(A3);
  int silenceSwitch = digitalRead(A2);
  
  
  
  
  //==========================================================Defining the system's normal state=======================================================//
  lcd.clear();
  lcd.print("System Normal");
  delay(200);
  
  
  
  
  
  
  
  //==========================================================Reading out the switches=======================================================//
  
  
  ///////////////////////////////////////////////////////////Reading the Smoke detectors///////////////////////////////////////////////////
  if(smokeSwitch == LOW)
  {
    smoke();
    while(true)
  {
  
          if(digitalRead(A2) == LOW)
   {
          digitalWrite(9, LOW);
          lcd.clear();
          lcd.print("Alarm Silenced");
          lcd.setCursor(0,1);
          lcd.print("AUDIBLE SILENCE");
          
          beepbeep();
          delay(10000);
          beepbeep();
          delay(10000);
          beepbeep();
    
   }                                    // Belongs to If A2
     
    }
                                       // Belongs to While True
    }                                  // Belongs to If SmokeSwitch
    
    
  ///////////////////////////////////////////////////////////Reading the fire drill switch///////////////////////////////////////////////////
    
    
    
    if(drillSwitch == LOW)
  {
    drill();
    while(true)
  {
  
          if(digitalRead(A2) == LOW)
   {
          digitalWrite(9, LOW);
          lcd.clear();
          lcd.print("Alarm Silenced");
          lcd.setCursor(0,1);
          lcd.print("AUDIBLE SILENCE");
          
          beepbeep();
          delay(10000);
          beepbeep();
          delay(10000);
          beepbeep();
    
   }                                    // Belongs to If A2
     
    }
                                       // Belongs to While True
    }                                  // Belongs to If drillSwitch
  ///////////////////////////////////////////////////////////Reading the manual callpoints///////////////////////////////////////////////////
    
    
    if(manualSwitch == LOW)
  {
    manualcallpoint();
    while(true)
  {
  
          if(digitalRead(A2) == LOW)
   {
          digitalWrite(9, LOW);
          lcd.clear();
          lcd.print("Alarm Silenced");
          lcd.setCursor(0,1);
          lcd.print("AUDIBLE SILENCE");
          
          
          beepbeep();
          delay(10000);
          beepbeep();
          delay(10000);
          beepbeep();
    
   }                                    // Belongs to If A2
     
    }
                                       // Belongs to While True
    }                                  // Belongs to If manualSwitch
    
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
  }                                    // Belongs to Void Loop
  


///////////////////////////////////////////////////////////Making a beep for when the system is silenced///////////////////////////////////////////////////////////////////////////////////

  
void beepbeep()
{
  digitalWrite(9, HIGH);
  delay(100);
  digitalWrite(9, LOW);
  delay(50);
  digitalWrite(9, HIGH);
  delay(100);
  digitalWrite(9, LOW);
  delay(50);
  digitalWrite(9, HIGH);
  delay(100);
  digitalWrite(9, LOW);
}


  ///////////////////////////////////////////////////////////Defining what happens when the smoke detectors are activated///////////////////////////////////////////////////


void smoke(){
  
  digitalWrite(9, HIGH);
  lcd.clear();
  lcd.print("Alarm Active");
  lcd.setCursor(0,1);
  lcd.print("SMOKE DETECTED");
  delay(500);
  
  
}


  ///////////////////////////////////////////////////////////Defining what happens when the fire drill switch is closed///////////////////////////////////////////////////


void drill()
{
  
  digitalWrite(9, HIGH);
  lcd.clear();
  lcd.print("Alarm Active");
  lcd.setCursor(0,1);
  lcd.print("EVACUATION TEST");
  delay(500);
  
}


   ///////////////////////////////////////////////////////////Defining what happens when a Manual Callpoint is activated///////////////////////////////////////////////////


void manualcallpoint()
{
  
  digitalWrite(9, HIGH);
  lcd.clear();
  lcd.print("Alarm Active");
  lcd.setCursor(0,1);
  lcd.print("MCP Active");
  delay(500);
  
}

Thank you guys very much for your help!

There are plenty of "finite states" in the planning and implementing code. I just did not give them a grand name.

...R

Oh, I didn't know that! Thanks for your help! The link really helped me out!

Robin2:
There are plenty of "finite states" in the planning and implementing code. I just did not give them a grand name.

...R

Then, you should. You're right, "Finite State Machine" does a certain cachet does it not?

JimboZA:
You're right, "Finite State Machine" does a certain cachet does it not?

The phrase immediately conjures up "hard" and "only for experts" in my mind. And, unfortunately, the way some experts present it, it is.

In reality it is a simple concept - like a shopping list, make a note of something you need so you don't forget it and then cross it out when you have it so you don't get the same thing twice.

...R