How can I stop this alarm with a button?

Hi guys,

I'm novice on arduinos. I'm building an alarm circuit with a relay works with Arduino Uno R3. I'm using this code that I attached on this message. But I don't know what I should write additional codes to this file for stopping alarm with an button on number 9 pin? Help me please.

sketch_oct08c.ino.ino (2.4 KB)

Your code is small enough to post in the message.
Type

[code]
Paste your code after that
Type [/code]

after that

The result will look like

your code here

I can not view your code as I'm using a cellphone at the moment

sterretje:
Your code is small enough to post in the message.
Type

[code]

Paste your code after that
Type [/code]


after that

The result will look like


your code here




I can not view your code as I'm using a cellphone at the moment
#include <LiquidCrystal.h>
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
int starttime;
int activetime;
int prevoustime = 0;
 
int hours = 0;
int mins = 0;
 
int ahours = 0;
int amins = 0;

int relay = 2;
 
void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
  
  Serial.begin(9600);
  
  pinMode(13, INPUT);
  digitalWrite(13, HIGH);
  pinMode(9, INPUT);
  digitalWrite(9, HIGH);
  pinMode(10, INPUT);
  digitalWrite(10, HIGH);
  
   pinMode(8, INPUT);
  digitalWrite(8, HIGH);
  pinMode(A0, OUTPUT);
  digitalWrite(A0, HIGH);
  
   pinMode(7, OUTPUT); 

   pinMode(relay, OUTPUT); 
  
  starttime = millis()/1000;
}
 
void loop()
{
  while(digitalRead(8) == LOW)
  {
    lcd.setCursor(6,1);
    lcd.print("Uyarici");
    lcd.setCursor(6,0);
       if(digitalRead(9) == LOW)
    {
     amins++;
    } 
    else if (digitalRead(10) == LOW)
    {
      ahours++;
    }
     lcd.setCursor(6,0);
  
  if(ahours < 10)
  {
    lcd.print("0");
    lcd.print(ahours);
  }
  else
  {
    lcd.print(ahours);
  }
    
    lcd.print(":");
    
  if (amins < 10)
  {
    lcd.print("0");
      lcd.print(amins);
  }
  else
  {
      lcd.print(amins);
  }
 if(amins > 59)
     {
      ahours++;
      amins = 0;
     } 
     if(ahours > 23)
     {
      ahours = 0; 
     }
     delay(500);
     lcd.clear();
  }
  
  if(digitalRead(13) == LOW)
  {
    
    lcd.setCursor(5,1);
    lcd.print("Zaman Ayari");
    lcd.setCursor(6,0);
     if(digitalRead(9) == LOW)
    {
     mins++;
    } 
    else if (digitalRead(10) == LOW)
    {
      hours++;
    }
    
 
  }
 
      activetime = (millis() / 1000) - starttime;
      if(prevoustime < (activetime - 59))
      {
       mins++;
       prevoustime = activetime;
      } 
      
      if(mins > 59)
     {
      hours++;
      mins = 0;
     } 
     
     if(hours > 23)
     {
      hours = 0; 
     }
  
  
  lcd.setCursor(6,0);
  
  if(hours < 10)
  {
    lcd.print("0");
    lcd.print(hours);
  }
  else
  {
    lcd.print(hours);
  }
    
    lcd.print(":");
    
  if (mins < 10)
  {
    lcd.print("0");
      lcd.print(mins);
  }
  else
  {
      lcd.print(mins);
  }
 
 
 
if(ahours == hours && amins == mins && amins != 0)
{
  digitalWrite(relay, HIGH);  
}

 
  Serial.println(mins);
  Serial.println(hours);
  Serial.println("");
  Serial.println(amins);
  Serial.println(ahours);
  Serial.println("");
  Serial.println(activetime);
  Serial.println(prevoustime);
  Serial.println(starttime);
  Serial.println("");
}

I'm a newbie too :slight_smile:

Trying with below code.

if(ahours == hours && amins == mins && amins != 0)
{
  if (digitalRead(9) == LOW)    //it means the alarm relay can only be HIGH if the button 9 is not press (LOW state), if button 9 is being pressing the alarm relay can not be HIGH, so that disable the alarm
    {
      digitalWrite(relay, HIGH);  
    }
}

Topic upped!

orkunkurun:
Topic upped!

Topic downed, as you added NO new information.

Changing the value of ahours and/or amins when the switch IS pressed is a crappy idea. Changing the value when the switch BECOMES pressed is a much better idea. The state change detection example shows how to do that.

You appear to already have a switch on pin 9 that has nothing to do with stopping an alarm. You have NO comments in the code that explain that the hell is connected to what.

You seem to have some foolish idea that dividing an unsigned long by 1000 will result in a value that will always fit in an int. Dream on.

I don't see anything in the code that indicates that there is an alarm, not even some comments. So, stopping the non-existent alarm doesn't seem necessary.

PS: Your code looks like it was typed by a drunken monkey. Use Tools + Auto Format, and get that monkey into rehab.