gpio strangeness?

on-topic Acorn-related news and discussions not covered by the other forums
Post Reply
avtovaz
Posts: 72
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

gpio strangeness?

Post by avtovaz » Sun Mar 22, 2020 8:32 pm

im having a strange problem with the gpio and the bbc basic. the program controls 2 windows and 2 fans via gpio, and also an 8 relay module. So first thing i noticed in my program is that i have set a variable G to be my power on, and im using another 6 A-F for window/fan operations.

Now, i set A-G as 0 or clr in the program and they turn on, and 1 turns them off [see the code]

now all was working fine until i decided to put a proc in for turning the power on and off in the operations.

so before i was saying
power on
open 1
close 1
power off

now i am trying to type
open 1
close 1

and in the stat of each proc called ive put another proc at the start for power on, and one at the end for power off. It dosnt work, and i cant work out why. i think its to do with the first part not the adding of the power on and power off procs.


sorry for A> my bad explanation and B> for the lamer code!

thanks

Code: Select all


REM WINDOW PROGRAM

REM Initialise GPIO:
INSTALL @lib$ + "gpiolib"
GPIO% = FN_gpio_setup

REM Pin numbers to activate, in sequence:
DATA 14, 15, 18, 23, 24, 25, 8

REM Set to input first:
RESTORE
FOR I% = 1 TO 7
READ pin%
PROC_gpio_inp(GPIO%, pin%)
NEXT

REM Set pins to output:
RESTORE
FOR I% = 1 TO 7
READ pin%
PROC_gpio_out(GPIO%, pin%)
NEXT

REM RESET
PROCZEROSTART
PROCPINS

MODE 4
REPEAT
INPUT "STATE ACTION...",Z$
IF Z$="POWER ON" THEN PROCPOWERON
IF Z$="POWER OFF" THEN PROCPOWEROFF
IF Z$="OPEN 1" THEN PROCOPEN1
IF Z$="OPEN 2" THEN PROCOPEN2
IF Z$="FAN 1 ON" THEN PROCFAN1ON
IF Z$="FAN 2 ON" THEN PROCFAN2ON
IF Z$="FAN 1 OFF" THEN PROCFAN1OFF
IF Z$="FAN 2 OFF" THEN PROCFAN2OFF
IF Z$="CLOSE 1" THEN PROCCLOSE1
IF Z$="CLOSE 2" THEN PROCCLOSE2
IF Z$="ALL OFF" THEN PROCZEROSTART
IF Z$="CLOSE BOTH" THEN PROCCLOSEBOTH
IF Z$="BOTH FANS ON" THEN PROCBOTHFANSON
IF Z$="BOTH FANS OFF" THEN PROCBOTHFANSOFF
PROCPINS
CLS
PRINT A;B;C;D;E;F;G
PRINT Z$
UNTIL FALSE



REM SET THE PIN OUTPUTS
DEF PROCPINS
IF A=1 THEN PROC_gpio_set(GPIO%, 2^14)
IF A=0 THEN PROC_gpio_clr(GPIO%, 2^14)
IF B=1 THEN PROC_gpio_set(GPIO%, 2^15)
IF B=0 THEN PROC_gpio_clr(GPIO%, 2^15)
IF C=1 THEN PROC_gpio_set(GPIO%, 2^18)
IF C=0 THEN PROC_gpio_clr(GPIO%, 2^18)
IF D=1 THEN PROC_gpio_set(GPIO%, 2^23)
IF D=0 THEN PROC_gpio_clr(GPIO%, 2^23)
IF E=1 THEN PROC_gpio_set(GPIO%, 2^24)
IF E=0 THEN PROC_gpio_clr(GPIO%, 2^24)
IF F=1 THEN PROC_gpio_set(GPIO%, 2^25)
IF F=0 THEN PROC_gpio_clr(GPIO%, 2^25)
IF G=1 THEN PROC_gpio_set(GPIO%, 2^8)
IF G=0 THEN PROC_gpio_clr(GPIO%, 2^8)
ENDPROC


REM RESET ALL
DEF PROCZEROSTART
A=1
B=1
C=1
D=1
E=1
F=1
G=1
Z$=""
ENDPROC

DEF PROCPOWERON
G=0
PROCPINS
ENDPROC

DEF PROCPOWEROFF
G=1
PROCPINS
ENDPROC


DEF PROCOPEN1
PROCPOWERON
PROCCLOSE1
E=0
F=0
PROCPINS
WAIT 280
E=1
F=1
PROCPINS
PROCPOWEROFF
ENDPROC

