[Updated] How can I use a limit switch to turn off the buzzer?

Hi, :grin:

I’m a newbie here. I have a question, how can I use a limit switch to turn off the buzzer?

I would like to make an Arduino project on an alarm clock (a piggy bank alarm clock). The plan is at a certain time, when the buzzer rings, the user has to use the insert the coin (which will touch the limit switch) to turn off the alarm. We would like to use the limit switch only to turn off the buzzer. We’ve connected the limit switch to an Arduino relay on the buzzer side of the relay.

So, theoretically, does the limit switch must be connected in a Normal Open (NO) circuit?

Below is the coding we’ve made for the certain “turn off the alarm” condition. Thank you for your replies and help in advance! :smiley:

#include <EEPROM.h>
#include <Keypad.h>
#include <DS1307.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <LiquidCrystal.h>

DS1307 rtc(SDA, SCL);
Time t;
#define buz 3
LiquidCrystal lcd(A3, A2, A1, A0, 5, 4); 

int Hor, Min, Sec, h, m, s;
int in1, val = 0;
char* tim;
char* dat;
int ASCII = 48;
const char key = 0; 
char buffer[2];

const byte numRows= 4;
const byte numCols= 4;

char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[numRows] = {12, 11, 10, 9};
byte colPins[numCols]= {8, 7, 6, 5};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

/////Setup function/////
void setup() {
Wire.begin();
rtc.begin();
pinMode(in1, INPUT);
pinMode(buz, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
welcome();

rtc.setDOW(SATURDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(10, 54, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(8, 12, 2018); // Day, Month, Year
}

/////Loop function/////
void loop() {
t = rtc.getTime();
Hor = t.hour;
Min = t.min;
Sec = t.sec;
tim = rtc.getTimeStr();
dat = rtc.getDateStr();
char key = myKeypad.getKey();
val = digitalRead(in1);

if (val == HIGH){
digitalWrite(buz, LOW);
delay(500);
}

if(key == '#'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter New Time");
lcd.setCursor(0,10);

int j =0;
int i=0;
while( j<6)
{
if(i==2 || i == 5){
lcd.print(":");
i++;
}
key=myKeypad.getKey();
if(key)
{
lcd.print(key);
EEPROM.write(j,key);
j++;
i++;
}
}
}
changealarm();
checkalarm();
timedate();
}

/////Compare current time with alarm time/////
void checkalarm(){
if( Hor == h && Min == m)
{
delay(200);
lcd.clear();
lcd.setCursor(0,10);
lcd.print("Insert coin to stop Alarm");
digitalWrite(buz, HIGH);
delay(200);
}
}

/////Store alarm time/////
void changealarm(){
buffer[0]=EEPROM.read(0);
buffer[1]=EEPROM.read(1);
h = atoi(buffer);
buffer[0]=EEPROM.read(2);
buffer[1]=EEPROM.read(3);
m = atoi(buffer);
buffer[0]=EEPROM.read(4);
buffer[1]=EEPROM.read(5);
s = atoi(buffer);
}

/////Print current time, current date and alarm time/////
void timedate(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(rtc.getTimeStr());
lcd.setCursor(0,10);
lcd.print("Date: ");
lcd.print(rtc.getDateStr());
lcd.setCursor(0,20);
lcd.print("Alarm: ");
lcd.print(h);
lcd.print(".");
lcd.print(m);
lcd.print(".");
lcd.print(s);
}

/////Welcome display/////
void welcome(){
lcd.setCursor(0,0);
lcd.print(" Welcome ");
lcd.setCursor(0,10);
lcd.print(" To ");
lcd.setCursor(0,20);
lcd.print("TIMe Project ");
delay(5000);
}

Hudhudpanda:
Below is PART OF the coding we've made

You left out the stuff above setup(), it seems.

You might also like to hit control-T before you post, to get some snazzy indents to make it easier to read.

This question could benefit from a circuit diagram I think. Not clear (to me, anyway) how this is all wired. Is the "limit switch" in the buzzer circuit on the buzzer side of the relay or connected to the Arduino some way?

I've inserted the full coding that we made :smiley: