how to control door using SIM900a - arduino

I need help I'm doing project about opening and closing the door using SIM900a.. it says that no errors but it doesn't work !! I used AT commands and it supposes to send notification to the user using sms when theres knocking, motion at home, high temperature and when theirs smoke please help please please
#include <SoftwareSerial.h>
#include "pins_arduino.h"
#include <String.h>
SoftwareSerial SIM900(7, 8); //tx & rx pins
String msg = String("");
char inchar;
int x = 0;
int y = 0;
float z =0;
int PIR_sensor = 12;
int door_lock = 4 ; //close the door
int door_lock1 = 5 ;
int led = 11;
int led1 = 10; //red led
int mic= 6;
int Gas_sensor = A0;
float Tem_sensor = A1;
float temp =0.0;
String textForGAS ;
String textForPIR ;
String textForTAM ;
void setup()
{
Serial.begin(19200);
pinMode(PIR_sensor, INPUT);
pinMode(door_lock, OUTPUT);
pinMode(door_lock1, OUTPUT);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
digitalWrite(door_lock, LOW);
digitalWrite(door_lock1, LOW);
digitalWrite(led, LOW);
digitalWrite(led1, LOW);
// wake up the GSM shield
SIM900.begin(19200);
delay(20000); // give time to log on to network.
SIM900.print("AT+CMGF=1r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0r");
// blurt out contents of new SMS upon receipt to the GSM shield’s serial out
delay(100);
Serial.println("Ready…");
}
void sendSMS(String message)
{
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"+96896089681\"");
delay(100);
SIM900.println(message);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
}
void loop()
{
//If a character comes in from the cellular module…
if (SIM900.available())
{
inchar = SIM900.read();
Serial.println(inchar);
if (inchar == '#')
{
delay(10);
inchar = SIM900.read();
if (inchar == 'a')
{
delay(10);
inchar = SIM900.read();
if (inchar == '0')
{
digitalWrite(door_lock, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led, LOW);
}
else if (inchar == '1')
{
digitalWrite(door_lock1, HIGH);
digitalWrite(led, HIGH);
digitalWrite(led1, LOW);
}
delay(10);
inchar = SIM900.read();
if (inchar == 'b')
{
inchar = SIM900.read();
if (inchar == '0')
{
digitalWrite(PIR_sensor, LOW);
}
else if (inchar == '1')
{
digitalWrite(PIR_sensor, HIGH);
}
}
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
Gas_sensor = (analogRead(A0));
PIR_sensor = (digitalRead(12));
Tem_sensor = (analogRead(A1));
temp = Tem_sensor * 0.48828125;
//program for GAS sensor
if (temp > 70.0)
{
textForTAM = "Alarm ! The degree of Tamperture is :n ";
textForTAM.concat(temp);
textForTAM = textForTAM +"C";
Serial.println(textForTAM);
sendSMS(textForGAS);
delay(1000);
do {
z = (analogRead(A0));
}
while (z >= 141.312);
}
//program for GAS sensor
if (Gas_sensor > 500)
{
textForGAS = "Alarm ! there is Gas by rate :n ";
textForGAS.concat(Gas_sensor);
Serial.println(textForGAS);
sendSMS(textForGAS);
delay(1000);
}
//program for PIR sensor
if (digitalRead(12) == HIGH)
{
textForPIR = "Warnning ! n theres's a motion ";
textForPIR.concat(PIR_sensor);
Serial.println(textForPIR);
sendSMS(textForPIR);
delay(1000);
}
}
thank you

Related

Switching on the diode for some time, as well as execution of the next program in Arduino

The circuit made in tinkercad
I use the relay, because I have only 4 wires to led diode and this two switches.
int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) == HIGH and digitalRead(s2) == LOW)
{
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis();
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(s2) == HIGH and digitalRead(s1) == LOW)
{
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis();
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(k1) == HIGH && (millis() - startTime1 >= led1Duration))
{
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k2) == HIGH && (millis() - startTime2 >= led2Duration))
{
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k1) == HIGH and digitalRead(k2) == LOW)
{
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == LOW)
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == HIGH)
{
digitalWrite(led, HIGH);
}
}
Is there easier way to do that, cause when I assembled it the switches work very slow that blue led turn on after some time. Next I use the esp2866 but the relay for 3V doesn't turn on. How can I make it to work?
#Peter is true : avoid blocking delay code.
here is an example : I have a little simplified your code and added else if to make it more readable.
update1 :
int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
unsigned long t200 = 200;
unsigned long t500 = 500;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) && !digitalRead(s2)) {
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis();
blinkLed200();
} else if (!digitalRead(s1) && digitalRead(s2)) {
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis();
blinkLed500();
}
if (digitalRead(k1) && (millis() - startTime1 >= led1Duration)) {
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
if (digitalRead(k2) && (millis() - startTime2 >= led2Duration)) {
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
}
void blinkLed200() {
if (millis() - t200 > 200) { //tick every 200mS
t200 = millis();
digitalWrite(led, !digitalRead(led));
}
}
void blinkLed500() {
if (millis() - t500 > 500) { //tick every 500mS
t500 = millis();
digitalWrite(led, !digitalRead(led));
}
}
The 3V relays have a very low resistance and I am not sure that the digital outputs are powerful enough. Personally I always switch relays via a transistor.

Arduino Anti-Theft System

I have an arduino anti-theft sistem.
The problem is the function void disarm()
//define Variables
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <RTClib.h>
//LCD
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
RTC_DS1307 rtc;
// Definire PINI
const int ledPin4 = 4;
const int ledPin5 = 5;
int lastButtonstate = LOW;
int lastState;
int button = 3;
// PIR
int pir = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
//Senzor dist
#define trigerPin 9
#define echoPin 10
long duration, distance;
// Arm/Disarm
bool isArmed = false;
//tastatura si parola
char password[5] ="1234"; //create a password
int pozisyon = 0; //keypad position
const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;
char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins [rows] = {14, 15, 16, 17}; //pins of the keypad
byte colPins [cols] = {18, 19, 20, 21};
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
void setup() {
Serial.begin(9600);
rtc.begin();
pinMode(trigerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pir, INPUT);
//Setup for LEDs
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// Pin Mode Buton
lastState = digitalRead(button);
//Setup for LCD
lcd.init();
lcd.backlight();
lcd.setCursor(5,0);
lcd.print(" Sistem ");
lcd.setCursor(2,1);
lcd.print(" anti-efractie ");
delay(1000);
lcd.clear();
}
void loop() {
int currState = digitalRead(button);
if(currState != lastState && currState == HIGH) {
if(!isArmed) {
arm();
}
else if(isArmed) {
disarm();
}
}
lastState = currState;
pirsenzor();
distsenzor();
delay(100);
}
//=================================FUNCTII & SENZORI=================================
void arm() {
if(!isArmed) {
// Armare si cerere parola
lcd.clear();
lcd.print("Armare sistem");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Introduceti codul:");
delay(50);
int attempts = 0;
do {
char whichKey = myKeypad.getKey();
if (whichKey != NO_KEY) {
lcd.setCursor(pozisyon, 1);
lcd.print("*");
password[pozisyon] = whichKey;
pozisyon++;
}
if (pozisyon == 4) {
// Verificare parola corecta
if(strcmp(password, "1234") == 0) {
digitalWrite(ledPin5, HIGH);
delay(300);
digitalWrite(ledPin5, LOW);
delay(300);
digitalWrite(ledPin5, HIGH);
delay(300);
digitalWrite(ledPin5, LOW);
delay(300);
digitalWrite(ledPin5, HIGH);
delay(300);
digitalWrite(ledPin5, LOW);
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem armat");
delay(3000);
lcd.clear();
isArmed = true;
break;
}
else {
// Verificare parola incorecta
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Parola Incorecta!");
delay(1000);
lcd.clear();
pozisyon = 0;
attempts++;
if(attempts == 1)
{
lcd.clear();
break;
}
continue;
}
}
} while (!isArmed);
}
}
void disarm() {
if(isArmed) {
// Dezarmare si cerere parola
lcd.clear();
lcd.print("Dezarmare sistem");
Serial.println("Sunt aici");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Introduceti codul:");
delay(50);
int attempts = 0;
do {
char whichKey = myKeypad.getKey();
if (whichKey != NO_KEY) {
lcd.setCursor(pozisyon, 1);
lcd.print("*");
password[pozisyon] = whichKey;
pozisyon++;
}
if (pozisyon == 4) {
// Verificare parola corecta
if(strcmp(password, "1234") == 0) {
Serial.println("Am trecut de asta");
digitalWrite(ledPin4, HIGH);
delay(300);
digitalWrite(ledPin4, LOW);
delay(300);
digitalWrite(ledPin4, HIGH);
delay(300);
digitalWrite(ledPin4, LOW);
delay(300);
digitalWrite(ledPin4, HIGH);
delay(300);
digitalWrite(ledPin4, LOW);
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem dezarmat");
Serial.println("Am facut asta");
delay(3000);
isArmed = false;
lcd.clear();
break;
}
else {
// Verificare parola incorecta
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Parola Incorecta!");
delay(1000);
lcd.clear();
pozisyon = 0;
attempts++;
if(attempts == 3)
{
lcd.clear();
break;
}
}
}
} while (isArmed);
}
}
// Senzor PIR
void pirsenzor() {
distsenzor();
val = digitalRead(pir); // read input value
if (val == HIGH && !isArmed && distance <= 250) {
digitalWrite(ledPin4, HIGH);
delay(300);
digitalWrite(ledPin4, LOW);
delay(300);
digitalWrite(ledPin4, HIGH);
delay(300);
digitalWrite(ledPin4, LOW); // turn LED ON
if (pirState == LOW) {
lcd.setCursor(0, 0);
lcd.print("Motion detected at");
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" m ");
pirState = HIGH;
}
} else {
digitalWrite(ledPin4, LOW); // turn LED OFF
if (pirState == HIGH) {
lcd.clear();
pirState = LOW;
}
}
}
//Senzor distanta
void distsenzor() {
digitalWrite(trigerPin, LOW); // ensure trigger is low
delayMicroseconds(2);
digitalWrite(trigerPin, HIGH); // send a 10us high to trigger
delayMicroseconds(10);
digitalWrite(trigerPin, LOW);
duration = pulseIn(echoPin, HIGH); //Measure the duration of the echo pulse
distance = (duration / 2) / 29.1; //Calculate the distance using the speed of sound
if (distance >= 400 || distance <= 2) { //Check if the distance is out of range
}
}
Everything is good I push the button to arm the system, the system ask to introduce the password everything is ok but when disarm function is called the function bypass password verification step.
A mention I have a pushbutton with a double function (arm/disarm system).
Sorry for my bad english :).
a suggestion or a solution please.
When you arm the system with
isArmed = true;
you should also reset
pozisyon = 0;
because pozisyon == 4 is used as an indicator that a password has been entered

Serial Print once only

I am a ON/OFF program in which if I turn On it will serial print to serial monitor 1 "ON" only but still looping and turn On the outputs and if turn Off it will serial print 1 "OFF" only but still looping and turn Off the outputs .
Here's my code:
int pbuttonPin = 7;// push button
int fan = 8;
int water = 9;
int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status
void setup() {
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(fan, OUTPUT);
pinMode(water, OUTPUT);
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
}
void loop() {
val = digitalRead(pbuttonPin);// read the push button value
if(val == HIGH && lightON == LOW){
pushed = 1-pushed;
delay(100);
}
lightON = val;
if(pushed == LOW){
Serial.print("ON\n");
Serial.println();
digitalWrite(fan, LOW);
digitalWrite(water, LOW);
delay(100);
}
else if(pushed == HIGH) {
Serial.print("OFF\n");
Serial.println();
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
delay(100);
}
}
I guess you want something like this but i am not sure
bool buttonState = false, buttonStateBefore = false;
buttonState = !digitalRead(buttonPin); //needs to be inverted because INPUT_PULLUP
if(buttonState > buttonStateBefore) doStuff(); //Serial print and turn on/off ligths
buttonStateBefore = buttonState;
Now doStuff() is only called once when the button is pressed.
Maybe this works for you
Add this at last of the code Sting previousval = val at the end of the code and put a if statement which checks if the previous value has changed or not. If it is, then run the code and put this part which contains the print statement in it . basically this will be your code
if ( previousval =! val){
if(pushed == LOW){
Serial.print("ON\n");
Serial.println();
digitalWrite(fan, LOW);
digitalWrite(water, LOW);
delay(100);
}
else if(pushed == HIGH) {
Serial.print("OFF\n");
Serial.println();
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
delay(100);
previousval = val
}
}

