Damir MukametkarimFabio Di Spazioahmed mohamed galal osman
Published

Smart Trash Bin

Smart trash bin that separates metal waste from non-metal.

IntermediateWork in progress318
Smart Trash Bin

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Inductive Proximity Sensor, Stainless Steel
Inductive Proximity Sensor, Stainless Steel
×1
STMicroelectronics NFC TAG EXPANSION BOARD M24SR
×1
STM32 Nucleo-64 Board
STMicroelectronics STM32 Nucleo-64 Board
×1

Software apps and online services

Mbed Studio
Arm Mbed Mbed Studio
Android Studio
Android Studio
Firebase
Google Firebase

Story

Read more

Code

smart bin

C/C++
#include "mbed.h"
#include "ultrasonic.h"
#include "Servo.h"
#include "X_NUCLEO_NFC01A1.h"
#include "NDefLib/NDefNfcTag.h"
#include "NDefLib/RecordType/RecordURI.h"
#include "NDefLib/RecordType/RecordText.h"

Serial pc(USBTX, USBRX);
DigitalIn mySwitch(D6);
DigitalOut myled(LED1);
Servo myservo(D3);

// variables
int angle = 0;    // variable to store the servo position
int flag = 0; 

bool inductiveTest()
{
    if (mySwitch == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int updateM24SR(X_NUCLEO_NFC01A1 *nfcNucleo, char *data)
{

    //retrieve the NdefLib interface
    NDefLib::NDefNfcTag &tag = nfcNucleo->getM24SR().getNDefTag();
    printf("System Init done: !\n\r");
    /*SESSION 1*/
    //open the i2c session with the nfc chip
    if (tag.openSession())
    {
        printf("Session opened\n\r");
        nfcNucleo->getLed1() = 1;

        //create the NDef message and record
        NDefLib::Message msg;
        NDefLib::RecordText rText(data);
        msg.addRecord(&rText);

        //write the tag
        if (tag.write(msg))
        {
            printf("Tag written\n\r");
            nfcNucleo->getLed2() = 1;
        }
        else
        {
            printf("Error writing \n\r");
        } //if-else

        //close the i2c session
        if (tag.closeSession())
        {
            printf("Session closed\n\r");
            nfcNucleo->getLed3() = 1;
        }
        else
        {
            printf("Error closing the session\n\r");
        } //if-else
    }
    else
        printf("Error opening the session\n\r");
}

void dist(int distance)
{
    if (distance > 90) //mm
    {
        pc.printf("Distance > 90 %d mm\r\n", distance);
        myservo.write(180);
        flag = 0;
    }
    else //if (distance < 90)
    {
        //        wait(1);
        pc.printf("Distance <= 90 %d mm\n\r", distance);
        if (inductiveTest())
        {
            myled = !myled;
            pc.printf("OBJECT DETECTED - Start Metal sensor\n\r", mySwitch);
            myservo.write(0);
            flag = 1;
        }
    }
}

ultrasonic mu(D8, D9, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9
                                     //have updates every .1 seconds and a timeout after 1
                                     //second, and call dist when the distance changes

int main(void)
{

    pc.baud(9600);
     myservo.write(angle);
    //use default board pinout
    I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN, X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);

    X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel, NULL,
                                                             X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN, X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN,
                                                             X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN, X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN,
                                                             X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN);

    //retrieve the NdefLib interface
    NDefLib::NDefNfcTag &tag = nfcNucleo->getM24SR().getNDefTag();

    mu.startUpdates(); //start measuring the distance
    //Do something else here
    //call checkDistance() as much as possible, as this is where
    //the class checks if dist needs to be called.

    for (size_t i = 0; i < 50; i++)
    {
        mu.checkDistance();
        printf("Check Distance: !\n\r");
        if (flag == 1)
        {
            updateM24SR(nfcNucleo, "YES");
            printf("Inside the write tag condition: !\n\r");
            wait(2);
            NVIC_SystemReset();
        }
    }

//  to delete the tag and not give credit
    updateM24SR(nfcNucleo, "NO");
    printf("not metal: !\n\r");

    pc.printf("wait for 10 sec\n\r");
    wait(10);
    NVIC_SystemReset();
}

Credits

Damir Mukametkarim

Damir Mukametkarim

2 projects • 0 followers
Fabio Di Spazio

Fabio Di Spazio

3 projects • 4 followers
ahmed mohamed galal osman

ahmed mohamed galal osman

3 projects • 0 followers

Comments