Go Down

Topic: Sound sensor & LED delay in automation project (Read 43 times) previous topic - next topic

ajmemeni

Hi everyone, I hope I can resolve these 2 things:

I have arduino project - House automation

I need to adjust few things:


 1. milis instead delay
- for example LED via ldr and PIR motion sensor - because when I use delay led is ON for few seconds, but in the meantime I cant use my other sensors ( I cant use RFID card reader when LED is on)

I tried few things, but nothing worked so far..

Code: [Select]
void readLIGHT()
{
 
 String string1 = "Lighting percentage: ";
  String string2 = string1 + light;

  int value_pir = digitalRead(PIR);

  String string3 = "Motion detector: ";
  String string4 = string3 + value_pir;
  light = map(analogRead(ldrPin) , 0 , 1024 , 1 , 100);
  Serial.println(string2);
  Serial.println(string4);
  delay(1000);


  if ((light < 50) && ( value_pir == HIGH) )       //This is percentage of light intensity. can be changed between 0 - 100
  {
    digitalWrite(lightingOutput, HIGH);
    Serial.println("Lighting ON");
    delay(8000);

  }
  else
  {
    digitalWrite(lightingOutput, LOW);
    Serial.println("Lighting OFF");

  }

}





2. I have working code for sound sensor - When I clap hands turn on/off LED,
but when I put code in my automation project with others - it doesnt work, its somehow blocked by other sensors/code?

This is sound sensor clap code:

Code: [Select]
int LED=5;

int soundSensor=A1;

boolean LEDStatus=false;
   

void setup() {

Serial.begin(9600);

  pinMode(soundSensor,INPUT);
  pinMode(LED,OUTPUT);

}

void loop() {


  int SensorData=analogRead(soundSensor);

 
  if(SensorData>600){

    if(LEDStatus==false){
        LEDStatus=true;
        digitalWrite(LED,HIGH);
    }
    else{
        LEDStatus=false;
        digitalWrite(LED,LOW);
    }

    delay(50);
  }



 }



Whole code:

Code: [Select]
#include <Servo.h>
#include "DHT.h"
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>


#define SERVO_PIN 8
#define SS_PIN 10
#define RST_PIN 9

#define lightingOutput A2
#define ldrPin A0
#define PIR 7

#define ledGrn 6

#define heatingOutput A3
#define coolingOutput A4

#define DHTPIN 2
#define DHTTYPE DHT11


MFRC522 mfrc522(SS_PIN, RST_PIN); // Create instance of our reader
Servo myservo; // Create instance of our motor /// create servo object to control a servo

DHT dht(DHTPIN, DHTTYPE);     //// Initialize DHT sensor for normal 16mhz Arduino


int pos;
int light;
float h, t;
int soundSensor=A1;
int LED=5;
boolean LEDStatus=false;
const int buzzer = 4;

void setup()
{

  Serial.begin(9600);
  dht.begin();
  SPI.begin();
  mfrc522.PCD_Init();
  myservo.attach(SERVO_PIN);
  myservo.write(0);


  pinMode(heatingOutput, OUTPUT);
  pinMode(coolingOutput, OUTPUT);
  pinMode(ledGrn, OUTPUT);
  pinMode(soundSensor,INPUT);
  pinMode(LED,OUTPUT);
  pinMode(lightingOutput, OUTPUT);

 
}
void loop() {

  Serial.println("\nHOME AUTOMATION\n");


  readSOUND();
  readRFID();
  readDHT();
  readLIGHT();


}


////////////////////////////////////////////
void readRFID()

{
 


  //Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent() ) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial() ) {
    return;
  }
  // If a card is detected, execute the following:
  Serial.println("Time to open");
  // Print the card's ID
  String content = "";
  byte letter;
  for ( byte i = 0; i < mfrc522.uid.size; i++ ) {
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
    if ( i < mfrc522.uid.size - 1 ) content += "-";
  }
  content.toUpperCase();
  Serial.println();
  Serial.println("UID tag :'" + content + "'");

  if (content == "C1-E5-86-25") {

    Serial.println("Authorized access");

    digitalWrite(ledGrn, HIGH);


    for (pos = 0; pos <= 90; pos += 1) {
      myservo.write(pos);
      delay(25);
    }
    for (pos = 90; pos >= 0; pos -= 1) {
      myservo.write(pos);
      delay(70);

      digitalWrite(ledGrn, LOW);

    }
  }
  else {
    Serial.println("Access Denied");
      tone(buzzer, 1000, 6000);

  }
}

//////////////////////////////////////////////////////

void readDHT()
{




  h = dht.readHumidity();
  t = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.println();


  if (t < 18) //if temperature is less than 25 celcius
  {
    digitalWrite(heatingOutput, HIGH);
    Serial.print("Heating activated\n");
  }
  else
  {
    digitalWrite(heatingOutput, LOW);
  }

  if (t > 22) //if temperature is more than 30 celcius
  {
    digitalWrite(coolingOutput, HIGH);
    Serial.print("Cooling activated\n");
  }
  else
  {
    digitalWrite(coolingOutput, LOW);
  }

}



//////////////////////////////////////////////////////

void readLIGHT()
{
 
 String string1 = "Lighting percentage: ";
  String string2 = string1 + light;

  int value_pir = digitalRead(PIR);

  String string3 = "Motion detector: ";
  String string4 = string3 + value_pir;
  light = map(analogRead(ldrPin) , 0 , 1024 , 1 , 100);
  Serial.println(string2);
  Serial.println(string4);
  delay(1000);



  if ((light < 50) && ( value_pir == HIGH) )                           
  {
    digitalWrite(lightingOutput, HIGH);
    Serial.println("Lighting ON");
    delay(8000);


  }
  else
  {
    digitalWrite(lightingOutput, LOW);
    Serial.println("Lighting OFF");

  }


}


//////////////////////////////////////////////////////////////

void readSOUND()
{
  int SensorData=analogRead(soundSensor);

 
  if(SensorData>600){

    if(LEDStatus==false){
        LEDStatus=true;
        digitalWrite(LED,HIGH);
    }
    else{
        LEDStatus=false;
        digitalWrite(LED,LOW);
    }

    delay(50);
  }

}

Go Up