How to use a button in a while loop in Arduino?

So i tried to make a little quiz, it can use a lot of improvement but i would like to have a finished project before i better it. I want the user to choose the answer by pressing a button.
My code currently looks like this.
char serialData;
int i = 1;
int redLed = 12;
int greenLed=13;
int buttonA=11;
int buttonB=10;
int buttonC=9;
int lastState = LOW;
int currentState;
void setup()
{
Serial.begin(9600);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buttonA,INPUT);
pinMode(buttonB,INPUT);
pinMode(buttonC,INPUT);
// read the state of the switch/button:
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
// save the the last state
lastState = currentState;
delay(100);
}
void loop()
{
vraag1();
delay(1000);
vraag2();
delay(1000);
vraag3();
delay(1000);
vraag4();
delay(1000);
vraag5();
delay(1000);
vraag6();
}
void vraag1()
{ //1
Serial.println(" 3+3=? ");
delay(400);
Serial.println(" A) 6");
delay(200);
Serial.println(" B) 5 ");
delay(200);
Serial.println(" C) 4 ");
delay(200);
while (Serial.available() ==0){} //2-2
/*
This is the part I added to resolve the issue - but it
does not work.
*/
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
lastState = currentState;
if (Serial.available())
{ //3
serialData = Serial.read();
if (serialData == '6' || serialData == 'a' || serialData == 'A')
{ //4
Serial.println(" Correct ");
i++;
digitalWrite(greenLed, HIGH);
} //4
else
{ //5
Serial.println(" Incorrect ");
digitalWrite(redLed, HIGH);
} //5
}
delay(1000);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
}
This is the code I added to try it but it doesn't work at all. Does anyone have another suggestion?
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
lastState = currentState;

