- #1
axe34
- 38
- 0
Homework Statement
I'm using a PIC 16F877A microcontroller. An LDR and moisture sensor connect to the ADC. The main body of the code is ''void main'', and there is also a sub-code called "void light_the_led'' which is read inside the 'void main code'.
I cannot get access easily to hardware or software to test this code below. Does it look ok? Particularly is the ADC code set-up correctly?
Homework Equations
see code below
3. The Attempt at a Solution
see code below. NOTE: this microcontroller gives logic 0 when output is high, and 1 when low!
Thanks. I wish I had access to someone who could try this on a microcontroller (and use an led instead of solenoid).
Code:
/*****************************************************************************************************
Description: 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.
**********************************************************************************************************/#include <16f877a.h> // compiler directives
#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); // set up ADC
Setup_adc (ADC_CLOCK_INTERNAL); // Setup ADC
while(true)
{
Set_adc_channel (1 );
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 solenoidSet_adc_channel ( 2 );
reading = read_adc(2); //read moisture sensor value
if (reading > cuttoff3)
light_the_led(0);
{ Set_adc_channel (1)
if (reading > cuttoff2)
light_the_led(1); }
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
}
}
Last edited by a moderator: