Arduino Anti-Theft System - arduino

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

Related

AttachIntrerrupt stops NRF24L01+ from working - ARDUINO

If I remove attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); The code works. But once its added, the radio.available doesnt let anything under it run.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct InputData // define stuct
{
int x;
int y;
};
InputData data;
// Motor A connections
int motor_enA = 9;
int motor_in1 = 10;
int motor_in2 = 6;
int encoder1 = 2;
int encoder2 = 3;
int counter = 0;
int angle = 0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
// Set all the motor control pins to outputs
pinMode(motor_enA, OUTPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
analogWrite(motor_enA, 255);
pinMode (encoder1, INPUT);
pinMode (encoder2, INPUT);
attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
}
void loop() {
readEncoder();
if (radio.available()) {
radio.read(&data, sizeof(data));
// Serial.println(data.y);
if (data.y > 5) {
digitalWrite(motor_in1, HIGH);
digitalWrite(motor_in2, LOW);
}
else if (data.y < -5) {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, HIGH);
}
else {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
}
}
if(counter>1){
counter=0;
angle+=2;
}else if(counter<-1){
counter=0;
angle-=2;
}
Serial.print("Position: ");
Serial.println(angle);
}
void readEncoder()
{
if(digitalRead(encoder1)==HIGH){
int b = digitalRead(encoder2);
if(b>0){
counter++;
}
else{
counter--;
}
}
}
I have tried removing and adding the line, as described above^^
as mentioned by Hcheung, make counter volatile and remove readEncoder(); from loop.
I simplify a bit ISR readEncoder();
volatile int counter = 0;
[....]
void readEncoder() {
//if(digitalRead(encoder1)==HIGH){ //we are precisely here because digitalRead(encoder1) = HIGH !
if(digitalRead(encoder2)) counter++;
else counter--;
}

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;

Input from serial monitor and push button

