final

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

Revision:
5:61e7a1d16b21
Parent:
4:2a561107cc01
--- a/main.cpp	Mon Nov 23 21:26:55 2020 +0000
+++ b/main.cpp	Wed Dec 02 18:17:19 2020 +0000
@@ -3,9 +3,13 @@
 #include "PinDetect.h"
 #include "rtos.h"
 #include "Servo.h"
+//#include "PololuLedStrip.h"
+#include "NeoMatrix.h"
+#define LED_COUNT 24 // uses a 24-led ring
 
 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
 Serial pc(USBTX, USBRX); // tx, rx
+Timeout countdown; // for alarm duration
 
 // pb
 PinDetect button1(p7,PullDown);
@@ -23,17 +27,21 @@
 // motors
 Servo Shade(p23);
 Servo Pipe(p24);
+// led panel
+NeoArr array(p5, 1);
 
-
+// global variables
 Mutex myMut;
-volatile int button1_push = 0;
-volatile int button2_push = 0;
-volatile float setWaterLevel = 0.5;
-volatile float setMositLevel = 0.5;
-volatile float setLightLevel = 0.5;
+//volatile int button1_push = 0;
+//volatile int button2_push = 0;
+volatile float setWaterLevel = 0;
+volatile float setMoistLevel = 0.0;
+volatile float setLightLevelHigh = 0.5;
+volatile float setLightLevelLow = 0.2;
 volatile float shadePosition = 0.0;
 volatile float pipePosition = 0.0;
 
+// debug varibles
 volatile int counting = 0; // keep track of how many seconds has passed
 
 // sensor readings
@@ -42,47 +50,45 @@
 volatile float moist = 0.0;
 
 // callback functinos for 2 pushbuttons
-void Button1_Callback (void) {button1_push = 1;}
-void Button2_Callback (void) {button2_push = 1;}
-
-// function for the buttons thread: adjust pre-set light levels
-void buttons_function(void const *argument){
-    while(1){
-        if (button1_push && button2_push)
-        {
-            uLCD.cls();
-            uLCD.printf("DON'T PRESS THE BUTTON AT THE SAME TIME!\n\r");
-            button1_push = button2_push = 0;
-            uLCD.cls();
-        }
-        if (button1_push)
-        {
-            setLightLevel -= .1;
-            button1_push = 0;
-        }
-        if (button2_push)
-        {
-            setLightLevel += .1;
-            button2_push = 0;
-        }
-        if (setLightLevel<0) setLightLevel = 0;
-        if (setLightLevel>1) setLightLevel = 1;
-
-        myMut.lock();
-        uLCD.locate(0,10);
-        uLCD.printf("SetLightLevel: %1.1f\n\r", setLightLevel);
-        myMut.unlock();
-        Thread::wait(500);
-    }    
+void Button1_Callback (void){
+    setLightLevelHigh -= .1;
+    setLightLevelHigh = setLightLevelHigh>0? setLightLevelHigh:0;
+}
+void Button2_Callback (void){
+    setLightLevelHigh += .1;
+    setLightLevelHigh = setLightLevelHigh<1? setLightLevelHigh:1;
 }
 
 // move the servos, also light an LED
 void motors_function(void const *argument){
     while(1){
-        shadePosition = 1-light; // raise the shade when light 
-        Shade = shadePosition;
-        led.write(shadePosition);
-        
+        if (light>setLightLevelHigh)
+        {
+            array.fillScreen(0,0,0,0);
+            array.write();
+            /*
+            code for lower the shade here
+            */
+            pc.printf("case 1\n\r");
+        }
+        else if (light<setLightLevelLow)
+        {
+            array.fillScreen(0,255,255,255);
+            array.write();
+            pc.printf("case 2\n\r");
+        }
+        else
+        {
+            array.fillScreen(0,0,0,0);
+            array.write();
+            Shade = 0;
+            /*
+            code for openning the shade here
+            */
+            pc.printf("case 3\n\r");
+
+        }
+
         pipePosition = moist<0.6? 1:0;
         Pipe = pipePosition;
         Thread::wait(1000);
@@ -93,15 +99,20 @@
     uLCD.printf("\n\rstart printing\n");
     uLCD.cls();
     speaker.period(1.0/800.0);
+    
+    // led init
+    array.setBrightness(.1);    // set brightness to 0.1
+    array.clear();
 
+    // attach button callbacks
     button1.attach_deasserted(&Button1_Callback);
     button1.setSampleFrequency();
     button2.attach_deasserted(&Button2_Callback);
     button2.setSampleFrequency();
 
-    Thread buttons(buttons_function);
+    // attach threads
     Thread motors(motors_function);
-
+    
     while(1) {
         water = waterSensor.read();
         moist = moistureSensor.read();
@@ -112,12 +123,16 @@
         uLCD.printf("water: %f\n\r",water);
         uLCD.printf("moist: %f\n\r",moist);
         uLCD.printf("light: %f\n\r",light);
+        
+        uLCD.locate(0,10);
+        uLCD.printf("SetLightHigh: %1.1f\n\r", setLightLevelHigh);
+        uLCD.printf("SetLightLow: %1.1f\n\r", setLightLevelLow);
+        uLCD.printf("SetMoistLevel: %1.1f\n\r", setMoistLevel);        
         myMut.unlock();
         counting++;
          
         // sound the alarm if water level too low
-        speaker = water<setWaterLevel? 0.5:0;
-    
-        Thread::wait(1000);
+        speaker = moist<setMoistLevel? 0.01:0;
+        Thread::wait(1000); // every one second
     }
 }
\ No newline at end of file