Slow Functioning of Code

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:-

  1. Home Security through Ultrasnoic Sensor
  2. Home Security through IR Sensor
  3. Home Security through Gas Detection Sensor
  4. Auto Light On off via Remote
  5. Auto Light on of via LDR
  6. 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

You have 1 second delay in the code. What for?

So that it do not get crazy and detects normally should I remove it?

comment out the code in loop()
then in sequence add in the code for each device until you find the one causing the delays
then see if you can figure out the cause

Who knows if not you?

Not getting you.
I am not able to understand what you are saying can you pls suggest some changes in code?

Actually i am newbie here can you pls suggest should I remove or not and also you can ask me any additional information if you need i will response immediately

if you have a number of sections of code executing in sequence each controlling/reading a device how do you find which is causing the problem - it can be very difficult
one way is to comment out each section using /* .... */, e.g.

//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;
*/

then add each section in by removing the enclosing /* ... */ until you find the one causing the problem
in practice you may get interaction between devices etc etc which can make it difficult to sort out the problem - you may end up trying different combinations of devices

What is the point of such a project if you do not understand what you are doing? You need to understand the code, understand what is happening in it.
If there are unclear places - ask the forum.

Thanks for your replies just want to ask one thing likewise here

there is delay of 1 second so when the ir sensor send low than it will apply 1 second ?

yes.
And 3 lines below you have another 1 sec delay

Ya I have removed and update my code here is my 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);
  }
  else
    digitalWrite(irled , LOW);
  digitalWrite(irbuz , LOW);

  //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 here is the updated code a bit fast process is there but the main issue is occurine with me is here

//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;


this sensor is working after 25 second

But tried uploading this code individually it work as expected and fast pls suggest

What do you mean?

1 Like

I mean to say that I want to build a automatic light for any room mean to say ir sensor will detect the person when entering the room and when the person is going out ir detect the person again and then the light turn off automatically/

Basically I want to do it for bathroom .

So Ir detect 1 time led turn on and 2 time detection led turn of

Here is the code

//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;


But this function is working after around 25-30 second after detection !!!

Pls Help

There is nothing in this code that can cause a delay of 25 seconds. It can be five seconds maximum, no more.
Are you sure you checked EXACTLY this code?

Use the advice from post #4 - gradually comment on parts of the program and measure the delay until it drops to 1-2 seconds
This way you will know which part of the code is slowing you down.

have you tested each device in a seperate program to check it does what you require?
once each device performs to specification you can start to merge into a complete system.
attempting to implement a complex system with multiple devices before you know each works as required can be very difficult if not impossible

1 Like

Ya that's what i want to say I have tested each and every program and device in an individual program but when I merge it, it is working so slow.

That's why I want some resolution

I think it is hard to give a "resolution" without your tests with the removing parts of the code...

Just A moment I am attaching the file for my code
Ono I can't attach the file

This will get very unreliable. Think about it what could happen if the IR sensor detects the person on entry but fires a second time due to some movements. The second event will switch the light off again.

Therefore I would introduce a short "blocking" time for let's say 3-5 seconds to not immediately shut off the light again.

I think a "finite state machine" could help you for that. Google for it.