Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:15 23 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : A little help

     Page 2 of 2    
Author Message
rhamer
Senior Member

Joined: 06/06/2011
Location: Australia
Posts: 174
Posted: 05:21am 23 Jul 2011
Copy link to clipboard 
Print this post

Because you have 3 conditions that your testing for in line 80 you need to put some brackets around 2 of them to force the evaluation order.

Consider this

If a=1 or b=0 and c=1 then do something

Is different logic to

If a=1 or (b=0 and c=1) then do something

This is because what's in the brackets gets evaluated to either true or false first, then that result gets evaluated against the first expression to ultimately determine if the "IF" statement evaluates to true.

If it does it will do what is after the "then" otherwise it won't.

See how you go with that. If it all sounded like Chinese then I'll have another go a explaining I better.

Cheers

Rohan


Rohan Hamer
HAMFIELD Software & Hardware Solutions

Makers of the Maximite Expander.

http://www.hamfield.com.au
 
VK6MRG

Guru

Joined: 08/06/2011
Location: Australia
Posts: 347
Posted: 05:28am 23 Jul 2011
Copy link to clipboard 
Print this post

That makes sense Rohan, just like a mathematic equation, work the brackets first.
Its easier to ask forgiveness than to seek permission!

............VK6MRG.............VK3MGR............
 
rhamer
Senior Member

Joined: 06/06/2011
Location: Australia
Posts: 174
Posted: 05:37am 23 Jul 2011
Copy link to clipboard 
Print this post

One other thing is you can (and should when you understand better) use constants with pins as well.

So at the start of your program have something like.

10 OpenButton = Pin(1)
20 CloseButton = Pin(2)
30 GateIsOpen = Pin(3)
40 GateIsClosed = Pin(4)

50 Opened = 1
60 Closed = 0

Then you can write very readable code like

100 IF GateIsOpened = Opened then do something

Again I hope that makes sense, and the syntax might not be exact as I am doing it from 20 year old memory.

Cheers

RohanEdited by rhamer 2011-07-24
Rohan Hamer
HAMFIELD Software & Hardware Solutions

Makers of the Maximite Expander.

http://www.hamfield.com.au
 
seco61
Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 205
Posted: 08:21am 23 Jul 2011
Copy link to clipboard 
Print this post

Hi.

Here is a bit of code that you can play with for your gate opener controller. I have not tested it as I have not set up the I/O connections.

Be aware, that if you run the code and the I/O pins are not connected to switches then the results will be fascinating as the input pins will be floating.

Please adjust the constants to match your requirements.


1 ' Constants
2 Relay_on = 1
3 Inactive = 1
4 Closed = 1
5 High = 1
6 Relay_off = 0
7 Active = 0
8 Pressed = 0
9 Low = 0

10 ' Pin configuration definitions
11 Digital_input = 2
12 Int_on_low_to_high = 6
13 Int_on_high_to_low = 7
14 Digital_output = 8

20 ' Pin definitions
21 open_sw = 1
22 close_sw = 2
23 opened_sensor = 3
24 closed_sensor = 4
25 open_relay = 5
26 close_relay = 6

50 ' Maximum relay operate time (in seconds)
51 WaitTime = 50

100 setpin open_sw, Digital_input ' set pin to digital input to read the Open switch
110 setpin close_sw, Digital_input ' set pin to digital input to read the Close switch
120 setpin opened_sensor, Int_on_high_to_low, 4000 ' set pin to digital input to read the Opened sensor and execute interrupt on high to low transition
130 setpin closed_sensor, Int_on_high_to_low, 5000 ' set pin to digital input to read the Closed sensor and execute interrupt on high to low transition
140 pin(open_relay) = Relay_off ' initialise open relay state
145 setpin open_relay, Digital_output ' set pin for 3.3V digital output for the Open relay
150 pin(close_relay) = Relay_off ' initialise close relay state
155 setpin close_relay, Digital_output ' set pin for 3.3V digital output for the Close relay

