Arizona Chris
Joined: 20 Dec 2014 Posts: 69 Location: Arizona
|
An automated LED Plant Light using the PIC12F1840 |
Posted: Sun Nov 13, 2016 12:54 pm |
|
|
My most recent project uses this wonderful 8 pin PIC that does nearly anything. If you havent looked at this device, then spend some time and check it out.
This project essentially is a bright red/blue LED lamp that turns on for 4 hours at dusk, then delays 14 hours, then looks again for night again. It is a 3 state Finite State Machine that uses a phototransistor to detect the room brightness, and detect either day or night. So far, it works great, and I hope this inspires you not only to try some sort of day/night photocell project yourself, but to consider the possibilities of this awesome PIC.
Ive posted a web page on the project here:
http://www.schursastrophotography.com/PICprojects/Plantlite.html
This explains the design, schematics, layout, photos of the project and lists the code.
Here is the CCS-C code directly:
Code: |
Code for this project using CCS-C into the PIC12F1840 processor:
//****************************************************************************
//Chris Schur
//(Plant Light): 12F1840
//Date: 10/28/16
//****************************************************************************
/*Description: This program will light the high intensity plant light when it
is night time for four hours or so then turn off looking for day again.
*/
//I/O Designations ---------------------------------------------------
// A0 (RA0): Analog input - Photocell
// A1 (RA1): Output - Day/Night Search LED
// A2 (RA2): Output - Day/Night LED
// A3 (RA3): X (can ONLY be input)
// A4 (RA4): Output - Post Delay LED
// A5 (RA5): Output - Lamp ON Array
//--------------------------------------------------------------------
//Include Files:
#include <12F1840.h> //Normally chip, math, etc. used is here.
//Directives and Defines:
#device ADC=10
#fuses NOPROTECT,NOMCLR
#use delay(internal=4000000) //Use internal clock, no Xtal
#use fast_io(A)
//****************************************************************************
//Global Variables:
//****************************************************************************
int16 result; //0 - 1023 out of ADC
int8 fsm; //finite state
int16 n; //time counting var.
//****************************************************************************
//-- Main Program
//****************************************************************************
void main(void) {
// Set TRIS I/O directions, define analog inputs, compartors:
//(analog inputs digital by default)
set_tris_A(0b001001);
//(analog inputs digital by default)
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
//Initialize variables and Outputs: --------------------------------------
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A4);
output_low(PIN_A5);
fsm = 0; //initial state upon turn on
//----------------------------------------------------------------
delay_ms(2000);
//MAIN LOOP:
while(true) {
//FSM for operation
switch (fsm) {
case 0: //LIGHT MEASURE STATE
//Here we measure the photocell, if its day we stay here, if its night
//we jump to state 1.
output_low(PIN_A4); //turn off post delay lite
//First we read the photocell:
result = read_adc(); //take analog reading. 5v=day, 0v=nite
if (result >= 10) { //daytime
output_high(PIN_A2);
output_high(PIN_A1);
delay_ms(1000); //take readings periodically lookin for night
output_low(PIN_A1); //blink night search LED
delay_ms(500);
fsm = 0; } //stay here until night, keep looking.
if (result < 10) { //nighttime
output_low(PIN_A2);
fsm = 1; } //jump state, its night now.
break;
case 1: //LIGHT ON STATE
//Simple - turn on the big lamps for 4 hours, then jump to state 2
output_low(PIN_A1); //no longer searching for night
output_high(PIN_A5); //turn on plant light
//now 4 hour delay:
//delay_ms(3000); //short test delay of 3s
//for(n=1; n<2; n++) { //1 mins test delay
for(n=1; n<240; n++) { //240 mins For 4H delay
delay_ms(60000); } //one minute delay = 60,000 mS.
//turn off plant lamp:
output_low(Pin_A5);
fsm = 2; //jump state to post light long delay...
break;
case 2: //LONG DELAY STATE
//Here we delay after lamps turn off until it will be day time (~14h)
//then jump back to state 1.
output_high(PIN_A4); //post delay lamp
//14 hour delay now:
//delay_ms(5000); //5s test delay
//for(n=1; n<2; n++) { //1 mins test delay
for(n=1; n<840; n++) { //840 mins For 14H delay
delay_ms(60000); } //one minute delay = 60,000 mS.
//ok, its daytime now, youve waited long enough
fsm = 0; //done, jump back to light measure state look for night.
break;
} //switch
} //while
} //main
|
|
|