![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The use of the KL-46Z FRDM microcontroller to handle the I/O to detect water moisture and light exposure for a potted plant using a 555 Timer Chip and onboard visible light sensor. This project is incomplete as of 4/26/15
main.cpp@2:435f5545e2b6, 2015-05-04 (annotated)
- Committer:
- bomalley
- Date:
- Mon May 04 21:37:27 2015 +0000
- Revision:
- 2:435f5545e2b6
- Parent:
- 1:5b951fe1f430
Finished timer chip fn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bomalley | 0:1a57cdd09c38 | 1 | #include "mbed.h" |
bomalley | 0:1a57cdd09c38 | 2 | #include "SLCD.h" |
bomalley | 0:1a57cdd09c38 | 3 | |
bomalley | 0:1a57cdd09c38 | 4 | #define PROGNAME "Plant Vital Sign Detector v1 v3\r\n" |
bomalley | 0:1a57cdd09c38 | 5 | #define HIGH 1 |
bomalley | 0:1a57cdd09c38 | 6 | #define LOW 0 |
bomalley | 0:1a57cdd09c38 | 7 | #define PIN_2_555 D2 |
bomalley | 0:1a57cdd09c38 | 8 | #define PIN_3_555 D3 |
bomalley | 0:1a57cdd09c38 | 9 | #define PIN_3_CLONE LED_GREEN |
bomalley | 0:1a57cdd09c38 | 10 | #define LEDON LOW |
bomalley | 0:1a57cdd09c38 | 11 | #define LEDOFF HIGH |
bomalley | 0:1a57cdd09c38 | 12 | #define OUTPULSELEN 100 // ms |
bomalley | 0:1a57cdd09c38 | 13 | |
bomalley | 0:1a57cdd09c38 | 14 | #define DATATIME 100 |
bomalley | 0:1a57cdd09c38 | 15 | #define SERIALSPEED 57600 |
bomalley | 0:1a57cdd09c38 | 16 | #define MAX_OUT 1500 // ms |
bomalley | 0:1a57cdd09c38 | 17 | #define MIN_OUT 500 |
bomalley | 0:1a57cdd09c38 | 18 | #define ABORTTIME 3500 |
bomalley | 0:1a57cdd09c38 | 19 | #define INPUTSTART 1 |
bomalley | 0:1a57cdd09c38 | 20 | #define DATARATE 1000 // ms time between data takes. |
bomalley | 0:1a57cdd09c38 | 21 | #define LCDLEN 10 |
bomalley | 0:1a57cdd09c38 | 22 | |
bomalley | 1:5b951fe1f430 | 23 | #define MAXTIME 2 |
bomalley | 0:1a57cdd09c38 | 24 | #define TIMERSTATE 0 |
bomalley | 0:1a57cdd09c38 | 25 | #define CHECKMOISTURE 1 |
bomalley | 0:1a57cdd09c38 | 26 | #define MOREMOISTURE 2 |
bomalley | 0:1a57cdd09c38 | 27 | #define CHECKLIGHT 3 |
bomalley | 0:1a57cdd09c38 | 28 | #define MORELIGHT 4 |
bomalley | 0:1a57cdd09c38 | 29 | #define LESSLIGHT 5 |
bomalley | 0:1a57cdd09c38 | 30 | |
bomalley | 2:435f5545e2b6 | 31 | #define LIGHTMIN .100 //there is too much light |
bomalley | 2:435f5545e2b6 | 32 | #define LIGHTMAX .900 // there is not enough light |
bomalley | 2:435f5545e2b6 | 33 | |
bomalley | 2:435f5545e2b6 | 34 | #define WATERMAX 1500 //Do not water. There is Moisture |
bomalley | 2:435f5545e2b6 | 35 | #define WATERMIN 3000 //Water, there is no moisture |
bomalley | 2:435f5545e2b6 | 36 | |
bomalley | 0:1a57cdd09c38 | 37 | #define ML "MORE LIGHT" |
bomalley | 0:1a57cdd09c38 | 38 | #define LL "LESS LIGHT" |
bomalley | 0:1a57cdd09c38 | 39 | #define MW "MORE WATER" |
bomalley | 0:1a57cdd09c38 | 40 | |
bomalley | 0:1a57cdd09c38 | 41 | |
bomalley | 0:1a57cdd09c38 | 42 | // this constant won't change: |
bomalley | 0:1a57cdd09c38 | 43 | DigitalOut oneShotIn (PIN_2_555); |
bomalley | 0:1a57cdd09c38 | 44 | // the pin that the pushbutton is attached to |
bomalley | 0:1a57cdd09c38 | 45 | DigitalIn oneShotOut (PIN_3_555); |
bomalley | 0:1a57cdd09c38 | 46 | DigitalOut ledPin(PIN_3_CLONE); // the pin that the LED is attached to |
bomalley | 0:1a57cdd09c38 | 47 | AnalogIn LightSensor(PTE22); // define light sensor |
bomalley | 0:1a57cdd09c38 | 48 | |
bomalley | 0:1a57cdd09c38 | 49 | Serial pc(USBTX, USBRX); |
bomalley | 0:1a57cdd09c38 | 50 | SLCD slcd; |
bomalley | 0:1a57cdd09c38 | 51 | Timer milliTimer; |
bomalley | 0:1a57cdd09c38 | 52 | |
bomalley | 0:1a57cdd09c38 | 53 | int PgmState; |
bomalley | 0:1a57cdd09c38 | 54 | long oneShotLen; |
bomalley | 0:1a57cdd09c38 | 55 | long timeOut = ABORTTIME; |
bomalley | 0:1a57cdd09c38 | 56 | long nextDataTake = DATARATE; |
bomalley | 0:1a57cdd09c38 | 57 | long oneShotBegin; |
bomalley | 0:1a57cdd09c38 | 58 | long oneShotEnd; |
bomalley | 0:1a57cdd09c38 | 59 | // Variables will change: |
bomalley | 0:1a57cdd09c38 | 60 | int buttonPushCounter = 0; // counter for the number of button presses |
bomalley | 0:1a57cdd09c38 | 61 | int buttonState = 0; // current state of the button |
bomalley | 0:1a57cdd09c38 | 62 | int lastButtonState = 0; // previous state of the button |
bomalley | 0:1a57cdd09c38 | 63 | |
bomalley | 0:1a57cdd09c38 | 64 | float lightVal; //value of the light sensor |
bomalley | 0:1a57cdd09c38 | 65 | unsigned short lightWord; |
bomalley | 0:1a57cdd09c38 | 66 | |
bomalley | 0:1a57cdd09c38 | 67 | int timerChip; |
bomalley | 0:1a57cdd09c38 | 68 | char lcdData[LCDLEN]; |
bomalley | 0:1a57cdd09c38 | 69 | |
bomalley | 0:1a57cdd09c38 | 70 | void LCDMess(char *lMess){ |
bomalley | 0:1a57cdd09c38 | 71 | slcd.Home(); |
bomalley | 0:1a57cdd09c38 | 72 | slcd.clear(); |
bomalley | 0:1a57cdd09c38 | 73 | slcd.printf(lMess); |
bomalley | 0:1a57cdd09c38 | 74 | } |
bomalley | 0:1a57cdd09c38 | 75 | void pulseOut (long pulseLen){ |
bomalley | 0:1a57cdd09c38 | 76 | oneShotIn.write(LOW); |
bomalley | 0:1a57cdd09c38 | 77 | wait_us(pulseLen); |
bomalley | 0:1a57cdd09c38 | 78 | oneShotIn.write(HIGH); |
bomalley | 0:1a57cdd09c38 | 79 | } |
bomalley | 0:1a57cdd09c38 | 80 | |
bomalley | 0:1a57cdd09c38 | 81 | void setup() { |
bomalley | 0:1a57cdd09c38 | 82 | |
bomalley | 0:1a57cdd09c38 | 83 | // initialize serial communication: |
bomalley | 0:1a57cdd09c38 | 84 | oneShotIn.write(HIGH); |
bomalley | 0:1a57cdd09c38 | 85 | oneShotOut.mode(PullNone); |
bomalley | 0:1a57cdd09c38 | 86 | milliTimer.start(); |
bomalley | 0:1a57cdd09c38 | 87 | milliTimer.reset(); |
bomalley | 0:1a57cdd09c38 | 88 | } |
bomalley | 0:1a57cdd09c38 | 89 | |
bomalley | 0:1a57cdd09c38 | 90 | void readLightSensor() { |
bomalley | 0:1a57cdd09c38 | 91 | lightVal = LightSensor.read(); |
bomalley | 0:1a57cdd09c38 | 92 | lightWord = LightSensor.read_u16(); |
bomalley | 0:1a57cdd09c38 | 93 | pc.printf("LS => %1.3f %5d \r\n", lightVal, lightWord); |
bomalley | 0:1a57cdd09c38 | 94 | |
bomalley | 0:1a57cdd09c38 | 95 | if(lightVal <= LIGHTMIN) { |
bomalley | 0:1a57cdd09c38 | 96 | PgmState = LESSLIGHT; |
bomalley | 0:1a57cdd09c38 | 97 | } |
bomalley | 0:1a57cdd09c38 | 98 | else if(lightVal >= LIGHTMAX) { |
bomalley | 0:1a57cdd09c38 | 99 | PgmState = MORELIGHT; |
bomalley | 0:1a57cdd09c38 | 100 | } |
bomalley | 0:1a57cdd09c38 | 101 | } |
bomalley | 0:1a57cdd09c38 | 102 | |
bomalley | 0:1a57cdd09c38 | 103 | void readTimerChip() { |
bomalley | 0:1a57cdd09c38 | 104 | timerChip = oneShotOut.read(); |
bomalley | 0:1a57cdd09c38 | 105 | // pc.printf(); |
bomalley | 2:435f5545e2b6 | 106 | /**** |
bomalley | 2:435f5545e2b6 | 107 | #define WATERMAX 1500 //Do not water. There is Moisture |
bomalley | 2:435f5545e2b6 | 108 | #define WATERMIN 3000 //Water, there is no moisture |
bomalley | 2:435f5545e2b6 | 109 | ***/ |
bomalley | 2:435f5545e2b6 | 110 | |
bomalley | 2:435f5545e2b6 | 111 | if(timerChip >= WATERMAX) { |
bomalley | 2:435f5545e2b6 | 112 | //Do not water move to check light |
bomalley | 2:435f5545e2b6 | 113 | PgmState = CHECKLIGHT; |
bomalley | 2:435f5545e2b6 | 114 | } |
bomalley | 2:435f5545e2b6 | 115 | else if(timerChip <= WATERMIN) { |
bomalley | 2:435f5545e2b6 | 116 | //Tell pc to water then move to check light |
bomalley | 2:435f5545e2b6 | 117 | pc.printf(MW); |
bomalley | 2:435f5545e2b6 | 118 | PgmState = CHECKLIGHT; |
bomalley | 2:435f5545e2b6 | 119 | } |
bomalley | 0:1a57cdd09c38 | 120 | } |
bomalley | 0:1a57cdd09c38 | 121 | |
bomalley | 0:1a57cdd09c38 | 122 | int main(){ |
bomalley | 2:435f5545e2b6 | 123 | |
bomalley | 0:1a57cdd09c38 | 124 | pulseOut(OUTPULSELEN); |
bomalley | 1:5b951fe1f430 | 125 | //LCDMess(PROGNAME); |
bomalley | 2:435f5545e2b6 | 126 | setup(); |
bomalley | 0:1a57cdd09c38 | 127 | PgmState = TIMERSTATE; |
bomalley | 1:5b951fe1f430 | 128 | int count = 0; |
bomalley | 0:1a57cdd09c38 | 129 | |
bomalley | 0:1a57cdd09c38 | 130 | while (true) { |
bomalley | 0:1a57cdd09c38 | 131 | switch(PgmState) { |
bomalley | 0:1a57cdd09c38 | 132 | case TIMERSTATE: |
bomalley | 0:1a57cdd09c38 | 133 | //loops until timer => MAXTIME |
bomalley | 0:1a57cdd09c38 | 134 | //Go on to CHECKMOISTURE |
bomalley | 0:1a57cdd09c38 | 135 | if(milliTimer.read_ms() >= MAXTIME) { |
bomalley | 0:1a57cdd09c38 | 136 | milliTimer.reset(); |
bomalley | 2:435f5545e2b6 | 137 | PgmState = CHECKMOISTURE; |
bomalley | 0:1a57cdd09c38 | 138 | } |
bomalley | 0:1a57cdd09c38 | 139 | break; |
bomalley | 0:1a57cdd09c38 | 140 | case CHECKMOISTURE: |
bomalley | 0:1a57cdd09c38 | 141 | //call readTimerChip(); |
bomalley | 0:1a57cdd09c38 | 142 | readTimerChip(); |
bomalley | 0:1a57cdd09c38 | 143 | break; |
bomalley | 0:1a57cdd09c38 | 144 | case MOREMOISTURE: |
bomalley | 0:1a57cdd09c38 | 145 | //Output to PC "NEEDS WATER" |
bomalley | 0:1a57cdd09c38 | 146 | //Change state to CHECKLIGHT |
bomalley | 0:1a57cdd09c38 | 147 | pc.printf(MW); |
bomalley | 0:1a57cdd09c38 | 148 | break; |
bomalley | 0:1a57cdd09c38 | 149 | case CHECKLIGHT: |
bomalley | 0:1a57cdd09c38 | 150 | //call readLightSensor |
bomalley | 0:1a57cdd09c38 | 151 | readLightSensor(); |
bomalley | 0:1a57cdd09c38 | 152 | break; |
bomalley | 0:1a57cdd09c38 | 153 | case MORELIGHT: |
bomalley | 0:1a57cdd09c38 | 154 | //output to PC "MORE LIGHT" |
bomalley | 0:1a57cdd09c38 | 155 | //CHange state to TIMERSTATE |
bomalley | 0:1a57cdd09c38 | 156 | pc.printf(ML); |
bomalley | 0:1a57cdd09c38 | 157 | PgmState = TIMERSTATE; |
bomalley | 0:1a57cdd09c38 | 158 | break; |
bomalley | 0:1a57cdd09c38 | 159 | case LESSLIGHT: |
bomalley | 0:1a57cdd09c38 | 160 | //OP to PC "LESS LIGHT" |
bomalley | 0:1a57cdd09c38 | 161 | //Chane State to TIMERSTATE |
bomalley | 0:1a57cdd09c38 | 162 | pc.printf(LL); |
bomalley | 0:1a57cdd09c38 | 163 | PgmState = TIMERSTATE; |
bomalley | 0:1a57cdd09c38 | 164 | break; |
bomalley | 1:5b951fe1f430 | 165 | |
bomalley | 0:1a57cdd09c38 | 166 | } |
bomalley | 1:5b951fe1f430 | 167 | count++; |
bomalley | 1:5b951fe1f430 | 168 | pc.printf("d",count); |
bomalley | 0:1a57cdd09c38 | 169 | wait(DATATIME); |
bomalley | 0:1a57cdd09c38 | 170 | } |
bomalley | 0:1a57cdd09c38 | 171 | } |