200 settick 1000, 3000 ' set the timer tick interrupt to execute every 1000 milliseconds (1 second)
210 Wait_timer = 0
220 open_relay_state = Relay_off
230 close_relay_state = Relay_off

1000 Key$ = UCASE$(INKEY$)
1100 do while Key$ <> "Q"
1200 if (Key$ = "O" or pin(open_sw) = Pressed) and pin(closed_sensor) = Active then
1300 pin(open_relay) = Relay_on ' Turn on the relay
1350 open_relay_state = Relay_on
1400 print "Gate Opening"
1500 Wait_timer = WaitTime ' set timer for maximum relay on time
1700 elseif (Key$ = "C" or pin(close_sw) = Pressed) and pin(opened_sensor) = Active then
1800 pin(close_relay) = Relay_on ' Turn on the relay
1850 close_relay_state = Relay_on
1900 print "Gate Closing"
2000 Wait_timer = WaitTime ' set timer for maximum relay on time
2100 pin(close_relay) = Relay_off ' and turn off the relay
2200 endif
2300 Key$ = UCASE$(INKEY$)
2500 loop
2999 end

3000 ' interrupt routine for periodic 1 second tick
3010 if Wait_timer <> 0 then
3020 Wait_timer = Wait_timer - 1
3030 if Wait_Timer = 0 then
3100 if open_relay_state = Relay_on then
3110 open_relay_state = Relay_off
3120 pin(open_relay) = Relay_off ' turn off the relay
3130 endif
3200 if close_relay_state = Relay_on then
3210 close_relay_state = Relay_off
3220 pin(close_relay) = Relay_off ' turn off the relay
3230 endif
3300 endif
3400 endif
3999 ireturn

4000 ' interrupt routine to handle opened_sensor activated
4100 if open_relay_state = relay_on then
4200 open_relay_state = relay_off
4300 pin(open_relay) = Relay_off ' turn off the relay
4400 endif
4999 ireturn

5000 ' interrupt routine to handle closed_sensor activated
5100 if close_relay_state = relay_on then
5200 close_relay_state = relay_off
5300 pin(close_relay) = Relay_off ' turn off the relay
5400 endif
5999 ireturn


regards

Gerard (vk3cg/vk3grs)Edited by seco61 2011-07-24

Regards

Gerard (vk3cg/vk3grs)
 
VK6MRG

Guru

Joined: 08/06/2011
Location: Australia
Posts: 347
Posted: 08:46am 23 Jul 2011
Copy link to clipboard 
Print this post

Here is the current Version of the MAXI GATE.

3 ' Constants
4 ' ---------
5 High = 1
6 Low = 0
7 WaitTime = 45000
8 OpenSwitch = 1
9 CloseSwitch = 2
10 OpenedSwitch = 3
11 ClosedSwitch = 4
12 OpenRelay = 5
13 CloseRelay = 6
14 '
20 setpin 1,2 ' set pin 1 to digital input to read the Open switch
22 setpin 2,2 ' set pin 2 to digital input to read the Close Switch
24 setpin 3,2 ' set pin 3 to digital input to read the Opened switch
26 setpin 4,2 ' set pin 4 to digital input to read the Closed Switch
28 setpin 5,8 ' set pin 5 for 3.3V digital output to the Open relay
30 setpin 6,8 ' set pin 6 for 3.3V digital output to the Close relay
50 '
60 A$=INKEY$
70 Key$ = UCASE$(A$) ' now it doesn't matter if you use O or o, C or c.
80 if Key$ = "O" OR pin(1) = low AND pin(4) = Low then
90 pin(5) = High ' Open the relay
100 print "Gate Opening"
101 ExitLoop$ = "No"
102 Timer = 0
103 DO
104 GOSUB 300
108 LOOP until ExitLoop$ = "Yes" OR PIN(3) = Low
120 pin(5) = Low ' and turn off the relay
130 elseif Key$ = "C" OR pin(2) = Low AND pin(3) = Low then
140 pin(6) = High ' Open the relay
150 print "Gate Closing"
152 Timer = 0
153 DO
154 GOSUB 500
158 LOOP until ExitLoop$ = "Yes" OR PIN(4) = Low
160 '
170 pin(6) = Low ' and turn off the relay
180 elseif Key$ = "Q" then
190 END
200 endif
210 '
220 goto 60
230 '--------
300 ' Subroutine to manage opening the gate
310 if pin(3) = Low then
320 ExitLoop$ = "Yes"
330 endif
340 if timer > WaitTime AND ExitLoop$ = "No" then
350 ? "ALARM! ALARM! ' and all the rest of the code it needs
360 endif
370 Return
420'-------
500 ' Subroutine to manage closing the gate
510 if pin(4) = Low then
520 ExitLoop$ = "Yes"
530 endif
540 if timer > WaitTime AND ExitLoop$ = "No" then
550 ? "ALARM! ALARM! ' and all the rest of the code it needs
560 endif
570 Return
620 '-----


