Sample Code RAJ

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

#include <SPI.

h>
#include <SD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Adafruit_GPS.h>
#include <NewPing.h>
#include <HX711.h>
#include <SoftwareSerial.h>

// Define pin numbers


#define PIR_PIN 2
#define BUZZER_PIN 3
#define TRIGGER_PIN 4
#define ECHO_PIN 5
#define SERVO_PIN 6
#define GPS_TX_PIN 7
#define GPS_RX_PIN 8
#define LOADCELL_DOUT_PIN 9
#define LOADCELL_SCK_PIN 10
#define LED_PIN 11
#define SHREDDER_PIN 12

// Define constants
#define DISTANCE_THRESHOLD 20 // Threshold for waste bin proximity in cm
#define WEIGHT_THRESHOLD 1000 // Threshold for triggering shredder in grams

// Initialize modules
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address may vary
Servo servoMotor;
Adafruit_GPS GPS(&Serial);
NewPing sonar(TRIGGER_PIN, ECHO_PIN);
HX711 scale;
SoftwareSerial esp8266(GPS_TX_PIN, GPS_RX_PIN);

// Global variables
bool motionDetected = false;
bool wasteDetected = false;
bool shredderActive = false;
long lastShreddingTime = 0;

void setup() {
Serial.begin(9600);
lcd.begin();
lcd.backlight();
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(SHREDDER_PIN, OUTPUT);

servoMotor.attach(SERVO_PIN);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale();
scale.tare();

GPS.begin(9600);
esp8266.begin(9600);

// Initialize SD card
if (!SD.begin(10)) {
lcd.print("SD Card Error");
while (true);
}

// Initialize GPS
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
}

void loop() {
checkMotion();
checkWaste();
checkWeight();
checkGPS();
shredderControl();
}

void checkMotion() {
if (digitalRead(PIR_PIN) == HIGH) {
motionDetected = true;
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
} else {
motionDetected = false;
}
}

void checkWaste() {
if (sonar.ping_cm() < DISTANCE_THRESHOLD) {
wasteDetected = true;
digitalWrite(LED_PIN, HIGH);
servoMotor.write(90); // Open waste compartment
delay(5000); // Adjust time for waste disposal
servoMotor.write(0); // Close waste compartment
digitalWrite(LED_PIN, LOW);
} else {
wasteDetected = false;
}
}

void checkWeight() {
float weight = scale.get_units();
if (weight > WEIGHT_THRESHOLD && !shredderActive) {
shredderActive = true;
lastShreddingTime = millis();
digitalWrite(SHREDDER_PIN, HIGH);
}
}

void shredderControl() {
if (shredderActive && millis() - lastShreddingTime >= 5000) {
shredderActive = false;
digitalWrite(SHREDDER_PIN, LOW);
}
}

void checkGPS() {
char c = GPS.read();
if (GPS.newNMEAreceived() && GPS.parse(c)) {
if (GPS.fix) {
// Transmit GPS data to central database via ESP8266

esp8266.print("AT+CIPSTART=\"TCP\",\"your_server_address\",your_port_number\r\
n");
delay(1000);
esp8266.print("AT+CIPSEND=");
esp8266.println(50);
delay(1000);
esp8266.print("Latitude: ");
esp8266.println(GPS.latitudeDegrees, 6);
esp8266.print("Longitude: ");
esp8266.println(GPS.longitudeDegrees, 6);
delay(1000);
esp8266.print("AT+CIPCLOSE\r\n");
delay(1000);
}
}
}

You might also like