Please look over my code


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Mar 2004
    Posts
    92

    Default Please look over my code

    I'm using a 12F675 with 4mhz ceramic osc to control my homebuilt gate opener. I'm activating it with a wireless digital remote unit that has it's own relay, when signal is rx'd the relay simply closes and provides a high (+5v) to GPIO.3. This pin is also held low via a 10k resistor. This then toggles GPIO.0 which I have 2 status LED's hooked cathode to cathode.

    The problem is, all will work fine for days then suddenly it will start "toggling". That is, the gate will open, stop for a while then close over and over then simply go back to normal. Very randomly ! My PS is simply a sealed lead acid 12V battery with nothing else connected as of now. I thought the problem might be RF activating the wireless relay so i put a 3 second routine in, same problem so I disconnected the remote completely from the circuit and it happened again today. I currently have the board on my workbench powered by the same battery and it's stable for now, of course.

    So if you will please look at my code and see if it's something in there ? I know I'm still not real good at PBP but I've actually had a lot of success with many other projects using PBP and I love it.

    Thanks very much for your time,

    Sam

    Code:
    '****************************************************************
    '*  Name    : This is the one used for the gate opener !!!      *
    '*  Author  : SD                                                *
    '*  Notice  : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 3/15/2016                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :  THIS WORKS MUST HOLD DOWN FOR 3 SECONDS TO TOGGLE*
    '*          :                                                   *
    '****************************************************************
    CMCON=%00000111
    ANSEL=%00000000
    
    
    @ DEVICE pic12F675, XT_OSC
    _NOCLKOUT
    @ DEVICE pic12F675, WDT_ON
    @ DEVICE pic12F675, PWRT_ON
    @ DEVICE pic12F675, MCLR_OFF
    @ DEVICE pic12F675, BOD_ON
    
    DEFINE OSC 4
    INCLUDE "modedefs.bas"
    
    btn var gpio.3
    LoopTime var word
    
    'low gpio.0
    low gpio.1
    low gpio.2
    'high gpio.3
    low gpio.4
    low gpio.5
    
    InputButton:
    pause 20
    if btn = 1 then Check4Button
    goto InputButton
    
    Check4Button:
    for LoopTime = 0 to 3000     'Set time button must be down before toggle happens. 50= 50ms, 3000= 3 sec
    pause 1
    'if btn = 0 then close               'Do not use this line for gate opener
    next LoopTime
    if btn = 1 then tgl
    goto InputButton
    
    tgl:
    toggle gpio.0
    if gpio.0=1 then 
    gosub close
    endif
    
    if gpio.0=0 then
    gosub open
    endif
    goto InputButton
    
    
    
    
    
    open
    pause 1000       'Orange LED on
    high gpio.1
    pause 18500
    low gpio.1
    goto InputButton
    
    close
    pause 1000     'Blue LED on
    high gpio.2
    pause 19000
    low gpio.2
    goto InputButton

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,636


    Did you find this post helpful? Yes | No

    Default Re: Please look over my code

    three things
    1. these two pieces don't have proper labels ,relying on the good nature of the compiler is probably not a good idea



    open
    pause 1000 'Orange LED on
    high gpio.1
    pause 18500
    low gpio.1
    goto InputButton

    close
    pause 1000 'Blue LED on
    high gpio.2
    pause 19000
    low gpio.2
    goto InputButton
    2 spaghetti code like that is very difficult to debug or follow the logic of

    3 rmw issue possibilities abound

    for what its worth try it this way .it may look more complicated but it compiles to smaller code that's more predictable imo
    ps not sure what you are doing with gpio.0 , if it is a sensor indicating gate open/closed or moving state you need to add it back in to the code




    Code:
    CMCON=%00000111
    ANSEL=%00000000
    
    @ DEVICE pic12F675, XT_OSC
    _NOCLKOUT
    @ DEVICE pic12F675, WDT_ON
    @ DEVICE pic12F675, PWRT_ON
    @ DEVICE pic12F675, MCLR_OFF
    @ DEVICE pic12F675, BOD_ON
    
    DEFINE OSC 4
    
    trisio= %111001
    btn var gpio.3          ; assume low is active
    gate_state var byte  ; 0=closed ,1= open , 2=moving
    open_gate var gpio.1
    close_gate var gpio.2
    button_count var byte   ;counter to bebounce btn and ensure hold time
    gpio_shadow var byte     ;shadow gpio  to eliminate rmw issues
    'initialise
    open_gate  = 0
    close_gate = 0
    gate_state = 0    ;assume closed on power up
    button_count=0
    gpio_shadow=0
    
    mainloop:
    gosub Check4Button
    if (button_count > 30) and (gate_state.1 != 1)  then gosub move_gate
    pause 100
    goto mainloop
    Check4Button:
    if btn=0 then
     button_count=button_count+1   ;if button is active add to count
    else
     button_count=0    ;reset count
    endif
    return
     
    move_gate:
    pause 1000  ; not sure if this is required
    gate_state.1=1  ;gate now moving
    if  gate_state.0 = 0 then ;its closed so open the gate
        gpio_shadow=gpio_shadow | 2 ; high gpio.1   Orange LED on 
        ; same as gpio_shadow.1=1  if you prefer
        gate_state.0 = 1
     else       ; close the gate
        gpio_shadow=gpio_shadow | 4      'high gpio.2 Blue LED on
        ;same as gpio_shadow.2=1      if you prefer
        gate_state.0 = 0
    endif
    gpio=gpio_shadow
    pause 18500
    gpio_shadow=gpio_shadow & !6 
    ;same as gpio_shadow.1=0 : gpio_shadow.2=0 if you prefer
    gpio=gpio_shadow
    'while ! btn  ; wait for button release if needed
    'wend
    button_count=0
    gate_state.1=0
    return
    Last edited by richard; - 27th March 2016 at 03:47. Reason: typo as usual

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,636


    Did you find this post helpful? Yes | No

    Default Re: Please look over my code

    just can't help myself
    now have flashing led gpio.0 when gate in motion
    Code:
    CMCON=%00000111
    ANSEL=%00000000
    
    @ DEVICE pic12F675, XT_OSC
    _NOCLKOUT
    @ DEVICE pic12F675, WDT_ON
    @ DEVICE pic12F675, PWRT_ON
    @ DEVICE pic12F675, MCLR_OFF
    @ DEVICE pic12F675, BOD_ON
    
    DEFINE OSC 4
    trisio= %111000
    btn var gpio.3          ; assume low is active
    gate_state var byte  ; 0=closed ,1= open , 2=moving
    open_led var gpio.0
    open_gate var gpio.1
    close_gate var gpio.2
    button_count var byte   ;counter to bebounce btn and ensure hold time
    RUN_COUNT VAR BYTE
    gpio_shadow var byte     ;shadow gpio  to eliminate rmw issues
    'initialise
    open_gate  = 0
    close_gate = 0
    gate_state = 0    ;assume closed on power up
    button_count=0
    gpio_shadow=0
    open_led =0
    RUN_COUNT=0
    mainloop:
    gosub Check4Button
    if (button_count > 30) and (gate_state.1 != 1)  then gosub move_gate    ;has button been on for 3 seconds
    
    IF gate_state.1=1 THEN     ;is the gate in motion
    open_led =! open_led     ;flash led while gate moving 
    IF   RUN_COUNT THEN    ;should the motion continue ?
    RUN_COUNT = RUN_COUNT -1
    ELSE
    GOSUB STOP_GATE
    ENDIF
    ENDIF
    pause 100
    goto mainloop
    Check4Button:        ; when  button_count gets to 30 (3seconds ) we act on gate
    if btn=0 then
     button_count=button_count+1   ;if button is active add to count
    else
     button_count=0    ;reset count
    endif
    return
     
    move_gate:
    pause 1000  ; not sure if this is required
    gate_state.1=1  ;gate now moving
    if  gate_state.0 = 0 then ;its closed so open the gate
        gpio_shadow.0=1     ;WILL SET open_led =  ON 
        gpio_shadow=gpio_shadow | 2 ; high gpio.1   Orange LED 
        ; same as gpio_shadow.1=1  if you prefer
        gate_state.0 = 1
     else       ; close the gate
        gpio_shadow=gpio_shadow | 4      'high gpio.2 Blue LED on
        ;same as gpio_shadow.2=1      if you prefer
        gate_state.0 = 0    ;show closed gate  open_led =  Off 
        gpio_shadow.0=0   ; WILL SET open_led = FF
    endif
    gpio=gpio_shadow   ;apply to gpio
    RUN_COUNT=185;0    ;set 18.5 second timer for movement
    return
    
    STOP_GATE:
    gpio_shadow=gpio_shadow & ~6   ;kill movement
    ;same as gpio_shadow.1=0 : gpio_shadow.2=0 if you prefer
    gpio=gpio_shadow   ;apply to gpio
    'while ! btn  ; wait for button release if needed
    'wend
    button_count=0  ;reset everything ready for next button press
    gate_state.1=0; NOW no moving
    return

  4. #4
    Join Date
    Mar 2004
    Posts
    92


    Did you find this post helpful? Yes | No

    Default Re: Please look over my code

    WOW! Thanks for all your help and work Richard!

    I'll study it and try it out. I should have done a "block flow" for what I want to happen...

    Wait for button press (GPIO.3 is button sense input)

    Require button pressed for 3 seconds (GPIO.0 toggles) (orange and blue LEDs hooked cathode to cathode as status)

    Energize relay 1 for 18.5 seconds to open gate (GPIO.2)

    Wait for button press

    Require button pressed for 3 seconds

    Energize relay 2 for 19 seconds to close gate (GPIO.1)

    Start over
    The movement is controlled only by timing, I have no external sensors/limit switches or encoders.

    What do these mean in your code ? (!) and (;) I've not used or seen those.

    Thanks again, going to try it on the bench this morning.
    Last edited by Sam; - 27th March 2016 at 14:41.

  5. #5
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Please look over my code

    Sam,

    the ";" simply denotes a comment. Any code after the ";" has no effect on the program other than to provide a comment anyone trying to figure out what the code is supposed to be doing.

    the "!" is used in combination with the "=" and means "not equal".

    From the PBP manual...
    NOT EQUAL TO you can use either "<>" or "!="
    so where he has
    open_led != open_led
    simply means open_led "is not equal to" open_led.
    It causes the state of "open_led" to go to the opposite state. Similar to a "Toggle" command.

    A couple of other things to consider...
    Is there any chance of moisture accumulating on your PCB that would cause it to be erratic??
    Is your power supply good and stable?
    Why do you use an external ceramic resonator when the PIC has an internal 4MHz oscillator (not that this would cause you problems but it does reduce the I/O pins you have available.)
    You might want to consider incorporating a gate open/closed sensor for positive indication of gate position.



    dwight
    Last edited by Heckler; - 27th March 2016 at 14:51.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  6. #6
    Join Date
    Mar 2004
    Posts
    92


    Did you find this post helpful? Yes | No

    Default Re: Please look over my code

    Thanks Dwight, I've always used (') to comment out notes and have used (<>= NOT) but I do now see these in the manual, I just never noticed them before. Good to know, always learning.

    A couple of other things to consider...
    Is there any chance of moisture accumulating on your PCB that would cause it to be erratic??
    Is your power supply good and stable?
    Why do you use an external ceramic resonator when the PIC has an internal 4MHz oscillator (not that this would cause you problems but it does reduce the I/O pins you have available.)
    You might want to consider incorporating a gate open/closed sensor for positive indication of gate position.
    The PCB is in a gasketed weatherproof enclosure and has been totally dry
    Power supply is a brand new 12v SLA battery that has been at 12+ volts consistently during this time, I use a to220 7805 with caps
    I use the XT OSC because of timing only control and temperature differences
    I did consider limit switches or better, IR limit sensors but wanted to keep it simple as possible. But that would be more precise and reliable

Similar Threads

  1. avr code to code for PIC
    By JasonMcG in forum General
    Replies: 1
    Last Post: - 11th September 2014, 00:57
  2. Serial problem between BasicStamp code and PBP code
    By AllanZilkowsky in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 6th April 2014, 02:15
  3. Working code but my layman approach uses too much code space
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th December 2012, 20:44
  4. Code: Why is this code greater than 2000 words?
    By DrDreas in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 1st June 2007, 19:51

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts