Help with BASIC code please!

Discussion in 'Programmer's Corner' started by BillyJ, Dec 11, 2012.

  1. BillyJ

    Thread Starter New Member

    Dec 11, 2012
    12
    0
    Hi guys,
    this is my first post, so read with noob caution!
    so im doing this project which involves having a door unlock, the door lock (a solenoid) has to be help open until the door is closed. I know i need an interrupt command but don't know how to use it properly :confused:
    I have this code which will pause for 2/100's of a second before returning, but i need it to wait until the condition "btn1 = 1" to be true.

    THANKS! :D
    BillyJ



    Here's the current code for the 2/100's pause:

    Code ( (Unknown Language)):
    1.  
    2. if bnt1 = 0 then
    3.            
    4.         interrupt        
    5.                                    
    6.         pause 20
    7.         setint %10000000,%10000000    
    8.      
    9.        
    10.        
    11.         return                        
    12.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Hey Billy. Why do you want to use an interrupt command? Is that part of an assignment or just something you heard? (Homework assignments are OK, just means we have to blindly accept the conditions.)

    So I have a few questions:

    What is btn1?
    How do you know when to open the door/solenoid?
    How do you actually open the door/solenoid?
    How do you know when the door is open or closed?

    And hey, what compiler are you using?

    If I knew that I could help you with the code for this.


    And welcome to the forums!
     
  3. BillyJ

    Thread Starter New Member

    Dec 11, 2012
    12
    0
    I was just told that the interrupt command was the best thing to use, as i want to return to where i was in the code once its finished.



    btn1 is a micro switch that tells me if the door is open or closed

    i have a pir sensor that gives a logic high for 3 seconds to open the door

    the door is only unlocked and i have a a board that can hold the solenoid open with about 8v

    The btn1(micro switch) will tell me, i have it so the switch can be normally open or closed depending on how i set it up, the switch just goes into my microcontroller

    and for the compiler i not fully sure how to answer that, but im using:
    MacAXEapad software
    A PICAXE 18-M2 chip

    Thanks Ernie!
     
  4. SolderFlux

    New Member

    Dec 6, 2012
    9
    1
    Hi BillyJ,

    It is correct to use an Interupt Service Routine to detect an external event. However jumping into a ISR means you potentially disable all other interupt functions especially when you have a delay in your ISR. I suggest you use a polling method in your main loop. Debounce button1 and with a valid debounce set a flag in your main loop to tell if button1 is 0 or 1.
     
  5. BillyJ

    Thread Starter New Member

    Dec 11, 2012
    12
    0
    Hi SolderFlux

    I was using an interrupt because i want everything to stop until btn1 is 1 or 0, but my problem is more how do i make it wait for the button state to change before resuming. i want use a delay if i don't have to, and this is the only interrupt command in my code.

    Thanks
     
  6. rstevenson

    New Member

    Apr 5, 2011
    21
    1
    If you want everything else to stop while you are waiting you can do a busy wait with a while loop.
    so while button = same state
    do nothing
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    So far, if all you are doing is opening the door solenoid I don't see any compelling reason to use an interrupt.

    There is a little talked about step that happens before writing code and that is design.

    Is the door controller the only function of the micro?

    To start, PIR senses person, opens solenoid. True?

    Is there a max time to hold the solenoid open? What if the person just turns around and walks away without opening door? What if they prop the door open?
     
  8. BillyJ

    Thread Starter New Member

    Dec 11, 2012
    12
    0
    O.K. thanks guys,
    the micro is used to unlock the door, if it gets a pulse from a pir, it also send a pulse to an arduino im using to play a sound.

    And yes, pir senses person, plays sound, unlocks door, waits for a few seconds, locks door if door is closed, if not wait till door is closed.

    I have designed it. So to continue answering your questions:
    yes about 3 seconds, well then it will lock after 3 seconds if the door is closed. And then it will wait to be closed.

    Thanks
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Seems you have a good outline the code flow (a kind of flow chart) right there,so now all you need do is translate each step from words to code.
     
  10. BillyJ

    Thread Starter New Member

    Dec 11, 2012
    12
    0
    OK so i have this at the moment then:

    Code ( (Unknown Language)):
    1.  
    2. symbol r = C.7 'Red status LED
    3. symbol g = C.6 'Green Status LED
    4. symbol ard1 = C.0 'First arduino output
    5. symbol sol = C.1 'This pin controls the soloniod
    6. symbol btn = pinC.2 'This is the input signal from the PIR sensor
    7. symbol btn1 = pinC.1 ;THIS IS GOING TO BE THE MICRO SWITCH
    8.                                
    9.  
    10. Main:
    11.     High r
    12.     low ard1
    13. ;HERE I NEED TO HAVE THE INTERUPT CODE TO STOP EVERYTHING UNTIL THE MICRO SWITCH IS OPEN/CLOSED
    14.     if btn = 1 then
    15.         low r
    16.         gosub ArduinoControl1
    17.         gosub UnlockDoor
    18.         pause 150
    19.     end if
    20.    
    21.     goto Main
    22.    
    23. ArduinoControl1:
    24.            
    25.     high ard1
    26.     pause 100
    27.     low ard1
    28.    
    29. UnlockDoor:
    30.     High sol
    31.     high g
    32.     pause 2700
    33.     goto lockdoor
    34.    
    35. LockDoor:
    36.     low sol
    37.     low g
    38.     high r
    39.     goto main
    40.  
    Thanks
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I think you are very close, but I don't think you need the interrupt where you think you do from the comment. It looks like you have everything in place except for waiting for the door to be closed once more.

    As you say " locks door if door is closed, if not wait till door is closed" so that is all that is missing from the UnlockDoor area:

    Code ( (Unknown Language)):
    1. UnlockDoor:
    2.     High sol
    3.     high g
    4. Wait4DoorClosed:
    5.     if BTN1 = 1 then goto Wait4DoorClosed
    6.     pause 2700
    7.     goto lockdoor
    8.  
    It's been a while since I used Basic so there may be better structures to do that, but basically once the door is unlocked the code waits right there until it senses the door is closed, then goes thru the 3 (2.7?) second delay and closes the solenoid.

    Your program is just doing one simple linear function, not trying to juggle 101 things at the same time, so you can afford to just sit and wait in the code for things to happen.

    The only drawback here is if the person at the door blocks it open, the solenoid stays active and that may burn it out after a while (hours?).
     
Loading...