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

Dependencies:   SLCD mbed

Committer:
bomalley
Date:
Mon May 04 21:23:28 2015 +0000
Revision:
1:5b951fe1f430
Parent:
0:1a57cdd09c38
Child:
2:435f5545e2b6
changes made to code

Who changed what in which revision?

UserRevisionLine numberNew 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 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 oneShotIn.write(HIGH);
bomalley 0:1a57cdd09c38 81 oneShotOut.mode(PullNone);
bomalley 0:1a57cdd09c38 82 milliTimer.start();
bomalley 0:1a57cdd09c38 83 milliTimer.reset();
bomalley 0:1a57cdd09c38 84 }
bomalley 0:1a57cdd09c38 85
bomalley 0:1a57cdd09c38 86 void readLightSensor() {
bomalley 0:1a57cdd09c38 87 lightVal = LightSensor.read();
bomalley 0:1a57cdd09c38 88 lightWord = LightSensor.read_u16();
bomalley 0:1a57cdd09c38 89 pc.printf("LS => %1.3f %5d \r\n", lightVal, lightWord);
bomalley 0:1a57cdd09c38 90
bomalley 0:1a57cdd09c38 91 if(lightVal <= LIGHTMIN) {
bomalley 0:1a57cdd09c38 92 PgmState = LESSLIGHT;
bomalley 0:1a57cdd09c38 93 }
bomalley 0:1a57cdd09c38 94 else if(lightVal >= LIGHTMAX) {
bomalley 0:1a57cdd09c38 95 PgmState = MORELIGHT;
bomalley 0:1a57cdd09c38 96 }
bomalley 0:1a57cdd09c38 97 }
bomalley 0:1a57cdd09c38 98
bomalley 0:1a57cdd09c38 99 void readTimerChip() {
bomalley 0:1a57cdd09c38 100 timerChip = oneShotOut.read();
bomalley 0:1a57cdd09c38 101 // pc.printf();
bomalley 0:1a57cdd09c38 102 }
bomalley 0:1a57cdd09c38 103
bomalley 0:1a57cdd09c38 104 int main(){
bomalley 0:1a57cdd09c38 105 setup();
bomalley 0:1a57cdd09c38 106 pulseOut(OUTPULSELEN);
bomalley 1:5b951fe1f430 107 //LCDMess(PROGNAME);
bomalley 0:1a57cdd09c38 108 milliTimer.start();
bomalley 0:1a57cdd09c38 109 milliTimer.reset();
bomalley 0:1a57cdd09c38 110 PgmState = TIMERSTATE;
bomalley 1:5b951fe1f430 111 int count = 0;
bomalley 0:1a57cdd09c38 112
bomalley 0:1a57cdd09c38 113 while (true) {
bomalley 0:1a57cdd09c38 114 switch(PgmState) {
bomalley 0:1a57cdd09c38 115 case TIMERSTATE:
bomalley 0:1a57cdd09c38 116 //loops until timer => MAXTIME
bomalley 0:1a57cdd09c38 117 //Go on to CHECKMOISTURE
bomalley 0:1a57cdd09c38 118 if(milliTimer.read_ms() >= MAXTIME) {
bomalley 0:1a57cdd09c38 119 milliTimer.reset();
bomalley 1:5b951fe1f430 120 PgmState = CHECKLIGHT;
bomalley 0:1a57cdd09c38 121 }
bomalley 0:1a57cdd09c38 122 break;
bomalley 0:1a57cdd09c38 123 case CHECKMOISTURE:
bomalley 0:1a57cdd09c38 124 //call readTimerChip();
bomalley 0:1a57cdd09c38 125 readTimerChip();
bomalley 0:1a57cdd09c38 126 break;
bomalley 0:1a57cdd09c38 127 case MOREMOISTURE:
bomalley 0:1a57cdd09c38 128 //Output to PC "NEEDS WATER"
bomalley 0:1a57cdd09c38 129 //Change state to CHECKLIGHT
bomalley 0:1a57cdd09c38 130 pc.printf(MW);
bomalley 0:1a57cdd09c38 131 break;
bomalley 0:1a57cdd09c38 132 case CHECKLIGHT:
bomalley 0:1a57cdd09c38 133 //call readLightSensor
bomalley 0:1a57cdd09c38 134 readLightSensor();
bomalley 0:1a57cdd09c38 135 break;
bomalley 0:1a57cdd09c38 136 case MORELIGHT:
bomalley 0:1a57cdd09c38 137 //output to PC "MORE LIGHT"
bomalley 0:1a57cdd09c38 138 //CHange state to TIMERSTATE
bomalley 0:1a57cdd09c38 139 pc.printf(ML);
bomalley 0:1a57cdd09c38 140 PgmState = TIMERSTATE;
bomalley 0:1a57cdd09c38 141 break;
bomalley 0:1a57cdd09c38 142 case LESSLIGHT:
bomalley 0:1a57cdd09c38 143 //OP to PC "LESS LIGHT"
bomalley 0:1a57cdd09c38 144 //Chane State to TIMERSTATE
bomalley 0:1a57cdd09c38 145 pc.printf(LL);
bomalley 0:1a57cdd09c38 146 PgmState = TIMERSTATE;
bomalley 0:1a57cdd09c38 147 break;
bomalley 1:5b951fe1f430 148
bomalley 0:1a57cdd09c38 149 }
bomalley 1:5b951fe1f430 150 count++;
bomalley 1:5b951fe1f430 151 pc.printf("d",count);
bomalley 0:1a57cdd09c38 152 wait(DATATIME);
bomalley 0:1a57cdd09c38 153 }
bomalley 0:1a57cdd09c38 154 }