Program issue with pushbutton (Alarm)

HI
If possible can someone direct me in the right direction regarding my code for an alarm on a Mega 2560. I want to make an alarm that turns on when a remote is pushed (closing relay on receiver) and disarm when button is pushed again. I tried the push button examples but having no luck. (most of my code is adapted from examples on the net)

//Libraries
#include <LiquidCrystal.h>
#include "NewTone.h"
/*--------------------------CONSTANTS-------------------------*/
const int  buttonPin = 10;    // the pin that the pushbutton is attached to
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
const int buzzer = 29;      //Buzzer/small speaker
const int greenLED = 30;
const int redLED = 31;
const int pirBraai = 10; //PIR Lounge
const int pirLounge = 11; //PIR Lounge
const int doorMagSen = 12;    //Back Door magnetic sensor
const int garageMagSen = 24; //PIR BRaai
//const int sirenPin = 35;
const int relay1 = 23; // Outside Light
const int relay2 = 22; //Siren Relay
//LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //lcd ((RS, E, D4, D5, D6, D7)
const int rs = 8, en = 9, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
/*--------------------------VARIABLES------------------------*/
boolean armed = false;  //Variable for system state (armed:true / unarmed:false)

/**********************************************************************************/

void setup() {
  lcd.begin(16, 2);                 //Setup the LCD's number of columns and rows
  pinMode(doorMagSen, INPUT);       //Set all magnetic sensors as input with internal pullup resistor
  pinMode(pirBraai, INPUT);
  pinMode(pirLounge, INPUT);
  pinMode(garageMagSen, INPUT);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
}

void loop() { //Main loop
  buttonState = digitalRead(buttonPin); //Run function to activate the system
  if (buttonState == LOW) {
    NewTone(buzzer, 500, 200);
    systemIsArmed();
  }
  buttonState = digitalRead(buttonPin); //Run fuction to de activate the system
  if (buttonState == HIGH)  {
    NewTone(buzzer, 500, 200);
    systemIsUnarmed();

  }
}

/********************************FUNCTIONS************************************/

//While system is unarmed
void systemIsUnarmed() {
  int screenMsg = 0;
  lcd.clear();                  //Clear lcd
  unsigned long previousMillis = 0;           //To make a delay by using millis() function
  const long interval = 5000;           //delay will be 5 sec.
  //every "page"-msg of lcd will change every 5 sec
  while (!armed) {                  //While system is unarmed do...
    unsigned long currentMillis = millis();   //Store the current run-time of the system (millis function)
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      if (screenMsg == 0) {           //First page-message of lcd
        lcd.setCursor(0, 0);
        lcd.print("SYSTEM ALARM OFF");
        lcd.setCursor(0, 1);
        lcd.print("----------------");
        screenMsg = 1;
      }
      else {                  //Second page-message of lcd
        lcd.setCursor(0, 0);
        lcd.print("PRESS TO ARM ALARM  ");
        lcd.setCursor(0, 1);
        digitalWrite(redLED, LOW);
        digitalWrite(greenLED, HIGH);
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        screenMsg = 0;
      }
    }
  }
}

//While system is armed
void systemIsArmed() {
retry: //label for goto
  lcd.clear();
  int count = 25;               //Count 20sec before activate the system
  unsigned long previousMillis = 0;
  const long interval = 1000;
  while (!armed) {
    //While system is unarmed - for 10sed do...
    lcd.setCursor(0, 0);
    lcd.print(" SYSTEM WILL BE ");      //Print message to lcd with 10 sec timer
    lcd.setCursor(0, 1);
    lcd.print("   ARMED IN ");
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      //Screen counter 10sec
      if (count > 1) {
        count--;            //Countdown timer
      }
      else {
        armed = true;           //Activate the system!
        break;
      }
    }
    lcd.setCursor(12, 1);
    lcd.print(count);           //show the timer at lcd second line 13 possition
    //For screen counter - 20sec
    if (count >= 10) {
      lcd.setCursor(12, 1);
      lcd.print(count);       //print countdown timer at lcd
    }
    else {                //catch '0'bellow 10 (eg 09)
      lcd.setCursor(12, 1);
      lcd.print(" ");
      lcd.print(count);
    }
  }
  while (armed) {               //While system is armed do...
    lcd.setCursor(0, 0);
    lcd.print("SYSTEM IS ARMED!");
    lcd.setCursor(0, 1);
    lcd.print("----------------");
    int door = digitalRead(doorMagSen);   //Back door sensor
    int garage = digitalRead(garageMagSen);
    int pirBraai = digitalRead(pirBraai);
    int pirLounge = digitalRead(pirLounge);
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);

  }
  //Check values
  if (pirBraai == LOW) {
    alarmFunction(); //Call alarm!
  }
  if (garageMagSen == LOW) {
    alarmFunction(); //Call alarm!
  }
  //Pir Lounge
  if (pirLounge == LOW) { //Disarm the system with correct password
    alarmFunction(); //Call alarm!
  }
  //Back Door
  if (doorMagSen == LOW) { //call alarm!
    alarmFunction(); //Call alarm!
  }
}

//Alarm
void alarmFunction() {
  digitalWrite(relay2 , HIGH);
  unsigned long previousMillis = 0;
  const long interval = 500;
  boolean buzzerState = false;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis; //Play a beep NewTone every 0.5 second
    if (!buzzerState) {
      NewTone(buzzer, 700);
      buzzerState = true;
    }
    else {
      noNewTone(buzzer);
      buzzerState = false;
    }
  }
  lcd.setCursor(0, 0);
  lcd.print(" ALARM TRIGGERED ");
  lcd.setCursor(0, 1);
  lcd.print("  PRESS REMOTE  ");
}

but having no luck.

Luck does not come into it

What happens when you run the code ?

retry: //label for goto

Okay. Seriously, just throw the entire sketch in the garbage and start again. It will be easier than troubleshooting "spaghetti logic". The code also has some really weird functions. Go back to the button examples that you mentioned. We can help with that.

Q1 : how are the inputs wired ?

aarg:

retry: //label for goto

Okay. Seriously, just throw the entire sketch in the garbage and start again. It will be easier than troubleshooting "spaghetti logic".

It is a trick - there is no goto in the sketch !

UKHeliBob:
It is a trick - there is no goto in the sketch !

That just increases my concern level. :slight_smile:

const int garageMagSen = 24;
....
if (garageMagSen == LOW)

Doesn't seem very likely, does it?

Start from scratch.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.