mohamedalimani
Created January 30, 2021

smart garbage bin

a safe and more easy use garbage bin

smart garbage bin

Things used in this project

Story

Read more

Custom parts and enclosures

index_DSSkVr1p5O.jpg

Schematics

img_20210129_000723_yCXuao5Dcd.jpg

Code

wsn_mini_projet.ino

Arduino
#include<Servo.h>
int freq = 4000 ;
const int trigPin = 7;
const int echoPin = 6;
const int buzzer = 9 ;
int servoPin = 3 ; 
Servo Servo1 ;  
long duration;
int distance;
float dist = 200 ;
int flameSensor = A0 ;
int flameValue = 0 ;
const int mq3 = 13 ;
float gazValue ;
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)
int xvalue;
int yvalue;
int zvalue;

void setup() {
pinMode(buzzer,OUTPUT) ;
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
Serial.begin(9600);
Servo1.attach(servoPin) ; 
Serial.println("MQ3 warming up!");
delay(20000); // allow the MQ3 to warm up
}

void loop() {
 // delay(1000);
  dist = calculate_distance() ;
  if (dist <= 10){
   // servo motor open trash can
    openTrash() ;
  }
  detect_flame() ;
  detectGaz() ;
  monitorBalance() ;
}

float calculate_distance(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  return distance ;
}

void detect_flame(){
  flameValue=analogRead(flameSensor) ;
  Serial.print(" flame value = ") ;
  Serial.println(flameValue) ;
  if (flameValue <100){
    Serial.println(flameValue) ;
  tone(buzzer,freq) ;
  }
  else{
  noTone(buzzer) ;
  }
}

void openTrash(){
  Servo1.write(360) ; 
  delay(5000) ; 
  Servo1.write(0) ;
}

void detectGaz(){
  gazValue = digitalRead(mq3) ;
  Serial.print("Sensor Value: ");
  Serial.println(gazValue);
   if (gazValue == 0){
    Serial.println(gazValue) ;
  tone(buzzer,freq) ;
  }
  else{
  noTone(buzzer) ;
  }
  }

  void monitorBalance(){
     yvalue = analogRead(ypin);
  int y = map(yvalue, 268, 403, -100, 100);
  float yg = (float)y/(-100.00);
  yg = yg*180 ;
  Serial.print("\t");
  Serial.print(yg);
  Serial.print("g   ");
   if (yg <= 110){
  Serial.println("trash can going to fall !!!") ;
  tone(buzzer,freq) ;
  }
  else{
  noTone(buzzer) ;
  }
    }

Credits

mohamedalimani
1 project • 2 followers
Contact

Comments

Please log in or sign up to comment.