smart dustbin using hcsr04
Dependencies: HCSR04 TextLCD mbed-rtos mbed
main.cpp
- Committer:
- kumarlalit
- Date:
- 2017-04-19
- Revision:
- 0:4273965739dc
File content as of revision 0:4273965739dc:
#include "hcsr04.h" #include "TextLCD.h" #include "rtos.h" #include <string> DigitalOut myled(LED1); DigitalOut myled1(LED2); DigitalOut myled2(LED3); Serial pc(USBTX, USBRX); //TextLCD lcd(p21,p22,p23,p24,p25,p26); HCSR04 usensor(p15,p16); HCSR04 usensor1(p21,p22); HCSR04 usensor2(p23,p24); Serial SIM900(p13, p14); //tx, rx SIM 900 string result; char x; Thread thread; unsigned int dist; unsigned int dist1; unsigned int dist2; void clearString() { result.clear(); } void callback_rx() { while (SIM900.readable()) { x = SIM900.getc(); result += x; pc.putc(x); // print the answer from SIM900 } } void sendSMS1() { clearString(); SIM900.printf("AT+CMGF=1\r\n"); //at command for send sms wait_ms(1000); clearString(); wait_ms(1000); SIM900.printf("AT+CMGS="); SIM900.putc('"'); SIM900.printf("+919405204727"); SIM900.putc('"'); SIM900.printf("\r"); SIM900.printf("\n"); wait_ms(1000); SIM900.printf("Bin 1 is full"); wait_ms(1000); SIM900.putc(0x1A); wait_ms(5000); } void sendSMS2() { clearString(); SIM900.printf("AT+CMGF=1\r\n"); //at command for send sms wait_ms(1000); clearString(); wait_ms(1000); SIM900.printf("AT+CMGS="); SIM900.putc('"'); SIM900.printf("+919405204727"); SIM900.putc('"'); SIM900.printf("\r"); SIM900.printf("\n"); wait_ms(1000); SIM900.printf("Bin 2 is full"); wait_ms(1000); SIM900.putc(0x1A); wait_ms(5000); } void sendSMS3() { clearString(); SIM900.printf("AT+CMGF=1\r\n"); //at command for send sms wait_ms(1000); clearString(); wait_ms(1000); SIM900.printf("AT+CMGS="); SIM900.putc('"'); SIM900.printf("+919405204727"); SIM900.putc('"'); SIM900.printf("\r"); SIM900.printf("\n"); wait_ms(1000); SIM900.printf("Bin 3 is full"); wait_ms(1000); SIM900.putc(0x1A); wait_ms(5000); } void led1_thread() { while (true) { wait_ms(10); sendSMS1(); // SEND SMS wait_ms(10); Thread::wait(1000); } } void led2_thread() { while (true) { wait_ms(10); sendSMS2(); // SEND SMS wait_ms(100); Thread::wait(1000); } } void led3_thread() { while (true) { wait_ms(10); sendSMS3(); // SEND SMS wait_ms(100); Thread::wait(1000); } } int main() { SIM900.attach(&callback_rx); SIM900.baud(9600); // wait_ms(100); while(1) { usensor.start(); usensor1.start(); usensor2.start(); wait_ms(500); dist=usensor.get_dist_cm(); dist1=usensor1.get_dist_cm(); dist2=usensor2.get_dist_cm(); // lcd.locate(0,0); // lcd.printf("distance"); if(dist<20) { myled=1; thread.start(led1_thread); // lcd.locate(0,0); // lcd.printf("dist=cm:%ld\r\n",dist ); } else { myled=0; // lcd.printf("abnormal"); } if(dist1<20) { myled1=1; thread.start(led2_thread); //lcd.locate(0,0); //lcd.printf("dist=cm:%ld\r\n",dist ); } else { myled1=0; } if(dist2<20) { myled2=1; thread.start(led3_thread); //lcd.locate(0,0); //lcd.printf("dist=cm:%ld\r\n",dist ); } else { myled2=0; //lcd.printf("abnormal"); } } }