Hello all
I want to undertake a project whereby garden lights are hidden under flaps in the grass, and when it is raining and/or dark then a solenoid will lift the light into the garden through the flap. I'm trying to build a model of this just now using LEDs and a small solenoid. I'm using a PIC 16F877A microcontroller. ****Please note that a low output (logic o) from this controller corresponds to ON*************
I've attached my preliminary code below. The queries I have are:
1. I want to the ADC to read a value from the moisture sensor and LDR - is the code correct (see the set_adc_channel coding)?.
2. will the code work? Will one type of LED come on when it is quite dark and/or quite wet and the other come on when it is very dark and/or very wet? Will no LEDs come on when it isn't dark and/or wet?
I hope the questions make sense and I hope you can help me?
Thanks
Rob
/****************************************************************************************************
DESCRIPTION OF CODE: lights at pin A5 will come on when it is quite dark and/or quite wet. The lights at pin B4 will come on when it is very dark and/or very wet (and the lights at pin A5 will go off). No lights on when it is not dark enough or wet enough.
A push type solenoid will active only when either of the lights (pin A5 or B4) come on. The solenoid will be deactivated when no lights are on; hence the light will retract back under the ground.
*****************************************************************************************************/
#include <16f877a.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT,PUT
#use delay (clock=20000000)
#define SIDE_LIGHTS PIN_A5 // define the pin allocations
#define FULL_LIGHTS PIN_B4
#define_SOLENOID PIN_E0
#define cutoff1 128 // quite dark (reading the LDR input)
#define cutoff2 192 // very dark (reading the LDR input)
#define cutoff3 128 //quite wet (reading the moisture input)
#define cutoff4 192 //very wet (reading the moisture input)
void light_the_led(int led) // define a function to be read inside main function
{
Output_high(SIDE_LIGHTS); // turn OFF led
Output_high(FULL_LIGHTS); //turn led OFF
Switch(LED)
{
Case 0 : output_low(SIDE_LIGHTS); //turn this light on
Case 1 : output_low(FULL_LIGHTS); //turn this light on
}
}
void main( )
{
int reading;
Setup_adc_ports (RA1_RA2_Analog);
Setup_adc (ADC_CLOCK_INTERNAL); // Setup ADC
Set_adc_channel ( 1_2 ); // channel 1 is LDR, channel 2 is moisture sensor
while(true)
{
reading = read_adc( 1); //read LDR value
if (reading > cutoff1)
light_the_led(0);
output_low(SOLENOID) //activates the push type solenoid
else if (reading > cuttoff2)
light_the_led(1)
output_low(SOLENOID) //activates the push type solenoid
else
output_high(SOLENOID) //switches off the solenoid
reading = read_adc(2); //read moisture sensor value
if (reading > cuttoff3)
light_the_led(0);
output_low(SOLENOID) //activates the push type solenoid
else if (reading > cuttoff4)
light_the_led(1);
output_low(SOLENOID) //activates the push type solenoid
else
output_high(SOLENOID) //switches off the solenoid
}
}
I want to undertake a project whereby garden lights are hidden under flaps in the grass, and when it is raining and/or dark then a solenoid will lift the light into the garden through the flap. I'm trying to build a model of this just now using LEDs and a small solenoid. I'm using a PIC 16F877A microcontroller. ****Please note that a low output (logic o) from this controller corresponds to ON*************
I've attached my preliminary code below. The queries I have are:
1. I want to the ADC to read a value from the moisture sensor and LDR - is the code correct (see the set_adc_channel coding)?.
2. will the code work? Will one type of LED come on when it is quite dark and/or quite wet and the other come on when it is very dark and/or very wet? Will no LEDs come on when it isn't dark and/or wet?
I hope the questions make sense and I hope you can help me?
Thanks
Rob
/****************************************************************************************************
DESCRIPTION OF CODE: lights at pin A5 will come on when it is quite dark and/or quite wet. The lights at pin B4 will come on when it is very dark and/or very wet (and the lights at pin A5 will go off). No lights on when it is not dark enough or wet enough.
A push type solenoid will active only when either of the lights (pin A5 or B4) come on. The solenoid will be deactivated when no lights are on; hence the light will retract back under the ground.
*****************************************************************************************************/
#include <16f877a.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT,PUT
#use delay (clock=20000000)
#define SIDE_LIGHTS PIN_A5 // define the pin allocations
#define FULL_LIGHTS PIN_B4
#define_SOLENOID PIN_E0
#define cutoff1 128 // quite dark (reading the LDR input)
#define cutoff2 192 // very dark (reading the LDR input)
#define cutoff3 128 //quite wet (reading the moisture input)
#define cutoff4 192 //very wet (reading the moisture input)
void light_the_led(int led) // define a function to be read inside main function
{
Output_high(SIDE_LIGHTS); // turn OFF led
Output_high(FULL_LIGHTS); //turn led OFF
Switch(LED)
{
Case 0 : output_low(SIDE_LIGHTS); //turn this light on
Case 1 : output_low(FULL_LIGHTS); //turn this light on
}
}
void main( )
{
int reading;
Setup_adc_ports (RA1_RA2_Analog);
Setup_adc (ADC_CLOCK_INTERNAL); // Setup ADC
Set_adc_channel ( 1_2 ); // channel 1 is LDR, channel 2 is moisture sensor
while(true)
{
reading = read_adc( 1); //read LDR value
if (reading > cutoff1)
light_the_led(0);
output_low(SOLENOID) //activates the push type solenoid
else if (reading > cuttoff2)
light_the_led(1)
output_low(SOLENOID) //activates the push type solenoid
else
output_high(SOLENOID) //switches off the solenoid
reading = read_adc(2); //read moisture sensor value
if (reading > cuttoff3)
light_the_led(0);
output_low(SOLENOID) //activates the push type solenoid
else if (reading > cuttoff4)
light_the_led(1);
output_low(SOLENOID) //activates the push type solenoid
else
output_high(SOLENOID) //switches off the solenoid
}
}