When you hold the lighter gas inside the house, the alarm system is expected to be activated again.
MicroBlocks Codes of the PicoBricks
You can access the Microblocks codes of the project by dragging the image to the Microblocks Run tab or click the button.
MicroPython Codes of the PicoBricks
from machine import Pin, PWM
from utime import sleep
# define libraries
PIR = Pin(14, Pin.IN)
MQ2 = Pin(1, Pin.IN)
buzzer = PWM(Pin(20, Pin.OUT))
redLed = Pin(7, Pin.OUT)
button = Pin(10, Pin.IN, Pin.PULL_DOWN)
# define output and input pins
activated = 0
gas = 0
while True:
if button.value() == 1:
activated = 1
gas = 0
sleep(3)
redLed.value(1)
buzzer.duty_u16(0)
if MQ2.value() == 1:
gas = 1
if activated == 1:
if PIR.value() == 1:
buzzer.duty_u16(6000)
buzzer.freq(440)
sleep(0.2)
buzzer.freq(330)
sleep(0.1)
buzzer.freq(494)
sleep(0.15)
buzzer.freq(523)
sleep(0.3)
if gas == 1:
buzzer.duty_u16(6000)
buzzer.freq(330)
sleep(0.5)
redLed.value(1)
buzzer.freq(523)
sleep(0.5)
redLed.value(0)
# LED will light and buzzer will sound when PIR detects motion or MQ2 detects toxic gas
,
Arduino C Codes of the PicoBricks
void actived (){
digitalWrite(7,1);
while(!(digitalRead(14) == 1))
{
_loop();
}
motion_detected();
}
void motion_detected (){
while(1) {
// buzzer settings
tone(20,262,0.25*1000);
delay(0.25*1000);
tone(20,330,0.25*1000);
delay(0.25*1000);
tone(20,262,0.25*1000);
delay(0.25*1000);
tone(20,349,0.25*1000);
delay(0.25*1000);
// sound the buzzer when PIR detected a motion
_loop();
}
}
void _delay(float seconds) {
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}
void _loop() {
}
void loop() {
_loop();
}
void setup() {
pinMode(10,INPUT);
pinMode(1,INPUT);
pinMode(20,OUTPUT);
pinMode(7,OUTPUT);
pinMode(14,INPUT);
// define input and output pins
while(1) {
if(digitalRead(10) == 1){
_delay(3);
actived();
}
if(digitalRead(1) == 1){
while(!(digitalRead(10) == 1))
{
_loop();
tone(20,349,0.5*1000);
delay(0.5*1000);
digitalWrite(7,1);
_delay(0.5);
tone(20,392,0.5*1000);
delay(0.5*1000);
digitalWrite(7,0);
_delay(0.5);
}
}
_loop();
}
}
GitHub Project Page
Tags: