Created
November 2, 2024 16:52
-
-
Save marti1125/f3a4564182ddff00118e4aa937fff16d to your computer and use it in GitHub Desktop.
arduino project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal_I2C.h> | |
#include <Servo.h> | |
LiquidCrystal_I2C lcd(0x27,16,2); | |
Servo servo_7; | |
int MINIMO=20; //Distancia minima en cm | |
int TRIGGER1=11; | |
int ECHO1=10; | |
int TRIGGER2=9; | |
int ECHO2=8; | |
int LED1=7; | |
int LED2=6; | |
int contador,distancia1,distancia2; | |
int output1Value = 0; | |
int sen1Value = 0; | |
int sen2Value = 0; | |
int const gas_sensor = A1; | |
int const LDR = A0; | |
int limit = 400; | |
void setup(){ | |
lcd.init(); | |
lcd.clear(); | |
lcd.backlight(); | |
pinMode(TRIGGER1,OUTPUT); | |
pinMode(ECHO1,INPUT); | |
pinMode(TRIGGER2,OUTPUT); | |
pinMode(ECHO2,INPUT); | |
pinMode(LED1,OUTPUT); | |
pinMode(LED2,OUTPUT); | |
contador=0; | |
//Configuracion de lo Nuevo// | |
Serial.begin(9600); //initialize serial communication | |
pinMode(A0, INPUT); //LDR | |
pinMode(A1,INPUT); //gas sensor | |
pinMode(13, OUTPUT); //connected to relay | |
servo_7.attach(2, 500, 2500); //servo motor | |
pinMode(8,OUTPUT); //signal to piezo buzzer | |
pinMode(4, INPUT); //signal to PIR | |
pinMode(10, OUTPUT); //signal to npn as switch | |
pinMode(4, OUTPUT); //Red LED | |
pinMode(3, OUTPUT); //Green LED | |
} | |
void loop() { | |
distancia1=ultrasonido1(); | |
if(distancia1<=MINIMO){ | |
contador++; | |
digitalWrite(LED1,HIGH); | |
while(true){ | |
delay(500); | |
distancia1=ultrasonido1(); | |
if(distancia1>MINIMO)break; | |
} | |
digitalWrite(LED1,LOW); | |
} | |
distancia2=ultrasonido2(); | |
if(distancia2<=MINIMO){ | |
contador++; | |
digitalWrite(LED2,HIGH); | |
while(true){ | |
delay(500); | |
distancia2=ultrasonido2(); | |
if(distancia2>MINIMO) break; | |
} | |
digitalWrite(LED2,LOW); | |
} | |
lcd.setCursor(0,0); | |
lcd.print("Contador:"); | |
lcd.print(contador); | |
//-------------------------------------------------------------- | |
//------light intensity control------// | |
//-------------------------------------------------------------- | |
int val1 = analogRead(LDR); | |
if (val1 > 500) { | |
digitalWrite(13, LOW); | |
Serial.print("Bulb ON = "); | |
Serial.print(val1); | |
} else { | |
digitalWrite(13, HIGH); | |
Serial.print("Bulb OFF = "); | |
Serial.print(val1); | |
} | |
//-------------------------------------------------------------- | |
//------ light & fan control --------// | |
//-------------------------------------------------------------- | |
sen2Value = digitalRead(9); | |
if (sen2Value == 0) | |
{ | |
digitalWrite(10, LOW); //npn as switch OFF | |
digitalWrite(4, HIGH); // Red LED ON,indicating no motion | |
digitalWrite(3, LOW); //Green LED OFF, since no Motion detected | |
Serial.print(" || NO Motion Detected " ); | |
} | |
if (sen2Value == 1) | |
{ | |
digitalWrite(10, HIGH);//npn as switch ON | |
delay(5000); | |
digitalWrite(4, LOW); // RED LED OFF | |
digitalWrite(3, HIGH);//GREEN LED ON , indicating motion detected | |
Serial.print(" || Motion Detected! " ); | |
} | |
//--------------------------------------------------------------- | |
// ------- Gas Sensor --------// | |
//--------------------------------------------------------------- | |
int val = analogRead(gas_sensor); //read sensor value | |
Serial.print("|| Gas Sensor Value = "); | |
Serial.print(val); //Printing in serial monitor | |
if (val > limit) { | |
tone(8, 650); | |
} | |
delay(300); | |
noTone(8); | |
//-------------------------------------------------------------- | |
//------- servo motor ---------// | |
//------------------------------------------------------------- | |
sen1Value = 0.01723 * readUltrasonicDistance(6, 6); | |
if (sen1Value < 100) | |
{ | |
servo_7.write(90); | |
Serial.print(" || Door Open! ; Distance = "); | |
Serial.print(sen1Value); | |
Serial.print("\n"); | |
} | |
else | |
{ | |
servo_7.write(0); | |
Serial.print(" || Door Closed! ; Distance = "); | |
Serial.print(sen1Value); | |
Serial.print("\n"); | |
} | |
delay(10); | |
} | |
int ultrasonido1(){ | |
int resultado; | |
//Se envia la señal al ultrasonido para iniciar | |
digitalWrite(TRIGGER1,HIGH); | |
delayMicroseconds(10); | |
digitalWrite(TRIGGER1,LOW); | |
//Arduino espera la señal de retorno o se agote tiempo de espera | |
//para caluclar el tiempo en microsegundos | |
//11600/58=200cm, tiempo máximo de espera | |
resultado=pulseIn(ECHO1,HIGH,11600); | |
//Si el tiempo de espera se agota, devuelve cero, por lo que se considera | |
//el máximo valor de alcance | |
if(resultado==0) resultado=11600; | |
//Conversion del valor del tiempo en microsegundos a centímetros | |
resultado=resultado/58; | |
//Devuelve resultado | |
return resultado; | |
} | |
int ultrasonido2(){ | |
int resultado; | |
//Se envia la señal al ultrasonido para iniciar | |
digitalWrite(TRIGGER2,HIGH); | |
delayMicroseconds(10); | |
digitalWrite(TRIGGER2,LOW); | |
//Arduino espera la señal de retorno o se agote tiempo de espera | |
//para caluclar el tiempo en microsegundos | |
//11600/58=200cm, tiempo máximo de espera | |
resultado=pulseIn(ECHO2,HIGH,11600); | |
//Si el tiempo de espera se agota, devuelve cero, por lo que se considera | |
//el máximo valor de alcance | |
if(resultado==0) resultado=11600; | |
//Conversion del valor del tiempo en microsegundos a centímetros | |
resultado=resultado/58; | |
//Devuelve resultado | |
return resultado; | |
} | |
//Configuracion de lo Nuevo// | |
long readUltrasonicDistance(int triggerPin, int echoPin) | |
{ | |
pinMode(triggerPin, OUTPUT); // Clear the trigger | |
digitalWrite(triggerPin, LOW); | |
delayMicroseconds(2); | |
// Sets the trigger pin to HIGH state for 10 microseconds | |
digitalWrite(triggerPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(triggerPin, LOW); | |
pinMode(echoPin, INPUT); | |
// Reads the echo pin, and returns the sound wave travel time in microseconds | |
return pulseIn(echoPin, HIGH); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment