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
Diff: main.cpp
- Revision:
- 0:1a57cdd09c38
- Child:
- 1:5b951fe1f430
diff -r 000000000000 -r 1a57cdd09c38 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Apr 26 23:34:56 2015 +0000 @@ -0,0 +1,156 @@ +#include "mbed.h" +#include "SLCD.h" + +#define PROGNAME "Plant Vital Sign Detector v1 v3\r\n" +#define HIGH 1 +#define LOW 0 +#define PIN_2_555 D2 +#define PIN_3_555 D3 +#define PIN_3_CLONE LED_GREEN +#define LEDON LOW +#define LEDOFF HIGH +#define OUTPULSELEN 100 // ms + +#define DATATIME 100 +#define SERIALSPEED 57600 +#define MAX_OUT 1500 // ms +#define MIN_OUT 500 +#define ABORTTIME 3500 +#define INPUTSTART 1 +#define DATARATE 1000 // ms time between data takes. +#define LCDLEN 10 + +#define MAXTIME 500 +#define TIMERSTATE 0 +#define CHECKMOISTURE 1 +#define MOREMOISTURE 2 +#define CHECKLIGHT 3 +#define MORELIGHT 4 +#define LESSLIGHT 5 + +#define LIGHTMIN .100 +#define LIGHTMAX .900 +#define ML "MORE LIGHT" +#define LL "LESS LIGHT" +#define MW "MORE WATER" + + +// this constant won't change: +DigitalOut oneShotIn (PIN_2_555); +// the pin that the pushbutton is attached to +DigitalIn oneShotOut (PIN_3_555); +DigitalOut ledPin(PIN_3_CLONE); // the pin that the LED is attached to +AnalogIn LightSensor(PTE22); // define light sensor + +Serial pc(USBTX, USBRX); +SLCD slcd; +Timer milliTimer; + +int PgmState; +long oneShotLen; +long timeOut = ABORTTIME; +long nextDataTake = DATARATE; +long oneShotBegin; +long oneShotEnd; +// Variables will change: +int buttonPushCounter = 0; // counter for the number of button presses +int buttonState = 0; // current state of the button +int lastButtonState = 0; // previous state of the button + +float lightVal; //value of the light sensor +unsigned short lightWord; + +int timerChip; +char lcdData[LCDLEN]; + +void LCDMess(char *lMess){ + slcd.Home(); + slcd.clear(); + slcd.printf(lMess); +} +void pulseOut (long pulseLen){ + oneShotIn.write(LOW); + wait_us(pulseLen); + oneShotIn.write(HIGH); +} + +void setup() { + + // initialize serial communication: + + pc.printf(PROGNAME); + oneShotIn.write(HIGH); + oneShotOut.mode(PullNone); + ledPin.write(LOW); + milliTimer.start(); + milliTimer.reset(); + nextDataTake = DATARATE; + lastButtonState = LOW; + oneShotLen = 0; +} + +void readLightSensor() { + lightVal = LightSensor.read(); + lightWord = LightSensor.read_u16(); + pc.printf("LS => %1.3f %5d \r\n", lightVal, lightWord); + + if(lightVal <= LIGHTMIN) { + PgmState = LESSLIGHT; + } + else if(lightVal >= LIGHTMAX) { + PgmState = MORELIGHT; + } +} + +void readTimerChip() { + timerChip = oneShotOut.read(); + // pc.printf(); +} + +int main(){ + setup(); + pulseOut(OUTPULSELEN); + LCDMess(PROGNAME); + milliTimer.start(); + milliTimer.reset(); + PgmState = TIMERSTATE; + + while (true) { + switch(PgmState) { + case TIMERSTATE: + //loops until timer => MAXTIME + //Go on to CHECKMOISTURE + if(milliTimer.read_ms() >= MAXTIME) { + milliTimer.reset(); + PgmState = CHECKMOISTURE; + } + break; + case CHECKMOISTURE: + //call readTimerChip(); + readTimerChip(); + break; + case MOREMOISTURE: + //Output to PC "NEEDS WATER" + //Change state to CHECKLIGHT + pc.printf(MW); + break; + case CHECKLIGHT: + //call readLightSensor + readLightSensor(); + break; + case MORELIGHT: + //output to PC "MORE LIGHT" + //CHange state to TIMERSTATE + pc.printf(ML); + PgmState = TIMERSTATE; + break; + case LESSLIGHT: + //OP to PC "LESS LIGHT" + //Chane State to TIMERSTATE + pc.printf(LL); + PgmState = TIMERSTATE; + break; + } + wait(DATATIME); + } +} \ No newline at end of file