Alberto Li / Mbed 2 deprecated iot_smart_hedgehog_home

Dependencies:   4DGL-uLCD-SE LCD_fonts PinDetect Servo mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 #include "rtos.h"
00004 #include "SDFileSystem.h"
00005 #include "wave_player.h"
00006 RawSerial  pi(USBTX, USBRX);
00007 Mutex serial_mutex;
00008 
00009 Servo myservo(p21);
00010 AnalogIn LM61(p15);
00011 
00012 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
00013 AnalogOut DACout(p18);
00014 //On Board Speaker
00015 //PwmOut PWMout(p25);
00016 wave_player waver(&DACout);
00017 
00018 volatile float temp_out;
00019 volatile float rpm_out;
00020 volatile float wheel_speed_out;
00021 volatile int treat = 0;
00022 volatile int music = 0;
00023 volatile bool x;
00024 
00025 DigitalOut myled1(LED1);
00026 DigitalOut myled2(LED2);
00027 DigitalOut myled3(LED3);
00028 
00029 volatile long int count;
00030 
00031 Serial pc(USBTX, USBRX);
00032 Timer t;
00033 InterruptIn risingEdge(p11);
00034 
00035 void dev_recv()
00036 {   while(1){
00037         char temp = 0;
00038         while(pi.readable()) {
00039             myled3 = !myled3;
00040             serial_mutex.lock();
00041             temp = pi.getc();
00042             if (temp=='t'){
00043                 myled2=1;
00044                 treat=1;
00045             }
00046             if (temp=='m'){
00047                 myled1=1;
00048                 music=1;
00049             }
00050             serial_mutex.unlock();
00051         }
00052     }
00053 }
00054 
00055 void check_temp() {
00056     float tempC, tempF;
00057     while (1) {
00058         //conversion to degrees C - from sensor output voltage per LM61 data sheet
00059         tempC = ((LM61*3.3) - 0.600)*100.0;
00060         //convert to degrees F
00061         tempF = (9.0*tempC) / 5.0 + 32.0;
00062         temp_out = tempF + 16.7;
00063         //print current temp
00064         Thread::wait(500);
00065     }
00066 }
00067 
00068 void deliver_snack()
00069 {   while(1){
00070         if (treat == 1) {
00071             myservo = 1; //closed position
00072             Thread::wait(1000);
00073             myservo = .7; //open position
00074             Thread::wait(200); // open for .2 secs delivers half a small tupperware
00075             myservo = 1;
00076             Thread::wait(500);
00077             treat = 0;
00078             myled2=0;
00079         }
00080         Thread::yield();
00081     }
00082 }
00083 
00084 
00085 
00086 
00087 char* getOut()
00088 {
00089     char output[22];
00090 
00091     snprintf(output, 22, "%3.1f, %4.0f, %1.5f \n", temp_out, rpm_out, wheel_speed_out);
00092     return output;
00093 }
00094 
00095 void send_data() {
00096     while(1)
00097     {
00098         serial_mutex.lock();
00099         pi.puts(getOut());
00100         serial_mutex.unlock();
00101         Thread::wait(5000);
00102         
00103     }
00104 }
00105 
00106 void pulses() {
00107     count++;
00108 }
00109 
00110 void check_wheel() {
00111     double rpm = 0;
00112     double speed = 0;
00113     double circumference = 0.266 * 3.1416; // 26. cm wheel diameter * pi 
00114     risingEdge.rise(&pulses);
00115     long int temp = count;
00116     while (1) {
00117         count = 0;
00118         t.reset();
00119         t.start();
00120         while (t.read_ms() < 2001) {
00121             ;
00122         }
00123         t.stop();
00124         temp=count;
00125         double rev = (double)temp;
00126         double rpm = rev * 30;
00127         double speed = circumference * rev;
00128         rpm_out = (float)rpm;
00129         wheel_speed_out = (float)speed;
00130     }
00131 }
00132 
00133 
00134 int main() {
00135     //printf("Hello, in Main");
00136     Thread t1(check_temp);
00137     Thread t2(send_data);
00138     Thread t3(check_wheel);
00139     Thread t4(dev_recv);
00140     Thread t5(deliver_snack);
00141 
00142     while (1) {
00143         if (music == 1) {
00144             FILE *wave_file = fopen("/sd/wavfiles/crickets.wav", "r");
00145             waver.play(wave_file);
00146             fclose(wave_file);
00147             Thread::wait(1000);
00148             music = 0;
00149             myled2=0;
00150         }
00151     }
00152 }