final

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

Committer:
oscargao
Date:
Mon Nov 23 21:23:07 2020 +0000
Revision:
2:95f72e92b370
Parent:
1:5ceaf94b79e4
Child:
3:89e7deacb0c1
most things working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oscargao 0:941787ae3c86 1 #include "mbed.h"
oscargao 1:5ceaf94b79e4 2 #include "uLCD_4DGL.h"
oscargao 1:5ceaf94b79e4 3 #include "PinDetect.h"
oscargao 2:95f72e92b370 4 #include "rtos.h"
oscargao 2:95f72e92b370 5 #include "Servo.h"
oscargao 1:5ceaf94b79e4 6
oscargao 1:5ceaf94b79e4 7 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
oscargao 2:95f72e92b370 8 Serial pc(USBTX, USBRX); // tx, rx
oscargao 1:5ceaf94b79e4 9
oscargao 2:95f72e92b370 10 // pb
oscargao 2:95f72e92b370 11 PinDetect button1(p7,PullDown);
oscargao 2:95f72e92b370 12 PinDetect button2(p8,PullDown);
oscargao 2:95f72e92b370 13
oscargao 2:95f72e92b370 14 // sensors
oscargao 0:941787ae3c86 15 AnalogIn waterSensor(p20);
oscargao 0:941787ae3c86 16 AnalogIn moistureSensor(p19);
oscargao 0:941787ae3c86 17 AnalogIn lightSensor(p18);
oscargao 2:95f72e92b370 18
oscargao 2:95f72e92b370 19 // outputs
oscargao 0:941787ae3c86 20 PwmOut speaker(p21);
oscargao 0:941787ae3c86 21 PwmOut led(p22);
oscargao 2:95f72e92b370 22 PwmOut led1(LED1);
oscargao 2:95f72e92b370 23 // motors
oscargao 2:95f72e92b370 24 Servo Shade(p23);
oscargao 2:95f72e92b370 25 Servo Pipe(p24);
oscargao 2:95f72e92b370 26
oscargao 0:941787ae3c86 27
oscargao 2:95f72e92b370 28 Mutex myMut;
oscargao 2:95f72e92b370 29 volatile int button1_push = 0;
oscargao 2:95f72e92b370 30 volatile int button2_push = 0;
oscargao 2:95f72e92b370 31 volatile float setWaterLevel = 0.5;
oscargao 2:95f72e92b370 32 volatile float setMositLevel = 0.5;
oscargao 2:95f72e92b370 33 volatile float setLightLevel = 0.5;
oscargao 2:95f72e92b370 34 volatile float ShadePosition = 0.0;
oscargao 2:95f72e92b370 35 volatile float ShadePosition = 0.0;
oscargao 2:95f72e92b370 36
oscargao 2:95f72e92b370 37 volatile int counting = 0; // keep track of how many seconds has passed
oscargao 2:95f72e92b370 38
oscargao 2:95f72e92b370 39 // sensor readings
oscargao 2:95f72e92b370 40 volatile float water = 0.0;
oscargao 2:95f72e92b370 41 volatile float light = 0.0;
oscargao 2:95f72e92b370 42 volatile float moist = 0.0;
oscargao 2:95f72e92b370 43
oscargao 2:95f72e92b370 44 // callback functinos for 2 pushbuttons
oscargao 2:95f72e92b370 45 void Button1_Callback (void) {button1_push = 1;}
oscargao 2:95f72e92b370 46 void Button2_Callback (void) {button2_push = 1;}
oscargao 1:5ceaf94b79e4 47
oscargao 2:95f72e92b370 48 // function for the buttons thread: adjust pre-set light levels
oscargao 2:95f72e92b370 49 void buttons_function(void const *argument){
oscargao 2:95f72e92b370 50 while(1){
oscargao 2:95f72e92b370 51 if (button1_push && button2_push)
oscargao 2:95f72e92b370 52 {
oscargao 2:95f72e92b370 53 uLCD.cls();
oscargao 2:95f72e92b370 54 uLCD.printf("DON'T PRESS THE BUTTON AT THE SAME TIME!\n\r");
oscargao 2:95f72e92b370 55 button1_push = button2_push = 0;
oscargao 2:95f72e92b370 56 uLCD.cls();
oscargao 2:95f72e92b370 57 }
oscargao 2:95f72e92b370 58 if (button1_push)
oscargao 2:95f72e92b370 59 {
oscargao 2:95f72e92b370 60 setLightLevel -= .1;
oscargao 2:95f72e92b370 61 button1_push = 0;
oscargao 2:95f72e92b370 62 }
oscargao 2:95f72e92b370 63 if (button2_push)
oscargao 2:95f72e92b370 64 {
oscargao 2:95f72e92b370 65 setLightLevel += .1;
oscargao 2:95f72e92b370 66 button2_push = 0;
oscargao 2:95f72e92b370 67 }
oscargao 2:95f72e92b370 68 if (setLightLevel<0) setLightLevel = 0;
oscargao 2:95f72e92b370 69 if (setLightLevel>1) setLightLevel = 1;
oscargao 2:95f72e92b370 70
oscargao 2:95f72e92b370 71 myMut.lock();
oscargao 2:95f72e92b370 72 uLCD.locate(0,10);
oscargao 2:95f72e92b370 73 uLCD.printf("SetLightLevel: %1.1f\n\r", setLightLevel);
oscargao 2:95f72e92b370 74 myMut.unlock();
oscargao 2:95f72e92b370 75 Thread::wait(500);
oscargao 2:95f72e92b370 76 }
oscargao 1:5ceaf94b79e4 77 }
oscargao 1:5ceaf94b79e4 78
oscargao 2:95f72e92b370 79 // move the servos, also light an LED
oscargao 2:95f72e92b370 80 void motors_function(void const *argument){
oscargao 2:95f72e92b370 81 while(1){
oscargao 2:95f72e92b370 82 shadePosition = 1-light; // raise the shade when light
oscargao 2:95f72e92b370 83 Shade = shadePosition);
oscargao 2:95f72e92b370 84 led.write(shadePosition)
oscargao 2:95f72e92b370 85
oscargao 2:95f72e92b370 86 pipePosition = soil<0.6? 1:0;
oscargao 2:95f72e92b370 87 pipe = pipePosition;
oscargao 2:95f72e92b370 88 Thread::wait(1000);
oscargao 2:95f72e92b370 89 }
oscargao 2:95f72e92b370 90 }
oscargao 2:95f72e92b370 91
oscargao 0:941787ae3c86 92 int main() {
oscargao 1:5ceaf94b79e4 93 uLCD.printf("\n\rstart printing\n");
oscargao 2:95f72e92b370 94 uLCD.cls();
oscargao 2:95f72e92b370 95 speaker.period(1.0/800.0);
oscargao 2:95f72e92b370 96
oscargao 2:95f72e92b370 97 button1.attach_deasserted(&Button1_Callback);
oscargao 2:95f72e92b370 98 button1.setSampleFrequency();
oscargao 2:95f72e92b370 99 button2.attach_deasserted(&Button2_Callback);
oscargao 2:95f72e92b370 100 button2.setSampleFrequency();
oscargao 2:95f72e92b370 101
oscargao 2:95f72e92b370 102 Thread buttons(buttons_function);
oscargao 2:95f72e92b370 103 Thread motors(motors_function);
oscargao 0:941787ae3c86 104
oscargao 0:941787ae3c86 105 while(1) {
oscargao 0:941787ae3c86 106 water = waterSensor.read();
oscargao 0:941787ae3c86 107 moist = moistureSensor.read();
oscargao 0:941787ae3c86 108 light = lightSensor.read();
oscargao 2:95f72e92b370 109 myMut.lock();
oscargao 2:95f72e92b370 110 uLCD.locate(0,2);
oscargao 1:5ceaf94b79e4 111 uLCD.printf("%d:\n\r",counting);
oscargao 2:95f72e92b370 112 uLCD.printf("water: %f\n\r",water);
oscargao 2:95f72e92b370 113 uLCD.printf("moist: %f\n\r",moist);
oscargao 2:95f72e92b370 114 uLCD.printf("light: %f\n\r",light);
oscargao 2:95f72e92b370 115 myMut.unlock();
oscargao 0:941787ae3c86 116 counting++;
oscargao 2:95f72e92b370 117
oscargao 2:95f72e92b370 118 // sound the alarm if water level too low
oscargao 2:95f72e92b370 119 speaker = water<setWaterLevel? 0.5:0;
oscargao 2:95f72e92b370 120
oscargao 2:95f72e92b370 121 Thread::wait(1000);
oscargao 0:941787ae3c86 122 }
oscargao 2:95f72e92b370 123 }