Noob question :)

Post here to discuss programming with swordfish basic

Moderators: Chuckt, Garth, bitfogav

Post Reply
User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London

Noob question :)

Post by odessa » Sat Oct 23, 2010 11:03 pm

Hi all,

Right .... I'm using Swordfish ( and Proton as well ) to brush up my Basic programming skills..

What I would like to know is how do I get my program to watch for a button press continually ?

I can add lines in code to see if a button is pressed or not but its not an effecient way to do it as its all dependant on where that line is.

Is there a way I can use code to constantly be looking for a button press no matter where my code is at ?

Thanks

Jay
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Noob question :)

Post by bitfogav » Sun Oct 24, 2010 8:44 pm

Hi Jay

Sounds to me like you might need to look at using an Interrupt maybe?, this could be used to continually call a subroutine which checks your button, it depends on your programming structure. feel free to post your code so we can see what you are trying to achieve, but you could take a look at this website which has plenty of code examples and lots of information.. :)

http://www.digital-diy.com

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Noob question :)

Post by brad » Sun Oct 24, 2010 9:12 pm

Interrupts are certainly the way to go if you want it to always be checking. However, I have had no problems with just calling a routine. This is because, it calls the routine and checks the buttons and a much faster rate than I could ever press and release a button.

What were you planning on using it for?

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London

Re: Noob question :)

Post by odessa » Sun Oct 24, 2010 10:57 pm

Cheers Guys,

All I'm doing at the moment is making leds chase in patterns, strobe, using a PWM code etc etc ... just messing about really. I found out how to use the button commands within Proton which is amazingly easy compared to asm when it comes to debouncing etc.
I'm using a button to switch between routines which works fine except when a delay is used in a routine, especially in the PWM code as while it delays its not checking for a button press. I kind of guessed I would need to look at interupts, and the Digital-DIy site covers that I see :)

I'm not building anything solid at the moment, just having fun learning :)

I must say ..... using basic is AMAZING !!!!!!!!!! :D :D :D :D

After about an hour reading the Digital DIY guides I can now connect an LCD to my pic and display anything I want on it ... in Text or Dec... WOW :D

I made a times table calculator and a simple thermometer using a themistor ( and some ifffy guess conversion code lol ) but WOW .... It really is amazing

It really has opened up some amazing possibilties after using assembly :)

Jay
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Noob question :)

Post by brad » Mon Oct 25, 2010 5:01 am

It is a fantastic language and certainly a breath of fresh air compared to working with assembly :)

Now as for your buttons, What is the delay for? Is the delay there to debounce the buttons? If so, this is the way I go about debouncing buttons:

Code: Select all

Sub check_buttons()
    If left = 0 And button_delay = 0 Then
        inc(moved_spaces)
        button_delay = 15
    EndIf
    If button_delay <> 0 Then
        Dec(button_delay)
    EndIf
End Sub
I have taken just a small snippet of code from one of my games and cut out the fat. But when it calls the button routine it first checks to see if a button is pressed (in my case it's looking for a logic 0 because that's how I have wired up my hardware) If the button IS pressed AND the button delay is zero then it will accept the press and do accordingly.

Each time the button is pressed, it loads up the button_delay variable which will then prevent the button press from being accepted for a pre-determined period of time. However it does not stop the rest of the program running like calling a delay would.

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London

Re: Noob question :)

Post by odessa » Mon Oct 25, 2010 6:59 am

Hi Brad


What I mean is if my program has a delay to run an led sequence ie: led1=1 delayms 500, then for 500ms i cant monitor what my button is doing.

The debounce is all built in on Proton with the button command:

Code: Select all

buttonloop:
Button PORTB.3,0,255,250,btnvar,0,NoPress
GoTo flash
NoPress:
GoTo buttonloop
Its a bit like the btfss and btfsc in assembler, the variables are for repeat ( 255 = none ) 250 for count etc etc ...

which makes things very easy :)
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Noob question :)

Post by brad » Tue Oct 26, 2010 11:31 pm

Yes I see now!

You're absolutely correct, if you have a delay like that then nothing else can be happening (including checking for a switch press)

Just as has been said, interrupts are certainly the way to go, no matter what was happening in your project, if it sensed an interrupt (via the switch) then it would stop whatever it was doing, service the interrupt and then go back to whatever it was doing.

Digital-diy.com has loads of great code examples, I am sure they would be in the know about this task.

The only other way I could think of at the moment would be something like this:

Code: Select all

while true
     if led_delay > 1000 then
          led = 1
     elseif led_delay < 1000 then
          led = 0
     endif
     dec(led_delay)
     if led_delay = 0 then
          led_delay = 2000
     endif
     if button = 1 then
          do whatever...
     endif
wend

this piece of code doesn't actually use a delay where everything actually has to stop. it has a led_delay variable that is always counting down from 2000 (i just picked a number off the top of my head...) it then checks to see if the number is greater than or less than 1000. one way or the other the led will be either on or off. it then checks to the button and performs whatever functions there.

So you effectively get the same result, without using interrupts and with great accuracy as to the detection of a button press.

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London

Re: Noob question :)

Post by odessa » Wed Oct 27, 2010 12:30 am

Thats a clever way of doing it ! :D

I can see I'll need to look at interupts though, I have been reading loads on the Digital DIY site .... Its excellent, and very friendly for noobs too :)

Its really great seeing different ways of coding and improving the code too

Basic is def the the way forward for I think... I'm playing with both Proton and Swordfish which are similar enough not to be too confusing. I chose Proton as well as Swordfish as it supports lots
of my smaller chips which are great for smaller projetcs

I'm finding so much dropping into place now its great, Thanks for your help :)
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Noob question :)

Post by bitfogav » Wed Oct 27, 2010 2:30 am

odessa wrote: Basic is def the the way forward for I think... I'm playing with both Proton and Swordfish which are similar enough not to be too confusing. I chose Proton as well as Swordfish as it supports lots
of my smaller chips which are great for smaller projetcs
Ive done the same as you Jay, Ive got proton and swordfish, they work almost similar, its just some of the syntax that are different eg:

Code: Select all

while 1=1

Code: Select all

while true
Brad thats a great way of checking a switch and setting a time delay :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest