Solenoid controlled lights - please help with C code

Discussion in 'The Projects Forum' started by Rob35099, Sep 22, 2015.

  1. Rob35099

    Thread Starter New Member

    Sep 22, 2015
    6
    0
    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





    }

    }
     
  2. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,809
    834
    You may find it difficult to get the distance you need with a solenoid. How much throw does your design require?
     
  3. Rob35099

    Thread Starter New Member

    Sep 22, 2015
    6
    0
    For now, I'm doing a scale model. I'll need a motor in the future.

    Any ideas regarding the code - will it work ok for a solenoid?
     
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,809
    834
    Looks like a typo in the #define for SOLENOID.

    Your nested if statements are not clear. I expected to see more end of statement ;s and code blocks {}. Careful layout and formatting of the code helps reduce errors.

    Otherwise, a quick look at the code isn't possible because there is so much missing. I didn't do a mental simulation. What's important is your schematic connecting the pin to the solenoid. A micro pin cannot drive even one solenoid alone and you have to account for back EMF.

    Similar concerns (without the back EMF) occur for the LED. How much current will they draw?

    Do you have a schematic?
     
    Last edited: Sep 22, 2015
  5. blocco a spirale

    AAC Fanatic!

    Jun 18, 2008
    1,438
    368
    If you're not sure, rather than asking the question "will my code work" the best approach is to write it in sections then test each section on completion. You are going to have to write and debug it anyway so you should get familiar with the process.
     
  6. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,809
    834
    I have to make a clear statement that your code will NOT do as you expect.

    After spending some time with the snippet you provided (and modifying it so I could compile it without all the functions referenced), your structure is badly formed. The if statements will not execute in the way you expect.

    Does your code editor have a tool to auto format your code. This will indent it based on what it sees are code blocks. You will note that some of your if statements do not do what you think and many of the else if statements are incorrect.

    Get familiar with what a code block is, and use braces to mark each one explicitly. Also, you need to use many more semi-colons to mark the end of statements.

    You can try compiling your code and fixing the errors one-by-one (or multiple errors, if the compiler points out something you were doing wrong.

    Re-auto format frequently!

    When you have it compiled, then we can take another look at it,
     
    David Gould likes this.
Loading...