How to turn off alarm using Push Button?

I am trying to make an alarm using 2 push button and a photoresistor, I am able to snooze the alarm for 5 seconds with one pushbutton, But have dificulities turning the Piezo off when button 2 is pressed. Please Help!
My Code:
%%Push Button
clear; clc;
a= arduino();
previousPress = 0;
offPress = readDigitalPin(a,'D13'); %turn off completely
% count=0;
while true
thisPress = readDigitalPin(a,'D12'); %snooze
v = readVoltage(a,'A1');%photoresistor
disp(v);
if v >= 2.8
playTone(a,'D6',99,10);%buzzer
end
if thisPress==0 && previousPress==1
playTone(a,'D6',99,0);%buzzer
pause(5)%snooze 5sec
end
offPress = readDigitalPin(a,'D13'); %turn off completely
if offPress==0 && previousPress==1
end
playTone(a,'D6',99,0);%buzzer off
previousPress = thisPress;
% count = count + 1;
end

Réponses (1)

Adam Danz
Adam Danz le 27 Avr 2020
Modifié(e) : Adam Danz le 27 Avr 2020
I'll take a shot at it... I may not have interpretted what you're doing correctly but at least this should give you an idea of how to solve this.
If you want the while loop to end when the alarm is turned off,
offPress = readDigitalPin(a,'D13'); %turn off completely ( I ASSUME THIS EQUALS 0)
while ~offPress
...
offPress = readDigitalPin(a,'D13'); %turn off completely ( DETECT IF BUTTON PRESSED
end
If you want the while loop to continue running,
previousOffPress = 0; % SET INITIAL OFFPRESS STATE
while true
...
if v >= 2.8 && ~offPress %(ASSUMING offPress==0 means not pressed)
...
end
if thisPress==0 && previousPress==1 && ~offPress % (SAME ASSUMPTION)
...
end
offPress = readDigitalPin(a,'D13');
if offPress && ~previousOffPress % DETECT IF TURNED OFF
playTone(a,'D6',99,0);%buzzer off
previousOffPress = offPress;
end
end
If you have any questions or problems, please explain in more detail what buttons you have, what they are supposed to do, and your updated code.

6 commentaires

ayush Patel
ayush Patel le 27 Avr 2020
Modifié(e) : Adam Danz le 27 Avr 2020
Button 1(D12) snoozes the buzzer for 5 seconds, Button 2 (D13) turns off the alarm completly, Photoresistor (A1), and piezo/ buzzer (PWM pin6) should start when the photoresistor detects 2.8V.
My code:
%%Push Button
clear; clc;
a= arduino();
previousPress=0;
offPress = readDigitalPin(a,'D13')
while true
thisPress = readDigitalPin(a,'D12'); %snooze
v = readVoltage(a,'A1');%photoresistor
disp(v);
if v >= 2.8
playTone(a,'D6',99,10);%buzzer
end
if thisPress==0 && previousPress==1
playTone(a,'D6',99,0);%buzzer
pause(5)%snooze 5sec
end
previousPress = thisPress;
% offPress = readDigitalPin(a,'D13'); %turn off completely
if offPress==0 && thisPress==1
end
playTone(a,'D6',99,0);%buzzer off
end
Adam Danz
Adam Danz le 27 Avr 2020
When the alarm is turned off, should the while-loop end or should it continue but without playing any sound?
Another way to ask that is, when the alarm it turned off, should the system continue to sample the photoresistor and piezo etc?
ayush Patel
ayush Patel le 27 Avr 2020
When the alarm is turned off, the off button should exit the loop.
Here's most of the solution. You can fill in the rest and if there are any problems, show me the updated code and describe what's going on.
a= arduino();
offPress = 0;
while ~offPress
% Snooze control
snoozeTF = readDigitalPin(a,'D12'); % snoozeTF==1 means snooze-on
if snoozeTF
% Snooze-on pressed
pause(5)
snoozeTF = ~snoozeTF; %turn snooze off
end
% Turn off alarm
offPress = readDigitalPin(a,'D13'); % offPress==1 means turns-off
if offPress
% skip rest of loop and leave loop
continue
end
% If this section is reached, that means snooze is off
% and the stop-button has not been pressed.
% THE REST OF YOUR CODE GOES HERE
end
ayush Patel
ayush Patel le 27 Avr 2020
Modifié(e) : ayush Patel le 27 Avr 2020
I tried using this concept on my previous code,I have two variables (one for previousPress, and other for previousoffPress) and my off button is outside the loop. It's still not working,What am I doing wrong? Also what does (~) mean?
a= arduino();
previousPress=0;
previousoffPress=0;
while true
thisPress = readDigitalPin(a,'D12'); %snooze
v = readVoltage(a,'A1');%photoresistor
disp(v);
if v >= 2.8
playTone(a,'D6',99,10);%buzzer
end
if thisPress==0 && previousPress==1
playTone(a,'D6',99,0);%buzzer
pause(5)%snooze 5sec
end
offPress = readDigitalPin(a,'D13'); %turn off completely
if offPress==0 && previousoffPress==1
end
playTone(a,'D6',99,0);%buzzer off
previousPress = thisPress;
previouoffPress= offPress;
end
Adam Danz
Adam Danz le 27 Avr 2020
Modifié(e) : Adam Danz le 29 Avr 2020
It's impossible to know what your code is doing without knowing what the variables represent.
For example, when thisPress==1 does that mean snooze is on or does it mean it's off? When you press the snooze button, is it a state switch that stays in the on/off position or does it merely send a impulse signal?
Also, "It's not working" could have dozens of meaings. Make the problem a lot easier for other people to solve by focusing in on the problem.
Your updated code doesn't look like the code from my previous comment so I'm wondering what you tried and what, exactly, didn't work.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Centre d'aide et File Exchange

Modifié(e) :

le 29 Avr 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by