How to write a state change (edge detection) using analogRead() on Arduino?

I'm having trouble creating a toggle switch using an analog input connected to an RF receiver. I'm attempting to make a servo turn 180 degrees when the voltage reads higher than 800 and stay at 180 degrees until the button is pressed again. It only reads higher than 800 when I press a button on my key fob (RF transmitter). In my code it does this, but it doesn't hold at 180 degrees and wait until the button is pressed again to go back to 0 degrees.
#include <Servo.h>
Servo myservo;
const int analogInPin = A0;
int led = 13;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
myservo.attach(6);
myservo.write(0);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue > 800) {
digitalWrite(led, HIGH);
myservo.write(180);
delay(100);
}
else{
digitalWrite(led, LOW);
myservo.write(0);
delay(100);
}
}
EDIT: I'm pretty close with this edited code. I added a variable and an if statement. It turns the LED on and it stays on, but when pressed again it doesn't turn off. So close...
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(500);
ledState == 1;
}
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(500);
ledState == 0;
}
}
OK. I think I solved it! Take a look. It now turns the LED on when the button is pressed and stays on until the button is pressed again. This is how to create a switch using an analog input reading.
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(100);
ledState = 1;
Serial.println(ledState);
delay(500);
}
else {
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(100);
ledState = 0;
Serial.println(ledState);
delay(500);
}
}
}

Resources