Just need to add some code for the alarm subroutine, maybe an over current alarm if the gate is or gets jammed....


Its easier to ask forgiveness than to seek permission!

............VK6MRG.............VK3MGR............
 
seco61
Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 205
Posted: 09:00am 23 Jul 2011
Copy link to clipboard 
Print this post

Hi.

I my previous post I left in an extraneous command. I would have edited the post but for some reason the edit option has disappeared.

Please delete the following line:


2100 pin(close_relay) = Relay_off ' and turn off the relay



Also the following routines should reset the wait timer:


4000 ' interrupt routine to handle opened_sensor activated
4100 if open_relay_state = relay_on then
4200 open_relay_state = relay_off
4250 Wait_timer = 0
4300 pin(open_relay) = Relay_off ' turn off the relay
4400 endif
4999 ireturn

5000 ' interrupt routine to handle closed_sensor activated
5100 if close_relay_state = relay_on then
5200 close_relay_state = relay_off
5250 Wait_timer = 0
5300 pin(close_relay) = Relay_off ' turn off the relay
5400 endif
5999 ireturn


regards

Gerard (vk3cg/vk3grs)
Edited by seco61 2011-07-24

Regards

Gerard (vk3cg/vk3grs)
 
VK6MRG

Guru

Joined: 08/06/2011
Location: Australia
Posts: 347
Posted: 09:14am 23 Jul 2011
Copy link to clipboard 
Print this post

  seco61 said   Hi.

Here is a bit of code that you can play with for your gate opener controller. I have not tested it as I have not set up the I/O connections.

Be aware, that if you run the code and the I/O pins are not connected to switches then the results will be fascinating as the input pins will be floating.

Please adjust the constants to match your requirements.


1 ' Constants
2 Relay_on = 1
3 Inactive = 1
4 Closed = 1
5 High = 1
6 Relay_off = 0
7 Active = 0
8 Pressed = 0
9 Low = 0

10 ' Pin configuration definitions
11 Digital_input = 2
12 Int_on_low_to_high = 6
13 Int_on_high_to_low = 7
14 Digital_output = 8

20 ' Pin definitions
21 open_sw = 1
22 close_sw = 2
23 opened_sensor = 3
24 closed_sensor = 4
25 open_relay = 5
26 close_relay = 6

50 ' Maximum relay operate time (in seconds)
51 WaitTime = 50

100 setpin open_sw, Digital_input ' set pin to digital input to read the Open switch
110 setpin close_sw, Digital_input ' set pin to digital input to read the Close switch
120 setpin opened_sensor, Int_on_high_to_low, 4000 ' set pin to digital input to read the Opened sensor and execute interrupt on high to low transition
130 setpin closed_sensor, Int_on_high_to_low, 5000 ' set pin to digital input to read the Closed sensor and execute interrupt on high to low transition
140 pin(open_relay) = Relay_off ' initialise open relay state
145 setpin open_relay, Digital_output ' set pin for 3.3V digital output for the Open relay
150 pin(close_relay) = Relay_off ' initialise close relay state
155 setpin close_relay, Digital_output ' set pin for 3.3V digital output for the Close relay