So I am doing a project. My task is to create a traffic light system that contains three modes that I can select from, by inputting the numbers 1,2 or 3 to the serial monitor. Everything was alright until I decided to add three push-buttons to the breadboard so I am also able to select any mode via the buttons. So far I have been unable to make the Arduino accept input from the serial monitor and the push buttons at the same time, I don't know if i'm in the right path or not to achieving my objective. I just need a small guidance to this, pls. Here is my current code:
//----------------------- Variables
#define ECHOPIN 3
#define TRIGPIN 2
char inVal;
String inString = "";
const int red_led = 11;
const int yellow_led = 12;
const int green_led = 13;
const int on_delay= 2000, off_delay= 1000; //led delays
const int min_distance = 10; // Distance sensor min distance
const int The_buzzer = 4;
float real_distance; // Distance obtained from function
int ldrPin = A0; // LDR pin
unsigned int sensorValue = 0;
float voltage;
float light_amount;
int brightness = 600; // amount of light treshhold
int button_one = 5;
String ButtonOne;
void setup() {
pinMode(red_led, OUTPUT);
pinMode(yellow_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(The_buzzer, OUTPUT);
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(button_one, INPUT);
}
void loop() {
if (Serial.available()>0)
distanceSensor(0); // distance sensor function
{
inVal=Serial.read();
switch((inVal) | (ButtonOne == "HIGH"))
{
case '1': // ------------------------- Regular Mode
while (true)
{
red_light();
yellow_light();
green_light();
yellow_light();
}
break;
case '2': // ------------------------ Pedestrian Mode
while (true)
{
real_distance = distanceSensor(0);
if (real_distance < min_distance)
{
for (int a= 0; a < 10; a++)
{
tone(The_buzzer,1000);
delay(1000);
noTone(The_buzzer);
delay(1000);
digitalWrite(yellow_led, HIGH);
delay(100);
digitalWrite(yellow_led,LOW);
}
}
real_distance = distanceSensor(0);
if (real_distance > min_distance)
{
red_light();
yellow_light();
green_light();
yellow_light();
}
}
break;
case '3': // --------------------------- NIGHT MODE
while (true)
{
light_amount = LDRSensor(0);
real_distance = distanceSensor(0);
if (light_amount > brightness)
{
red_light();
yellow_light();
green_light();
yellow_light();
red_light();
delay(100);
}
if (light_amount < brightness || real_distance < min_distance)
{
real_distance = distanceSensor(0); // distance sensor reading
if (real_distance > min_distance)
{
digitalWrite(green_led, LOW);
digitalWrite(red_led, HIGH);
}
if (real_distance < min_distance )
{
while(real_distance < min_distance && light_amount < brightness)
{ //maybe change this
digitalWrite(red_led, LOW);
digitalWrite(green_led, HIGH);
real_distance = distanceSensor(0);
}
digitalWrite(green_led, LOW);
}
}
}
break;
default:
standby_mode(); // blinks all leds until 1,2 or 3 is selected
}
}
}
//--------------------------------------- FUNCTIONS -----------------------
//----------------------------------- Red light function
void red_light()
{
digitalWrite(red_led, HIGH);
delay(on_delay);
digitalWrite(red_led,LOW);
}
//---------------------------------- Yellow light function
void yellow_light()
{
digitalWrite(yellow_led, HIGH);
delay(off_delay);
digitalWrite(yellow_led,LOW);
}
//--------------------------------- Green light function
void green_light()
{
digitalWrite(green_led, HIGH);
delay(on_delay);
digitalWrite(green_led,LOW);
}
//------------------------------ --- Distance sensor function
float distanceSensor(int x)
{
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN,HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN,LOW);
float distance = pulseIn(ECHOPIN, HIGH);
distance = distance/58;
Serial.print(distance);
Serial.println("cm");
delay(200);
float distance_reading = distance;
return distance_reading;
}
//------------------------------------- LDR sensor function
float LDRSensor(int h)
{
sensorValue = analogRead(ldrPin);
voltage = sensorValue * (5000.0 / 1024.0);
Serial.print("Sensor Output: ");
Serial.println(sensorValue);
Serial.print("Voltage (mv): ");
Serial.println(voltage);
Serial.println();
delay(5000);
return sensorValue;
}
//------------------------------------- Buzzer Function
void buzzer(unsigned char delayms)
{
analogWrite(The_buzzer, 20);
delay(delayms);
analogWrite(The_buzzer, 0);
delay(delayms);
}
// ------------------------------------- Standby Mode
void standby_mode()
{
for ( int a= 10; a < 14; a++)
{
digitalWrite(a,HIGH);
}
delay(off_delay);
for (int b=10; b < 14; b++)
{
digitalWrite(b,LOW);
}
delay(off_delay);
}
As I mentioned in my top comments, once you enter a given case, you never leave it (i.e. things get "stuck")
So, as I said, have a single outer loop, and each case just does one iteration.
Also, note that, below, inVal only gets changed if the serial port has input data available. So, the single loop approach mimics the multiple loops but still responds to changes in input.
Here is something that I think gets you closer to your intent [please pardon the gratuitous style cleanup]:
//----------------------- Variables
#define ECHOPIN 3
#define TRIGPIN 2
char inVal;
String inString = "";
const int red_led = 11;
const int yellow_led = 12;
const int green_led = 13;
const int on_delay = 2000,
off_delay = 1000; // led delays
const int min_distance = 10; // Distance sensor min distance
const int The_buzzer = 4;
float real_distance; // Distance obtained from function
int ldrPin = A0; // LDR pin
unsigned int sensorValue = 0;
float voltage;
float light_amount;
int brightness = 600; // amount of light treshhold
int button_one = 5;
String ButtonOne;
void
setup()
{
pinMode(red_led, OUTPUT);
pinMode(yellow_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(The_buzzer, OUTPUT);
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(button_one, INPUT);
}
void
loop()
{
// distance sensor function
if (Serial.available() > 0)
distanceSensor(0);
while (1) {
if (Serial.available() > 0)
inVal = Serial.read();
switch ((inVal) | (ButtonOne == "HIGH")) {
case '1': // Regular Mode
red_light();
yellow_light();
green_light();
yellow_light();
break;
case '2': // Pedestrian Mode
real_distance = distanceSensor(0);
if (real_distance < min_distance) {
for (int a = 0; a < 10; a++) {
tone(The_buzzer, 1000);
delay(1000);
noTone(The_buzzer);
delay(1000);
digitalWrite(yellow_led, HIGH);
delay(100);
digitalWrite(yellow_led, LOW);
}
}
real_distance = distanceSensor(0);
if (real_distance > min_distance) {
red_light();
yellow_light();
green_light();
yellow_light();
}
break;
case '3': // NIGHT MODE
light_amount = LDRSensor(0);
real_distance = distanceSensor(0);
if (light_amount > brightness) {
red_light();
yellow_light();
green_light();
yellow_light();
red_light();
delay(100);
}
if (light_amount < brightness || real_distance < min_distance) {
real_distance = distanceSensor(0); // distance sensor reading
if (real_distance > min_distance) {
digitalWrite(green_led, LOW);
digitalWrite(red_led, HIGH);
}
if (real_distance < min_distance) {
while (real_distance < min_distance && light_amount < brightness) { // maybe change this
digitalWrite(red_led, LOW);
digitalWrite(green_led, HIGH);
real_distance = distanceSensor(0);
}
digitalWrite(green_led, LOW);
}
}
break;
default: // blinks all leds until 1,2 or 3 is selected
standby_mode();
break;
}
}
}
//--------------------------------------- FUNCTIONS -----------------------
//----------------------------------- Red light function
void
red_light()
{
digitalWrite(red_led, HIGH);
delay(on_delay);
digitalWrite(red_led, LOW);
}
//---------------------------------- Yellow light function
void
yellow_light()
{
digitalWrite(yellow_led, HIGH);
delay(off_delay);
digitalWrite(yellow_led, LOW);
}
//--------------------------------- Green light function
void
green_light()
{
digitalWrite(green_led, HIGH);
delay(on_delay);
digitalWrite(green_led, LOW);
}
//------------------------------ --- Distance sensor function
float
distanceSensor(int x)
{
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
float distance = pulseIn(ECHOPIN, HIGH);
distance = distance / 58;
Serial.print(distance);
Serial.println("cm");
delay(200);
float distance_reading = distance;
return distance_reading;
}
//------------------------------------- LDR sensor function
float
LDRSensor(int h)
{
sensorValue = analogRead(ldrPin);
voltage = sensorValue * (5000.0 / 1024.0);
Serial.print("Sensor Output: ");
Serial.println(sensorValue);
Serial.print("Voltage (mv): ");
Serial.println(voltage);
Serial.println();
delay(5000);
return sensorValue;
}
//------------------------------------- Buzzer Function
void
buzzer(unsigned char delayms)
{
analogWrite(The_buzzer, 20);
delay(delayms);
analogWrite(The_buzzer, 0);
delay(delayms);
}
// ------------------------------------- Standby Mode
void
standby_mode()
{
for (int a = 10; a < 14; a++) {
digitalWrite(a, HIGH);
}
delay(off_delay);
for (int b = 10; b < 14; b++) {
digitalWrite(b, LOW);
}
delay(off_delay);
}
I think you didn't get the way arduino sketches works. The loop() function is called every time in a continuous loop (like a while(true)), so you should make your logic take advantage of this fact.
You use infinite loops inside the loop() function (which is already an infinite loop) so your code gets stuck in one of these loops and never get out, so it will never read the serial buffer or the GPIO pins.

how to control door using SIM900a

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

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