Help with BASIC code please!

Thread Starter

BillyJ

Joined Dec 11, 2012
12
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:

Rich (BB code):
if bnt1 = 0 then 
           
        interrupt        
                                   
        pause 20 
        setint %10000000,%10000000     
      
        
        
        return
 

ErnieM

Joined Apr 24, 2011
8,377
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!
 

Thread Starter

BillyJ

Joined Dec 11, 2012
12
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!
 
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.
 

Thread Starter

BillyJ

Joined Dec 11, 2012
12
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
 

rstevenson

Joined Apr 5, 2011
20
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
 

ErnieM

Joined Apr 24, 2011
8,377
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?
 

Thread Starter

BillyJ

Joined Dec 11, 2012
12
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
 

ErnieM

Joined Apr 24, 2011
8,377
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
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.
 

Thread Starter

BillyJ

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

Rich (BB code):
symbol r = C.7 'Red status LED
symbol g = C.6 'Green Status LED
symbol ard1 = C.0 'First arduino output
symbol sol = C.1 'This pin controls the soloniod
symbol btn = pinC.2 'This is the input signal from the PIR sensor
symbol btn1 = pinC.1 ;THIS IS GOING TO BE THE MICRO SWITCH
                                

Main:
    High r
    low ard1
;HERE I NEED TO HAVE THE INTERUPT CODE TO STOP EVERYTHING UNTIL THE MICRO SWITCH IS OPEN/CLOSED
    if btn = 1 then
        low r
        gosub ArduinoControl1
        gosub UnlockDoor
        pause 150
    end if
    
    goto Main
    
ArduinoControl1:
            
    high ard1
    pause 100
    low ard1
    
UnlockDoor:
    High sol
    high g
    pause 2700
    goto lockdoor
    
LockDoor:
    low sol
    low g
    high r
    goto main
Thanks
 

ErnieM

Joined Apr 24, 2011
8,377
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:

Rich (BB code):
UnlockDoor:
    High sol
    high g
Wait4DoorClosed:
    if BTN1 = 1 then goto Wait4DoorClosed
    pause 2700
    goto lockdoor
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?).
 
Top