200 settick 1000, 3000 ' set the timer tick interrupt to execute every 1000 milliseconds (1 second)
210 Wait_timer = 0
220 open_relay_state = Relay_off
230 close_relay_state = Relay_off

1000 Key$ = UCASE$(INKEY$)
1100 do while Key$ <> "Q"
1200 if (Key$ = "O" or pin(open_sw) = Pressed) and pin(closed_sensor) = Active then
1300 pin(open_relay) = Relay_on ' Turn on the relay
1350 open_relay_state = Relay_on
1400 print "Gate Opening"
1500 Wait_timer = WaitTime ' set timer for maximum relay on time
1700 elseif (Key$ = "C" or pin(close_sw) = Pressed) and pin(opened_sensor) = Active then
1800 pin(close_relay) = Relay_on ' Turn on the relay
1850 close_relay_state = Relay_on
1900 print "Gate Closing"
2000 Wait_timer = WaitTime ' set timer for maximum relay on time
2100 pin(close_relay) = Relay_off ' and turn off the relay
2200 endif
2300 Key$ = UCASE$(INKEY$)
2500 loop
2999 end

3000 ' interrupt routine for periodic 1 second tick
3010 if Wait_timer <> 0 then
3020 Wait_timer = Wait_timer - 1
3030 if Wait_Timer = 0 then
3100 if open_relay_state = Relay_on then
3110 open_relay_state = Relay_off
3120 pin(open_relay) = Relay_off ' turn off the relay
3130 endif
3200 if close_relay_state = Relay_on then
3210 close_relay_state = Relay_off
3220 pin(close_relay) = Relay_off ' turn off the relay
3230 endif
3300 endif
3400 endif
3999 ireturn

4000 ' interrupt routine to handle opened_sensor activated
4100 if open_relay_state = relay_on then
4200 open_relay_state = relay_off
4300 pin(open_relay) = Relay_off ' turn off the relay
4400 endif
4999 ireturn

5000 ' interrupt routine to handle closed_sensor activated
5100 if close_relay_state = relay_on then
5200 close_relay_state = relay_off
5300 pin(close_relay) = Relay_off ' turn off the relay
5400 endif
5999 ireturn


regards

Gerard (vk3cg/vk3grs)


Looks good. I've run it and the open part works but the close has an issue. I suspect it's my hardware as I have wires going everywhere
Thanks for the help.
I'm also seeing the importance of the spacing in the command lines in regards to the if and endif statements! This is something that I have been having trouble with!

Its easier to ask forgiveness than to seek permission!

............VK6MRG.............VK3MGR............
 
VK6MRG

Guru

Joined: 08/06/2011
Location: Australia
Posts: 347
Posted: 09:27am 23 Jul 2011
Copy link to clipboard 
Print this post

  seco61 said   Hi.

I my previous post I left in an extraneous command. I would have edited the post but for some reason the edit option has disappeared.

Please delete the following line:


2100 pin(close_relay) = Relay_off ' and turn off the relay



Also the following routines should reset the wait timer:


4000 ' interrupt routine to handle opened_sensor activated
4100 if open_relay_state = relay_on then
4200 open_relay_state = relay_off
4250 Wait_timer = 0
4300 pin(open_relay) = Relay_off ' turn off the relay
4400 endif
4999 ireturn

5000 ' interrupt routine to handle closed_sensor activated
5100 if close_relay_state = relay_on then
5200 close_relay_state = relay_off
5250 Wait_timer = 0
5300 pin(close_relay) = Relay_off ' turn off the relay
5400 endif
5999 ireturn


regards

Gerard (vk3cg/vk3grs)


OK, removing the line 2100 fixed the closing problem.
Thanks again.
Its easier to ask forgiveness than to seek permission!

............VK6MRG.............VK3MGR............
 
seco61
Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 205
Posted: 03:31pm 23 Jul 2011
Copy link to clipboard 
Print this post

