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@0:1a57cdd09c38, 2015-04-26 (annotated)
- Committer:
- bomalley
- Date:
- Sun Apr 26 23:34:56 2015 +0000
- Revision:
- 0:1a57cdd09c38
- Child:
- 1:5b951fe1f430
INCOMPLETE
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 | 0:1a57cdd09c38 | 23 | #define MAXTIME 500 |
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 | 0:1a57cdd09c38 | 31 | #define LIGHTMIN .100 |
bomalley | 0:1a57cdd09c38 | 32 | #define LIGHTMAX .900 |
bomalley | 0:1a57cdd09c38 | 33 | #define ML "MORE LIGHT" |
bomalley | 0:1a57cdd09c38 | 34 | #define LL "LESS LIGHT" |
bomalley | 0:1a57cdd09c38 | 35 | #define MW "MORE WATER" |
bomalley | 0:1a57cdd09c38 | 36 | |
bomalley | 0:1a57cdd09c38 | 37 | |
bomalley | 0:1a57cdd09c38 | 38 | // this constant won't change: |
bomalley | 0:1a57cdd09c38 | 39 | DigitalOut oneShotIn (PIN_2_555); |
bomalley | 0:1a57cdd09c38 | 40 | // the pin that the pushbutton is attached to |
bomalley | 0:1a57cdd09c38 | 41 | DigitalIn oneShotOut (PIN_3_555); |
bomalley | 0:1a57cdd09c38 | 42 | DigitalOut ledPin(PIN_3_CLONE); // the pin that the LED is attached to |
bomalley | 0:1a57cdd09c38 | 43 | AnalogIn LightSensor(PTE22); // define light sensor |
bomalley | 0:1a57cdd09c38 | 44 | |
bomalley | 0:1a57cdd09c38 | 45 | Serial pc(USBTX, USBRX); |
bomalley | 0:1a57cdd09c38 | 46 | SLCD slcd; |
bomalley | 0:1a57cdd09c38 | 47 | Timer milliTimer; |
bomalley | 0:1a57cdd09c38 | 48 | |
bomalley | 0:1a57cdd09c38 | 49 | int PgmState; |
bomalley | 0:1a57cdd09c38 | 50 | long oneShotLen; |
bomalley | 0:1a57cdd09c38 | 51 | long timeOut = ABORTTIME; |
bomalley | 0:1a57cdd09c38 | 52 | long nextDataTake = DATARATE; |
bomalley | 0:1a57cdd09c38 | 53 | long oneShotBegin; |
bomalley | 0:1a57cdd09c38 | 54 | long oneShotEnd; |
bomalley | 0:1a57cdd09c38 | 55 | // Variables will change: |
bomalley | 0:1a57cdd09c38 | 56 | int buttonPushCounter = 0; // counter for the number of button presses |
bomalley | 0:1a57cdd09c38 | 57 | int buttonState = 0; // current state of the button |
bomalley | 0:1a57cdd09c38 | 58 | int lastButtonState = 0; // previous state of the button |
bomalley | 0:1a57cdd09c38 | 59 | |
bomalley | 0:1a57cdd09c38 | 60 | float lightVal; //value of the light sensor |
bomalley | 0:1a57cdd09c38 | 61 | unsigned short lightWord; |
bomalley | 0:1a57cdd09c38 | 62 | |
bomalley | 0:1a57cdd09c38 | 63 | int timerChip; |
bomalley | 0:1a57cdd09c38 | 64 | char lcdData[LCDLEN]; |
bomalley | 0:1a57cdd09c38 | 65 | |
bomalley | 0:1a57cdd09c38 | 66 | void LCDMess(char *lMess){ |
bomalley | 0:1a57cdd09c38 | 67 | slcd.Home(); |
bomalley | 0:1a57cdd09c38 | 68 | slcd.clear(); |
bomalley | 0:1a57cdd09c38 | 69 | slcd.printf(lMess); |
bomalley | 0:1a57cdd09c38 | 70 | } |
bomalley | 0:1a57cdd09c38 | 71 | void pulseOut (long pulseLen){ |
bomalley | 0:1a57cdd09c38 | 72 | oneShotIn.write(LOW); |
bomalley | 0:1a57cdd09c38 | 73 | wait_us(pulseLen); |
bomalley | 0:1a57cdd09c38 | 74 | oneShotIn.write(HIGH); |
bomalley | 0:1a57cdd09c38 | 75 | } |
bomalley | 0:1a57cdd09c38 | 76 | |
bomalley | 0:1a57cdd09c38 | 77 | void setup() { |
bomalley | 0:1a57cdd09c38 | 78 | |
bomalley | 0:1a57cdd09c38 | 79 | // initialize serial communication: |
bomalley | 0:1a57cdd09c38 | 80 | |
bomalley | 0:1a57cdd09c38 | 81 | pc.printf(PROGNAME); |
bomalley | 0:1a57cdd09c38 | 82 | oneShotIn.write(HIGH); |
bomalley | 0:1a57cdd09c38 | 83 | oneShotOut.mode(PullNone); |
bomalley | 0:1a57cdd09c38 | 84 | ledPin.write(LOW); |
bomalley | 0:1a57cdd09c38 | 85 | milliTimer.start(); |
bomalley | 0:1a57cdd09c38 | 86 | milliTimer.reset(); |
bomalley | 0:1a57cdd09c38 | 87 | nextDataTake = DATARATE; |
bomalley | 0:1a57cdd09c38 | 88 | lastButtonState = LOW; |
bomalley | 0:1a57cdd09c38 | 89 | oneShotLen = 0; |
bomalley | 0:1a57cdd09c38 | 90 | } |
bomalley | 0:1a57cdd09c38 | 91 | |
bomalley | 0:1a57cdd09c38 | 92 | void readLightSensor() { |
bomalley | 0:1a57cdd09c38 | 93 | lightVal = LightSensor.read(); |
bomalley | 0:1a57cdd09c38 | 94 | lightWord = LightSensor.read_u16(); |
bomalley | 0:1a57cdd09c38 | 95 | pc.printf("LS => %1.3f %5d \r\n", lightVal, lightWord); |
bomalley | 0:1a57cdd09c38 | 96 | |
bomalley | 0:1a57cdd09c38 | 97 | if(lightVal <= LIGHTMIN) { |
bomalley | 0:1a57cdd09c38 | 98 | PgmState = LESSLIGHT; |
bomalley | 0:1a57cdd09c38 | 99 | } |
bomalley | 0:1a57cdd09c38 | 100 | else if(lightVal >= LIGHTMAX) { |
bomalley | 0:1a57cdd09c38 | 101 | PgmState = MORELIGHT; |
bomalley | 0:1a57cdd09c38 | 102 | } |
bomalley | 0:1a57cdd09c38 | 103 | } |
bomalley | 0:1a57cdd09c38 | 104 | |
bomalley | 0:1a57cdd09c38 | 105 | void readTimerChip() { |
bomalley | 0:1a57cdd09c38 | 106 | timerChip = oneShotOut.read(); |
bomalley | 0:1a57cdd09c38 | 107 | // pc.printf(); |
bomalley | 0:1a57cdd09c38 | 108 | } |
bomalley | 0:1a57cdd09c38 | 109 | |
bomalley | 0:1a57cdd09c38 | 110 | int main(){ |
bomalley | 0:1a57cdd09c38 | 111 | setup(); |
bomalley | 0:1a57cdd09c38 | 112 | pulseOut(OUTPULSELEN); |
bomalley | 0:1a57cdd09c38 | 113 | LCDMess(PROGNAME); |
bomalley | 0:1a57cdd09c38 | 114 | milliTimer.start(); |
bomalley | 0:1a57cdd09c38 | 115 | milliTimer.reset(); |
bomalley | 0:1a57cdd09c38 | 116 | PgmState = TIMERSTATE; |
bomalley | 0:1a57cdd09c38 | 117 | |
bomalley | 0:1a57cdd09c38 | 118 | while (true) { |
bomalley | 0:1a57cdd09c38 | 119 | switch(PgmState) { |
bomalley | 0:1a57cdd09c38 | 120 | case TIMERSTATE: |
bomalley | 0:1a57cdd09c38 | 121 | //loops until timer => MAXTIME |
bomalley | 0:1a57cdd09c38 | 122 | //Go on to CHECKMOISTURE |
bomalley | 0:1a57cdd09c38 | 123 | if(milliTimer.read_ms() >= MAXTIME) { |
bomalley | 0:1a57cdd09c38 | 124 | milliTimer.reset(); |
bomalley | 0:1a57cdd09c38 | 125 | PgmState = CHECKMOISTURE; |
bomalley | 0:1a57cdd09c38 | 126 | } |
bomalley | 0:1a57cdd09c38 | 127 | break; |
bomalley | 0:1a57cdd09c38 | 128 | case CHECKMOISTURE: |
bomalley | 0:1a57cdd09c38 | 129 | //call readTimerChip(); |
bomalley | 0:1a57cdd09c38 | 130 | readTimerChip(); |
bomalley | 0:1a57cdd09c38 | 131 | break; |
bomalley | 0:1a57cdd09c38 | 132 | case MOREMOISTURE: |
bomalley | 0:1a57cdd09c38 | 133 | //Output to PC "NEEDS WATER" |
bomalley | 0:1a57cdd09c38 | 134 | //Change state to CHECKLIGHT |
bomalley | 0:1a57cdd09c38 | 135 | pc.printf(MW); |
bomalley | 0:1a57cdd09c38 | 136 | break; |
bomalley | 0:1a57cdd09c38 | 137 | case CHECKLIGHT: |
bomalley | 0:1a57cdd09c38 | 138 | //call readLightSensor |
bomalley | 0:1a57cdd09c38 | 139 | readLightSensor(); |
bomalley | 0:1a57cdd09c38 | 140 | break; |
bomalley | 0:1a57cdd09c38 | 141 | case MORELIGHT: |
bomalley | 0:1a57cdd09c38 | 142 | //output to PC "MORE LIGHT" |
bomalley | 0:1a57cdd09c38 | 143 | //CHange state to TIMERSTATE |
bomalley | 0:1a57cdd09c38 | 144 | pc.printf(ML); |
bomalley | 0:1a57cdd09c38 | 145 | PgmState = TIMERSTATE; |
bomalley | 0:1a57cdd09c38 | 146 | break; |
bomalley | 0:1a57cdd09c38 | 147 | case LESSLIGHT: |
bomalley | 0:1a57cdd09c38 | 148 | //OP to PC "LESS LIGHT" |
bomalley | 0:1a57cdd09c38 | 149 | //Chane State to TIMERSTATE |
bomalley | 0:1a57cdd09c38 | 150 | pc.printf(LL); |
bomalley | 0:1a57cdd09c38 | 151 | PgmState = TIMERSTATE; |
bomalley | 0:1a57cdd09c38 | 152 | break; |
bomalley | 0:1a57cdd09c38 | 153 | } |
bomalley | 0:1a57cdd09c38 | 154 | wait(DATATIME); |
bomalley | 0:1a57cdd09c38 | 155 | } |
bomalley | 0:1a57cdd09c38 | 156 | } |