Ingo Lohs
Published © GPL3+

Trashcan Full?

This contribution is intended for all men who are supposed to bring the paper waste out of their 3rd floor apartment.

BeginnerProtip1 hour1,332
Trashcan Full?

Things used in this project

Hardware components

Photon
Particle Photon
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
4 pieces
×1
Breadboard (generic)
Breadboard (generic)
×1
Particle Power Shield with headers v2.1
×1
3.7 V LiPo Battery
PKCell, LP7850, 2500 mAh, 3.7V
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Code

Trashcan

C/C++
v1.0
// Ingo Lohs, 13.11.2017, works with Particle Photon v0.7.0-rc.3
// trashcan v1.0
// Checks the remaining amount of waste in the paper dustbin

// BOM:
// ** Breadboard
// ** Photon
// ** Ultrasonic HC-SR04
// ** 4 Wires
// ** Particle Power Shield and
// ** LiPo - in my case 2500mAh 3.7V

// Schematic:
// ** only HC-SR04 with trig > A3, echo > A4, VCC > VIN, GND > GND
 
#include <blynk.h>

int baudrate = 9600;               // Serial Monitor 
#define internalLED D7             // internal Device LED from Spark Core or Particle Photon
const int tank_max = 90;           // if distance in cm from Ultrasonic Sensor >=90 could be that the lid of the paper bin can no longer be closed = max. filling in cm
long distance, duration;           // UltraSonic values
#define trigPin	A3                 // UltraSonic HC-SR04 Pin Definitions
#define echoPin	A4                 // UltraSonic HC-SR04 Pin Definitions

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "your Blynk code here";

unsigned long lastmillis = 0;      // time for iteration the loop

// *********  

void setup() 
{
  Serial.begin(baudrate);
  while(!Serial);                   // Waiting for Serial connection
  pinMode(internalLED, OUTPUT);     // Set pin D2 as an OUTPUT
  digitalWrite(internalLED, LOW);   // internal LED off
  pinMode(trigPin, OUTPUT);         // Set pin A3
  pinMode(echoPin, INPUT);          // Set pin A4
  Blynk.begin(auth);                // Blynk magic starts here
}
 
// *********     
    
void loop() 
{
    Blynk.run();    
    
//    !!! http://docs.blynk.cc/#troubleshooting-flood-error  
//    no delays in loop - DO NEVER in LOOP!

            if ((millis() - lastmillis) > 2000) { // run every 2 seconds
                lastmillis = millis();
                digitalWrite(internalLED, HIGH);  // internal LED on
                  readData();
                  checkData();
                digitalWrite(internalLED, LOW);   // internal LED off
            }

}

// *********  

void readData()
{
      // read the Distance with the Ultrasonic HC-SR04
      digitalWrite(trigPin, LOW);  
      delayMicroseconds(2); 
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10); 
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration/2) / 29.1;
        
      Serial.print("Mülleimer: ");
      Serial.print(distance);
      Serial.println(" cm");
      
      // put a Graph-Widget in your Blynk App and map it to V0 (400 Energy Points)
      Blynk.virtualWrite(V0, distance);
}

// *********  

void checkData()
{
    // step 1 - Validate the Distance  - check the right position from UltraSonic Sensor - larger then 0 cm?
      if (distance == 0)
      {
        Serial.println("Distance Value = 0!");
        Blynk.virtualWrite(V1, "Error: Distance Value = 0!");
      }
    // step 2 - Is the distance in range?
      else if (distance >= tank_max || distance <= 0)
      {
        Serial.println("Out of range! Check position from UltraSonic Sensor!");
        Blynk.virtualWrite(V1, "Error: Echo distance problem!");
      }
      else
      {
        Blynk.virtualWrite(V1, "...Value read...");
      }
}

Trashcan

v1.0

Credits

Ingo Lohs

Ingo Lohs

182 projects • 194 followers
I am well over 50 years and come from the middle of Germany.

Comments