Hi.

Looking at the code again there is a (very) remote possibility that the wait timer could be updated asynchronously due to the interrupts, so to remove any affect this could have the following line should be changed to:


3010 if Wait_timer > 0 then


The full code would then be:


1 ' Constants
2 Relay_on = 1
3 Inactive = 1
4 Closed = 1
5 High = 1
6 Relay_off = 0
7 Active = 0
8 Pressed = 0
9 Low = 0

10 ' Pin configuration definitions
11 Digital_input = 2
12 Int_on_low_to_high = 6
13 Int_on_high_to_low = 7
14 Digital_output = 8

20 ' Pin definitions
21 open_sw = 1
22 close_sw = 2
23 opened_sensor = 3
24 closed_sensor = 4
25 open_relay = 5
26 close_relay = 6

50 ' Maximum relay operate time (in seconds)
51 WaitTime = 50

100 setpin open_sw, Digital_input ' set pin to digital input to read the Open switch
110 setpin close_sw, Digital_input ' set pin to digital input to read the Close switch
120 setpin opened_sensor, Int_on_high_to_low, 4000 ' set pin to digital input to read the Opened sensor and execute interrupt on high to low transition
130 setpin closed_sensor, Int_on_high_to_low, 5000 ' set pin to digital input to read the Closed sensor and execute interrupt on high to low transition
140 pin(open_relay) = Relay_off ' initialise open relay state
145 setpin open_relay, Digital_output ' set pin for 3.3V digital output for the Open relay
150 pin(close_relay) = Relay_off ' initialise close relay state
155 setpin close_relay, Digital_output ' set pin for 3.3V digital output for the Close relay

200 settick 1000, 3000 ' set the timer tick interrupt to execute every 1000 milliseconds (1 second)
210 Wait_timer = 0
220 open_relay_state = Relay_off
230 close_relay_state = Relay_off

1000 Key$ = UCASE$(INKEY$)
1100 do while Key$ <> "Q"
1200 if (Key$ = "O" or pin(open_sw) = Pressed) and pin(closed_sensor) = Active then
1300 pin(open_relay) = Relay_on ' Turn on the relay
1350 open_relay_state = Relay_on
1400 print "Gate Opening"
1500 Wait_timer = WaitTime ' set timer for maximum relay on time
1700 elseif (Key$ = "C" or pin(close_sw) = Pressed) and pin(opened_sensor) = Active then
1800 pin(close_relay) = Relay_on ' Turn on the relay
1850 close_relay_state = Relay_on
1900 print "Gate Closing"
2000 Wait_timer = WaitTime ' set timer for maximum relay on time
2100 endif
2200 Key$ = UCASE$(INKEY$)
2300 loop
2999 end

3000 ' interrupt routine for periodic 1 second tick
3010 if Wait_timer > 0 then
3020 Wait_timer = Wait_timer - 1
3030 if Wait_Timer = 0 then
3100 if open_relay_state = Relay_on then
3110 open_relay_state = Relay_off
3120 pin(open_relay) = Relay_off ' turn off the relay
3130 endif
3200 if close_relay_state = Relay_on then
3210 close_relay_state = Relay_off
3220 pin(close_relay) = Relay_off ' turn off the relay
3230 endif
3300 endif
3400 endif
3999 ireturn

4000 ' interrupt routine to handle opened_sensor activated
4100 if open_relay_state = relay_on then
4200 open_relay_state = relay_off
4250 Wait_timer = 0
4300 pin(open_relay) = Relay_off ' turn off the relay
4400 endif
4999 ireturn

5000 ' interrupt routine to handle closed_sensor activated
5100 if close_relay_state = relay_on then
5200 close_relay_state = relay_off
5250 Wait_timer = 0
5300 pin(close_relay) = Relay_off ' turn off the relay
5400 endif
5999 ireturn


Regards

Gerard (vk3cg/vk3grs)

Regards

Gerard (vk3cg/vk3grs)
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024