Hello Everyone.
I am new to arduino platform .
I am building up a project using arduino
My main goals for this project are as follow:-
- Home Security through Ultrasnoic Sensor
- Home Security through IR Sensor
- Home Security through Gas Detection Sensor
- Auto Light On off via Remote
- Auto Light on of via LDR
- Auto Light On for for Room
So I have done the codding and all the things are going well and my code is also working well but the process is soooo slow.
The code is running as expected but it is not fast as per arduino
here is the code
#include <IRremote.h>
const int trig = 2; // Ultrasnoic Sensor
const int echo = 3; // Ultrasnoic Sensor
const int autopin = 4; //Rmote Recieving Sensor
const int ldr = A0; // Auto Day and Night
const int gas = A1; //Gas Detection
const int ir = A2; //IR Sensor for Security
const int bathroomir = A3; //Bathroom IR for Auto Light
const int ultraled = 5; //Ultrasnoic LED
const int ultrabuz = 6; // Ultrasnoic Buzzer
const int irled = 7; // IR Security LED
const int irbuz = 8; // IR Security Buzzer
const int ldrled = 9; // Auto LDR
const int bathroomled = 10; // Bathroom Auto LED
const int autoled = 11; // Button for On Off switch
const int gasled = 12; //Gas Sensor LED
const int gasbuz = 13; // Gas Sensor Buzzer
const int gaswater = A4; // Gas Sensor Water F;pw
const int extraldrled = A5;
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int val;
int gasmax = 90; ///note to check the gas sensor value
int counter;
int distance;
IRrecv irrecv(autopin);
decode_results results;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
long duration;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(ultrabuz, OUTPUT);
pinMode(ultraled, OUTPUT);
pinMode(irled, OUTPUT);
pinMode(irbuz, OUTPUT);
pinMode(ir, INPUT);
pinMode(ldrled, OUTPUT);
pinMode(ldr, INPUT);
pinMode(gas , INPUT);
pinMode(gaswater, OUTPUT);
pinMode(gasled , OUTPUT);
pinMode(gasbuz , OUTPUT);
pinMode(bathroomir, INPUT);
pinMode(bathroomled, OUTPUT);
pinMode(autoled, OUTPUT);
pinMode(extraldrled, OUTPUT);
digitalWrite(bathroomled, ledState);
irrecv.enableIRIn();
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(5);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
distance = duration * 0.034 / 2;
duration = pulseIn(echo, HIGH);
val = analogRead(gas);
//AUTO SWITCH ON OF
if (irrecv.decode(&results))
{
switch (results.value)
{
case 0x1FEE01F : ////Note To Change the code
digitalWrite(autoled, HIGH);
break;
case 0x1FE10EF:
digitalWrite(autoled, LOW);
break;
default:
break;
}
irrecv.resume();
}
//AUTO BATHROOM LIGHT ON OF
int reading = digitalRead(bathroomir);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(bathroomled, ledState);
lastButtonState = reading;
// AUTO GAS DETECTION
// //if (val= gasmax) {
//
// Serial.println("Alert Somke Detected!!!!");
// digitalWrite(gaswater, HIGH);
// digitalWrite(gasled , HIGH);
// digitalWrite(gasbuz, HIGH);
// delay(500);
// digitalWrite(gasled , LOW);
// digitalWrite(gasbuz, HIGH);
// delay(500);
// }
// else {
// digitalWrite(gasled , LOW);
// digitalWrite(gasbuz, LOW);
// digitalWrite(gaswater, LOW);
// }
//AUTO LDR LIGHT ON / OF
if ( digitalRead( ldr) == 1) {
digitalWrite( ldrled, HIGH);
digitalWrite(extraldrled, HIGH);
}
else {
digitalWrite( ldrled , LOW);
digitalWrite(extraldrled, LOW);
}
//AUTO IR SECURITY
if (digitalRead(ir) == LOW) {
Serial.println("Alert Ir Detects Object");
digitalWrite(irled , HIGH);
digitalWrite(irbuz , HIGH);
delay(1000);
}
else
digitalWrite(irled , LOW);
digitalWrite(irbuz , LOW);
delay(1000);
//AUTO ULTRASNOCI SENSOR SECUTIRY
if (distance <= 40) {
Serial.print(distance);
Serial.println("cm Extreme Risk ");
digitalWrite(ultraled , HIGH);
digitalWrite(ultrabuz , HIGH);
delay(900);
digitalWrite(ultraled , LOW);
digitalWrite(ultrabuz , LOW);
delay(900);
}
else if (distance >= 41 && distance <= 55) {
Serial.print(distance);
Serial.println("cm HIGH Risk");
digitalWrite(ultraled , HIGH);
digitalWrite(ultrabuz , HIGH);
delay(1400);
digitalWrite(ultraled , LOW);
digitalWrite(ultrabuz , LOW);
delay(1400);
}
else
digitalWrite(ultraled, LOW);
digitalWrite(ultrabuz, LOW);
}
So this is my code.
Pls suggest some action or improvement in code so that my function run with rocket speed without any disturbance
Thanks