Midterm Project: “This is a Trashcan” Documentation

This is a Trashcan

By: Sherry Wu, Eva He

Final Work:

Planning/Brainstorm:

When we started to brainstorm this project, we wanted to make something useful and practical in our real life. I was inspired by a moving robot, and decided to create a robot trashcan. So I don’t need to take time to find a trashcan when I have trash. But a simple trashcan might be boring, so we decided to create something special on its physical appearance.

Process:

We first started to think about our code. 

First, we need to let the cap open and close, so we decide to use a servo to control the cap’s open and close. Then, we want the cap to open when our hand is nearby the trashcan, so we need to have a ultrasound sensor placed on the surface of the trash can. Other from the normal functions, we want the trashcan to note when trash is full. So we put an ultrasound sensor near the top of the trashcan, so it will warn us when it is full. After we built the prototype for our user testing day, we adjusted several codes, and it became like this.

Eva and I came up with the fundamental code together, and then we brake up for further build up. I finished connecting Arduino and code testing, Eva was in charged of 3D printing. We 3D printed the cap and the bottom part of our trashcan, everything went well.

int TrigCap = 5;
int EchoCap = 6;
     
int TrigTrash = 7;
int EchoTrash = 8;

#include <Servo.h>
Servo myservo;
int servoPin = 2;

int buzzPin = 9;
long freqency = 2000;

     
void setup() {
 Serial.begin(9600);  
 pinMode(TrigCap, OUTPUT);
 pinMode(EchoCap, INPUT);

 pinMode(TrigTrash, OUTPUT);
 pinMode(EchoTrash, INPUT);

 myservo.attach(servoPin);
 myservo.write(70);// initial
 delay(40);

 pinMode(buzzPin, OUTPUT);

 
}  

float measure (int Trig, int Echo) {
 digitalWrite(Trig, LOW);
 delayMicroseconds(2);
 digitalWrite(Trig,HIGH);
 delayMicroseconds(10);
 digitalWrite(Trig, LOW);
 float temp = float(pulseIn(Echo, HIGH));
 float distance = (temp * 17 )/1000;
 return distance;
}

void openCap (int hold) {
 myservo.write(110);
   delay(2000);
}

void closeCap () {
 myservo.write(180);
   delay(70);
}

   
void loop() {

 float distCap = measure (TrigCap, EchoCap);
 float distTrash = measure (TrigTrash, EchoTrash);
     
 //Serial.println(distCap);
 //Serial.println(distTrash);

 if (distCap < 20) {
   openCap(1000);
 }
 else {
   closeCap();
 }

 if (distTrash < 5) {
   tone (buzzPin, freqency);
   openCap(1000);
   noTone (buzzPin);
   closeCap ();
 }
 
 delay(100);
}  

3D printing process:

User Testing Day 1:

For our User Testing day, we made a prototype using cardboards, it is a general preview of what will our work be like.

During User Testing day, we received some comments. We decided to led the interface fit to our “lips” concept, and add some sound effects to it.

Modification after User Testing Day:

I went and borrow Micro SD card and speaker, so when our caps open, it will have an “ahhh” sound, and when it closes, it will have some eating sound. Then I started to write the code. Unfortunately, the speaker cannot produce the sound, and the cap won’t open while producing the “ahhh” sound. Here is the code for trying.

int TrigCap = 5; // 开盖子超声波引脚Trig 连接 IO 5
int EchoCap = 6; // 开盖子超声波引脚Echo 连接 IO 6
     
int TrigTrash = 7; // 垃圾高度超声波引脚Trig 连接 IO 7
int EchoTrash = 8; // 垃圾高度声波引脚Echo 连接 IO 8

#include "SD.h"     //读取 SD card 文件
#include "TMRpcm.h"     //读取 音频播放文件
#include "SPI.h"     //读取 SD card的SPI 文件
#define SD_ChipSelectPin 10     //设置SS 引脚 10
TMRpcm music;     //设置音频播放对象 "music"

#include <Servo.h>// 导入舵机模块
Servo myservo;
int servoPin = 2; // 舵机信号线接数字2

//int buzzPin = 9; // 蜂鸣器接数字9
long freqency = 2000; // 鸣叫频率

     
void setup() {
 Serial.begin(9600);  
 pinMode(TrigCap, OUTPUT);
 pinMode(EchoCap, INPUT);

 pinMode(10,OUTPUT);
 digitalWrite(10, HIGH);

 pinMode(TrigTrash, OUTPUT);
 pinMode(EchoTrash, INPUT);
 // 开机时将舵机调整到180度,保证垃圾桶盖是闭合的
 myservo.attach(servoPin);
 myservo.write(180);// initial
 delay(40);
 

 pinMode(buzzPin, OUTPUT);
 digitalWrite(buzzPin, LOW);

 music.speakerPin = 9; //设置音频输出针脚 9
 if (!SD.begin(SD_ChipSelectPin)) {
 Serial.println("SD fail");
 return;

}

 music.setVolume(5);    //   设置音量0 ~7
 
}

// 测量距离
float measure (int Trig, int Echo) {
 digitalWrite(Trig,LOW);
 delayMicroseconds(2);
 digitalWrite(Trig,HIGH);
 delayMicroseconds(10);
 digitalWrite(Trig, LOW);
 float temp = float(pulseIn(Echo, HIGH)); //存储回波等待时间
 float distance = (temp * 17 )/1000;
 return distance;
}

bool is_open;

// 关盖 
void closeCap () {     
   myservo.write(180);
   delay(70);
   //music.play("bite.wav"); 
}

// 开盖
void openCap () {
   Serial.println(1);
   myservo.write(110);
   Serial.println(2);
   //music.play("ahh.wav");
   delay(2000);
   closeCap ();
}



   
void loop() {

 float distCap = measure (TrigCap, EchoCap); // 测量手离垃圾桶的距离
 Serial.println(distCap);
 float distTrash = measure (TrigTrash, EchoTrash); // 测量垃圾桶内垃圾的高度
     
 //Serial.println(distCap);
 //Serial.println(distTrash);

// 如果手离垃圾桶距离小于4cm,开盖。
 if (distCap < 20) {
   openCap();
   delay(2000);
 }

// 当垃圾满的时候,立即开盖,并且报警,延迟1秒后关盖子
//  if (distTrash < 5) {
//    tone (buzzPin, freqency);
//    openCap();
//    noTone (buzzPin);
//    closeCap();
//  }
 
 delay(100);
}

So we modified our project on the basis of the original prototype. We 3d printed the caps and the base for our trashcan, and built the body part using cardboard, and painted acrylic onto it for it to look pretty.

We the modified the code, and decided to give the warning sound more “color”, so I inserted a Mario melody using buzzer.

Below is our final code for presentation

int TrigCap = 5; // 开盖子超声波引脚Trig 连接 IO 5
int EchoCap = 6; // 开盖子超声波引脚Echo 连接 IO 6
     
int TrigTrash = 7; // 垃圾高度超声波引脚Trig 连接 IO 7
int EchoTrash = 8; // 垃圾高度声波引脚Echo 连接 IO 8

#include <Servo.h>// 导入舵机模块
Servo myservo;
int servoPin = 2; // 舵机信号线接数字2

int buzzPin = 9; // 蜂鸣器接数字9
long freqency = 2000; // 鸣叫频率

/*************************************************

 * Public Constants

 *************************************************/

#include <pitches.h>

#define melodyPin 9
//Mario main theme melody
int melody[] = {
 NOTE_E7, NOTE_E7, 0, NOTE_E7,
 0, NOTE_C7, NOTE_E7, 0,
 NOTE_G7, 0, 0,  0,
 NOTE_G6, 0, 0, 0,

 NOTE_C7, 0, 0, NOTE_G6,
 0, 0, NOTE_E6, 0,
 0, NOTE_A6, 0, NOTE_B6,
 0, NOTE_AS6, NOTE_A6, 0,

 NOTE_G6, NOTE_E7, NOTE_G7,
 NOTE_A7, 0, NOTE_F7, NOTE_G7,
 0, NOTE_E7, 0,NOTE_C7,
 NOTE_D7, NOTE_B6, 0, 0,

 NOTE_C7, 0, 0, NOTE_G6,
 0, 0, NOTE_E6, 0,
 0, NOTE_A6, 0, NOTE_B6,
 0, NOTE_AS6, NOTE_A6, 0,

 NOTE_G6, NOTE_E7, NOTE_G7,
 NOTE_A7, 0, NOTE_F7, NOTE_G7,
 0, NOTE_E7, 0,NOTE_C7,
 NOTE_D7, NOTE_B6, 0, 0
};
//Mario main them tempo
int tempo[] = {
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 9, 9, 9,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 9, 9, 9,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
};

//

void buzz(int targetPin, long frequency, long length) {
 long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
 //// 1 second's worth of microseconds, divided by the frequency, then split in half since
 //// there are two phases to each cycle
 long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
 //// multiply frequency, which is really cycles per second, by the number of seconds to 
 //// get the total number of cycles to produce
 for (long i=0; i < numCycles; i++){ // for the calculated length of time...
   digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
   delayMicroseconds(delayValue); // wait for the calculated delay value
   digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
   delayMicroseconds(delayValue); // wait again or the calculated delay value
 }

}
void setup() {
 Serial.begin(9600);  
 pinMode(TrigCap, OUTPUT);
 pinMode(EchoCap, INPUT);

 pinMode(TrigTrash, OUTPUT);
 pinMode(EchoTrash, INPUT);
 // 开机时将舵机调整到70度,保证垃圾桶盖是闭合的
 myservo.attach(servoPin);
 myservo.write(70);// initial
 delay(40);

 pinMode(buzzPin, OUTPUT);

 
}  

// 测量距离
float measure (int Trig, int Echo) {
 digitalWrite(Trig, LOW);
 delayMicroseconds(2);
 digitalWrite(Trig,HIGH);
 delayMicroseconds(10);
 digitalWrite(Trig, LOW);
 float temp = float(pulseIn(Echo, HIGH)); //存储回波等待时间
 float distance = (temp * 17 )/1000;
 return distance;
}

// 开盖
void openCap (int hold) {
 myservo.write(110);
   delay(2000);
   
}
// 关盖
void closeCap () {
 myservo.write(180);
   delay(70);
}

   
void loop() {

 float distCap = measure (TrigCap, EchoCap); // 测量手离垃圾桶的距离
 float distTrash = measure (TrigTrash, EchoTrash); // 测量垃圾桶内垃圾的高度
     
 //Serial.println(distCap);
 //Serial.println(distTrash);

// 如果手离垃圾桶距离小于4cm,开盖。
 if (distCap < 20) {
   openCap(1000);
 }
 else {
   closeCap();
 }

// 当垃圾满的时候,立即开盖,并且报警,延迟1秒后关盖子
 if (distTrash < 5) {
   openCap(1000);
   Serial.println(" 'Mario Theme'");
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000/tempo[thisNote];

      buzz(melodyPin, melody[thisNote],noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(melodyPin,0,noteDuration);

   }
   closeCap ();
 }
 
 delay(100);
}  

Final Presentation:

Our poster and video⬆️

Further possible Modifications:

If I have more time, I would like to figure out how to add sound into our trashcan with out using the big speaker. That would add much more meaning to our project.

 

Leave a Reply

Your email address will not be published. Required fields are marked *