SmarTrash #ArduinoCloudGames
A smart IoT solution for trash monitoring and management in cities and homes. #ArduinoCloudGames
Components and supplies
1
PIR Sensor, 7 m
1
Arduino Oplà IoT Kit
1
Ultrasonic Sensor - HC-SR04 (Generic)
Tools and machines
1
Bin/Trash can
Apps and platforms
1
Arduino Web Editor
1
Arduino IoT Cloud
Project description
Code
smarTrash_code
arduino
This is the fully commented code for smarTrash. ** If you are going to implement this very code with the dashboarding proposed remember to change variable names accordingly :)
1// -------------- smarTrash --------------------- 2 3#include "thingProperties.h" 4#include <Arduino_MKRIoTCarrier.h> 5MKRIoTCarrier carrier; 6#define TRIG_PIN 10 // trigger pin for Ultrasonic sensor 7#define ECHO_PIN 4 // Echo pin for ultrasonic sensor 8 9 10void setup(){ 11 float temperatura; // definition of temperature variable 12 float soglia; // definition of threshold variable 13 float distanza; // definition of ultrasonic measurement variable 14 float umidita; // definition of humidity variable 15 16 17 // Connectiion to Wi-Fi network 18 Serial.begin(9600); 19 while (!Serial); 20 initProperties(); 21// Connect to Arduino IoT Cloud 22 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 23 //Get Cloud Info/errors , 0 (only errors) up to 4 24 setDebugMessageLevel(2); 25 ArduinoCloud.printDebugInfo(); 26 27 //Wait to get cloud connection to init the carrier 28 while (ArduinoCloud.connected() != 1) { 29 ArduinoCloud.update(); 30 delay(500); 31 } 32 33 //Carrier Initialization 34 carrier.begin(); 35 carrier.display.setRotation(0); 36 delay(1500); 37 38 39 // PinMode definition 40 pinMode(TRIG_PIN, OUTPUT); //trigger pin in output 41 pinMode(ECHO_PIN, INPUT); // echo pin in output 42 digitalWrite(TRIG_PIN, LOW); // initialization of trigger pin to low 43 pinMode(2,INPUT); // pinMode def. for PIR motion sensor 44 45} 46void loop() { 47 ArduinoCloud.update(); 48 // whenever the motion sensor detects a movement the measurement 49 // is paused, in order to avoid incorrect trash level measurements. 50 // The serial port also prints that bin is open. 51 52 if(digitalRead(2)==HIGH){ 53 Serial.print("The bin is open"); 54 Serial.print('\n'); 55 } 56 57 // When the sensor does not detect any movement for a while (the "while" 58 // is set via the manual regulation of one screw on the motion sensor), the 59 // measurement restarts. The measurements consists of a trash level, 60 // temperature and humidity 61 else{ 62 // Serial print on the state of the bin 63 Serial.print("The bin is closed "); 64 Serial.print('\n'); 65 // Distance measured by the ultrasonic sensor 66 digitalWrite(TRIG_PIN, HIGH); 67 delayMicroseconds(10); 68 digitalWrite(TRIG_PIN, LOW); 69 unsigned long tempo = pulseIn(ECHO_PIN, HIGH); 70 // Trash level (distanza) is measured in centimetres as the height of the bin (26.5cm) 71 // - the distance measured between the ultrasonic sensor and the bin. 72 distanza = (-(0.03438 * tempo / 2)+26.5);// 73 delay(1000); 74 // Serial print for the trash level 75 Serial.println("Trash Level: " + String(distanza) + "cm"); 76 Serial.print('\n'); 77 // Carrier measurement of temperature and serial print 78 temperatura = carrier.Env.readTemperature(CELSIUS); 79 Serial.print("Temperature: " +String(temperatura)+ "C"); 80 Serial.print('\n'); 81 // Carrier measurement of humidity and serial print 82 umidita = carrier.Env.readHumidity(); 83 Serial.print("Humidity: " +String(umidita) +"%"); 84 Serial.print('\n'); 85 // A threshold adjustment is introduced for temperature variations, 86 // in case the temperature is too high the threshold is reduced to 15 cm. 87 // Otherwise it is set to 20 cm 88 // The goal of this is to reduce the chance of rotting in food waste, 89 // it can be disactivated in case the bin is used for plastic. 90 if (temperatura>20){ 91 soglia=15; 92 } 93 else { 94 soglia=20; 95 } 96 // In case the distance is above the defined threshold then the 97 // carrier display turns red and a message is sent via the cloud 98 // that notifies that the bin is full. 99 if(distanza>soglia){ 100 carrier.display.fillScreen(ST77XX_RED); 101 notifica = "The bin is full! Please take out the trash"; 102 } 103 // In case the threshold is below the defined threshold then the 104 // carrier display turns red and a message is sent via the cloud 105 // that notifies that the bin is no longer full. 106 else{ 107 carrier.display.fillScreen(ST77XX_GREEN); 108 notifica = "The bin is no longer full!"; 109 } 110 } 111 delay(2000); 112 113 // All credits to DevotiLabs 114 // feel free to use our code and project, and don't hesitate to tell 115 // us what you end up doing with it ! 116 117 // Cheers, 118 // Eleonora, Federico, Claudio 119 120 }
Comments
Only logged in users can leave comments