DEF PROCCLOSE1
PROCPOWERON
PROCFAN1OFF
E=1
F=0
PROCPINS
WAIT 500
E=1
F=1
PROCPINS
PROCPOWEROFF
ENDPROC


DEF PROCOPEN2
PROCPOWERON
PROCCLOSE2
A=0
B=0
PROCPINS
WAIT 300
A=1
B=1
PROCPINS
PROCPOWEROFF
ENDPROC

DEF PROCCLOSE2
PROCPOWERON
PROCFAN2OFF
A=1
B=0
PROCPINS
WAIT 500
A=1
B=1
PROCPINS
PROCPOWEROFF
ENDPROC

DEF PROCFAN1ON
PROCPOWERON
PROCOPEN1
C=0
PROCPINS
PROCPOWEROFF
ENDPROC

DEF PROCFAN2ON
PROCPOWERON
PROCOPEN2
D=0
PROCPINS
PROCPOWEROFF
ENDPROC


DEF PROCFAN1OFF
PROCPOWERON
C=1
PROCPINS
PROCPOWEROFF
ENDPROC

DEF PROCFAN2OFF
PROCPOWERON
D=1
PROCPINS
PROCPOWEROFF
ENDPROC

DEF PROCCLOSEBOTH
PROCPOWERON
PROCCLOSE1
PROCCLOSE2
PROCPOWEROFF
ENDPROC

DEF PROCBOTHFANSON
PROCPOWERON
PROCFAN1ON
PROCFAN2ON
PROCPOWEROFF
ENDPROC

DEF PROCBOTHFANSOFF
PROCPOWERON
PROCFAN1OFF
PROCFAN2OFF
PROCPOWEROFF
ENDPROC

END 


avtovaz
Posts: 72
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: gpio strangeness?

Post by avtovaz » Thu Mar 26, 2020 10:06 pm

well i havnt found out exactly why, but if i do a proc with in a proc, sometimes i wont work the pins properly. Maybe it forgets a value or something, but i have changed the way i have worked it. maybe it forgets where it goes back to at the end proc i cant work it out.

i can now open and close my windows with out getting up, that is awesome. When i slipped a disk in my back a few years ago, this would have been really usefull!

User avatar
flaxcottage
Posts: 3939
Joined: Thu Dec 13, 2012 8:46 pm
Location: Derbyshire
Contact:

Re: gpio strangeness?

Post by flaxcottage » Fri Mar 27, 2020 9:34 am

Place an END between

UNTIL FALSE


END : REM needed here to stop the program running through the PROCs unless they are called.

REM ...
DEFPROCPINS
- John

Image

User avatar
Richard Russell
Posts: 1146
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: gpio strangeness?

Post by Richard Russell » Fri Mar 27, 2020 9:57 am

avtovaz wrote:
Thu Mar 26, 2020 10:06 pm
well i havnt found out exactly why, but if i do a proc with in a proc, sometimes i wont work the pins properly.
I'm not exactly sure why it isn't always working, but I'm worried about all those global variables (A, B, C, D, E, F, G) which is not a very good way of doing things (excessive use of global variables can lead to poorly-structured and difficult-to-maintain programs).

As far as I can see, the purpose of those global variables is to 'remember' the current state of each of the GPIO pins, but you don't need to do that in your program because the GPIO system remembers it for you! Because the GPIO commands you issue are 'set pin' or 'reset pin' all the other pins are automatically left in their previous states, unchanged. You are re-establishing those states in PROCPINS but that is unnecessary - you are giving your program extra work for no benefit, and I suspect introducing a bug at the same time.

I would strongly recommend stepping back and restructuring your program. Don't keep setting (or resetting) pins to their current states; try to think in terms of actions, not states. When you want to do something like turn a fan on or off, change the state of that pin only. That's easier to understand, easier to code and safer!

avtovaz
Posts: 72
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: gpio strangeness?

Post by avtovaz » Fri Mar 27, 2020 8:35 pm

thankyou again for the reply. the globals where for when i goto the 8 bit shift register, i though it would be easier. i am sure that is the problem now as i have instead of using the globals and procpins, ive just put each pin action down and it works.


so i am happy with the operations as they are, and ill try to program the shift register chip, and solder up a board for it. It is getting complex very quickly !

thanks again.

oh, do you know why also the set and clr is inversed for me? i cant work that out!

Post Reply