Oscar Yuyan Gao / Mbed 2 deprecated 4180_final_project_github

Dependencies:   mbed Servo NeoMatrix mbed-rtos 4DGL-uLCD-SE PinDetect PololuLedStrip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include "PinDetect.h"
00004 #include "rtos.h"
00005 #include "Servo.h"
00006 //#include "PololuLedStrip.h"
00007 #include "NeoMatrix.h"
00008 #define LED_COUNT 24 // uses a 24-led ring
00009 
00010 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
00011 Serial pc(USBTX, USBRX); // tx, rx
00012 Timeout countdown; // for alarm duration
00013 
00014 // pb
00015 PinDetect button1(p7,PullDown);
00016 PinDetect button2(p8,PullDown);
00017 
00018 // sensors
00019 AnalogIn waterSensor(p20);
00020 AnalogIn moistureSensor(p19);
00021 AnalogIn lightSensor(p18);
00022 
00023 // outputs
00024 PwmOut speaker(p21);
00025 PwmOut led(p22);
00026 PwmOut led1(LED1);
00027 // motors
00028 Servo Shade(p23);
00029 Servo Pipe(p24);
00030 // led panel
00031 NeoArr array(p5, 1);
00032 
00033 // global variables
00034 Mutex myMut;
00035 //volatile int button1_push = 0;
00036 //volatile int button2_push = 0;
00037 volatile float setWaterLevel = 0;
00038 volatile float setMoistLevel = 0.0;
00039 volatile float setLightLevelHigh = 0.5;
00040 volatile float setLightLevelLow = 0.2;
00041 volatile float shadePosition = 0.0;
00042 volatile float pipePosition = 0.0;
00043 
00044 // debug varibles
00045 volatile int counting = 0; // keep track of how many seconds has passed
00046 
00047 // sensor readings
00048 volatile float water = 0.0;
00049 volatile float light = 0.0;
00050 volatile float moist = 0.0;
00051 
00052 // callback functinos for 2 pushbuttons
00053 void Button1_Callback (void){
00054     setLightLevelHigh -= .1;
00055     setLightLevelHigh = setLightLevelHigh>0? setLightLevelHigh:0;
00056 }
00057 void Button2_Callback (void){
00058     setLightLevelHigh += .1;
00059     setLightLevelHigh = setLightLevelHigh<1? setLightLevelHigh:1;
00060 }
00061 
00062 // move the servos, also light an LED
00063 void motors_function(void const *argument){
00064     while(1){
00065         if (light>setLightLevelHigh)
00066         {
00067             array.fillScreen(0,0,0,0);
00068             array.write();
00069             /*
00070             code for lower the shade here
00071             */
00072             pc.printf("case 1\n\r");
00073         }
00074         else if (light<setLightLevelLow)
00075         {
00076             array.fillScreen(0,255,255,255);
00077             array.write();
00078             pc.printf("case 2\n\r");
00079         }
00080         else
00081         {
00082             array.fillScreen(0,0,0,0);
00083             array.write();
00084             Shade = 0;
00085             /*
00086             code for openning the shade here
00087             */
00088             pc.printf("case 3\n\r");
00089 
00090         }
00091 
00092         pipePosition = moist<0.6? 1:0;
00093         Pipe = pipePosition;
00094         Thread::wait(1000);
00095     }
00096 }    
00097 
00098 int main() {
00099     uLCD.printf("\n\rstart printing\n");
00100     uLCD.cls();
00101     speaker.period(1.0/800.0);
00102     
00103     // led init
00104     array.setBrightness(.1);    // set brightness to 0.1
00105     array.clear();
00106 
00107     // attach button callbacks
00108     button1.attach_deasserted(&Button1_Callback);
00109     button1.setSampleFrequency();
00110     button2.attach_deasserted(&Button2_Callback);
00111     button2.setSampleFrequency();
00112 
00113     // attach threads
00114     Thread motors(motors_function);
00115     
00116     while(1) {
00117         water = waterSensor.read();
00118         moist = moistureSensor.read();
00119         light = lightSensor.read();
00120         myMut.lock();
00121         uLCD.locate(0,2);
00122         uLCD.printf("%d:\n\r",counting);
00123         uLCD.printf("water: %f\n\r",water);
00124         uLCD.printf("moist: %f\n\r",moist);
00125         uLCD.printf("light: %f\n\r",light);
00126         
00127         uLCD.locate(0,10);
00128         uLCD.printf("SetLightHigh: %1.1f\n\r", setLightLevelHigh);
00129         uLCD.printf("SetLightLow: %1.1f\n\r", setLightLevelLow);
00130         uLCD.printf("SetMoistLevel: %1.1f\n\r", setMoistLevel);        
00131         myMut.unlock();
00132         counting++;
00133          
00134         // sound the alarm if water level too low
00135         speaker = moist<setMoistLevel? 0.01:0;
00136         Thread::wait(1000); // every one second
00137     }
00138 }