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 1 of 2 | |||||
Author | Message | ||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
I'm looking at making an automatic gate opener using the Maximite. I need some help to get things started, basically I need a program so that by pressing O for open and C for closed to turn on relay 1 for open and relay 2 for closed. Also, the same effect by using two normally open switches, 1 for open and 2 for close to be connected to two of the Digital I/O's. I have a few more inputs to be added and two other outputs also, but I'll add that after I can see an example to set me on my way. This is my first attempt at programming using I/O's to be called into a program and just need some help to set me on my way. Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
thetinkerer Regular Member Joined: 16/06/2011 Location: AustraliaPosts: 68 |
Here is a bit of code that sets and resets pin 0 which is the power Led(green)when the "c" or "o" key is pressed. 10 DIM a$
20 a$ = INKEY$ 22 IF a$ = "c" THEN 23 pin(0) =1 'set pin 10 24 PRINT "pin0 set" 25 ENDIF 32 IF a$ = "o" THEN 33 pin(0) =0 'RESset pin 10 34 PRINT "pin0 Reset" 35 ENDIF 100 GOTO 20 Hope this helps. regards Marc |
||||
thetinkerer Regular Member Joined: 16/06/2011 Location: AustraliaPosts: 68 |
To extend this further, Line 22 (and 32) could be changed to something like this: IF a$ = "c" or pin(10) = 1 THEN This will operate if the "c" key is pressed or if pin 1 is on. regards Marc |
||||
James_From_Canb Senior Member Joined: 19/06/2011 Location: AustraliaPosts: 265 |
Or 10 setpin 10,2 ' set pin 10 to digital input to read the Open switch 20 setpin 11,2 ' set pin 11 to digital input to read the Close Switch 30 setpin 12,8 ' set pin 12 for 3.3V digital output to the Open relay 40 setpin 13,8 ' set pin 13 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(10) = 1 then 90 pin(0) = 1 ' replace this with pin(12) = 1 when you set up the relays 100 print "Gate Opening" 110 pause 5000 ' wait long enough for the gate to open (5000 = 5 seconds) 120 pin(0) = 0 ' and turn off the relay - replace this with pin(12) = 0 130 elseif Key$ = "C" OR pin (11) = 1 then 140 pin (0) = 1 ' replace this with pin(13) = 1 when you set up the relays 150 print "Gate Closing" 160 pause 5000 ' and wait for it to close 170 pin(0) = 0 ' replace with pin(13) = 0 180 endif 190 ' 200 goto 60 James My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. Hedley Lamarr, Blazing Saddles (1974) |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
James, I ran your program, the gate just opens in a loop. > run Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Gate Opening Any clues? Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
Does the input need to be held low and when I press the switch it goes high? or the input held high and when i press the switch it goes low? Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
rhamer Senior Member Joined: 06/06/2011 Location: AustraliaPosts: 174 |
With that code, the idle state is low, and button press is high. Are you using my expander? Because the logic will be inverted. Look at the last part of the documentation that I sent with the kit, it explains it. 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: AustraliaPosts: 347 |
Yes i'm using the V2 expander. James, in the normal condition your program runs in a loop opening the gate. Pressing o or c does nothing. pressing the switches does nothing. Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
Ok, So this is what i've got so far and it works! 10 High = 0
20 low = 1 30 ' 32 ' 34 ' setpin 1,2 ' set pin 1 to digital input to read the Open switch 36 ' setpin 2,2 ' set pin 2 to digital input to read the Close Switch 38 setpin 5,8 ' set pin 5 for 3.3V digital output to the Open relay 40 setpin 6,8 ' set pin 6 for 3.3V digital output to the Close relay 42 ' setpin 3,2 ' software open limit 44 ' setpin 4,2 ' software closed limit 50 CLS 60 input A$ 62 if A$ = "Q" then goto 210 63 if A$ = "q" then goto 210 64 if A$ = "O" then goto 100 66 if A$ = "o" then goto 100 68 if A$ = "C" then goto 150 70 if A$ = "c" then goto 150 'yes i know it's a bit long, but it works! 72 goto 50 100 pin(5) = 1 ' turns on pin(5) drives the relay 102 print "Gate Opening" 104 pause 50000 ' wait long enough for the gate to open 106 pin(5) = 0 ' resets pin(5) = 0 and turns off the relay 108 goto 50 150 pin(6) = 1 ' turns on pin(6) drives the relay 152 print "Gate Closing" 154 pause 50000 ' and wait for it to close 156 pin(6) = 0 ' resets pin(6) = 0 and turns off the relay 158 goto 50 180 ' 200 ' 210 end Next i will tackle the switch inputs. Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
James_From_Canb Senior Member Joined: 19/06/2011 Location: AustraliaPosts: 265 |
In case anyone else gets this sort of problem, it's because of the IF statement. It says if Key$ = "O" OR pin(10) = 1 then The value Key$ is the character read from the keyboard (or nothing) converted to upper case, and pin(10) tests the value of the pin. Normally Off is 0 and On is 1. VK6MGR noted that pressing the o or c did nothing. That's because of the OR in the IF statement. We used an OR, so if the tests on either side of the OR are true, the code is executed. Rohan explained that the logic states are inverted in his expander boards which brings up an important point, especially as we start adding hardware and changing pin assignments. We should be defining values as constants at the top of the program so we only have to change things once, and so that there is no confusion about what words like High and Low mean. 0 and 1 are just values that can be interpreted in different ways by different hardware. Defining what the values mean stops problems, and is good coding practice. The code was supposed to be testing when Pin 10 (a normally open switch) was High (ie switch closed). Because the code tested for the value 1, it was true any time the switch was open. Just what we didn't want. So that's why VK6MGR saw the messages scrolling past saying the gate was opening. The ELSEIF stopped the code ever checking if Key$ was "C". That explains why pressing "C" had no effect. VK6MGR: You have defined High and Low at the top of your code (tick), but you haven't used them in the actual code. You should replace all occurences of 1 and 0 with the values Low and High. For instance, the if statement would be: if Key$ = "O" OR pin(10) = High then There's one PS to this though. If you're testing using pin(0) to blink the LED, I'm guessing you should use the old 0=low, 1=high values. All the other stuff going through the expander should work. Even if you're not using the expander it's still good practice to replace numeric values in your code with named constants. A trivial example would be to replace the 50000 with a constant called WaitTime. That way you only have to change it once, however many times it is used in the code, and it is a form of self-documentation which means you can use less comments, and have some chance of knowing what you intended when you come to review the code in a couple of months. One more PS. I didn't test the code before I posted it, but reckon it should have worked if Rohan's High and Low definitions had been added. Does this work? Again, I haven't tested it. 3 ' Constants 4 ' --------- 5 High = 0 6 Low = 1 7 WaitTime = 50000 8 '---------- 10 setpin 1,2 ' set pin 1 to digital input to read the Open switch 20 setpin 2,2 ' set pin 2 to digital input to read the Close Switch 30 setpin 5,8 ' set pin 5 for 3.3V digital output to the Open relay 40 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) = High then 90 pin(5) = High ' Open the relay 100 print "Gate Opening" 110 pause WaitTime ' wait long enough for the gate to open 120 pin(5) = Low ' and turn off the relay 130 elseif Key$ = "C" OR pin (2) = High then 140 pin(6) = High ' Open the relay 150 print "Gate Closing" 160 pause WaitTime ' and wait for it to close 170 pin(6) = Low ' and turn off the relay 180 elseif Key$ = "Q" then 190 END 200 endif 210 ' 220 goto 60 James My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. Hedley Lamarr, Blazing Saddles (1974) |
||||
rhamer Senior Member Joined: 06/06/2011 Location: AustraliaPosts: 174 |
Exactly right James. What is high, low, on or off is dependent on the hardware that is interfaced to it. So setting constants at the start of the program, then using them instead of absolute values is the only way to go. 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: AustraliaPosts: 347 |
OK, first I have removed the 10 High = 0 and 20 low = 1 as the program works without it. Still trying to get my head around this whole interfacing thing Second, I have added the WAITTIME = 50000 at the start of the program and changed the program to have PAUSE WAITTIME Looks better, works well. As was said, makes it easier and faster to alter the wait time if needed. Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
James, I cut and pasted the program you made up and tried to run it and came up with this error. Any clues. Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
James_From_Canb Senior Member Joined: 19/06/2011 Location: AustraliaPosts: 265 |
Yep. My bad. Get rid of the space between pin and ( on line 130. My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. Hedley Lamarr, Blazing Saddles (1974) |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
Ok, hear it is so far, Thanks James. Now I need to add a stop button and stop in the software, so I can type s and stop the program and a switch for gate opened and a switch for gate closed. This is to act as a limit switch, so that if the gate opens faster than normal (battery and solar setup, so if battery is fully charged the actuator will run a bit faster) the limit switch will end the open or closed sequence. Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
Would I use a GOSUB to have the extra switch inputs? Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
Would this work? 110 if pin(4) = 0 or PAUSE WAITTIME ' Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
James_From_Canb Senior Member Joined: 19/06/2011 Location: AustraliaPosts: 265 |
What happened to using constants for High and Low? It's your program, but it's difficult to read because I have to keep translating what 1 and 0 mean. Is it actually working like that? But, to answer your question: You need another IF statement to check for the stop switch activating before the time reaches WaitTime. But you also want to be able to interrupt even that with your emergency stop. Let me think about it a bit... My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. Hedley Lamarr, Blazing Saddles (1974) |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
Ok, I’m getting there. This is the code to date. I configured the relay logic to suit the 1 and 0 states and got it working as it is but I can change that. 3 ' Constants
4 ' --------- 5 High = 1 6 Low = 0 7 WaitTime = 50000 8 '---------- 10 setpin 1,2 ' set pin 1 to digital input to read the Open switch 12 setpin 2,2 ' set pin 2 to digital input to read the Close Switch 14 setpin 3,2 ' set pin 3 to digital input to read the Opened switch 16 setpin 4,2 ' set pin 4 to digital input to read the Closeed Switch 30 setpin 5,8 ' set pin 5 for 3.3V digital output to the Open relay 40 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" 110 pause WaitTime ' wait long enough for the gate to open 120 pin(5) = 0 ' 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" 160 pause WaitTime ' and wait for it to close 170 pin(6) = 0 ' and turn off the relay 180 elseif Key$ = "Q" then 190 END 200 endif 210 ' 220 goto 60 So, if the gate is open and you press "o" or the open button, nothing happens! If the gate is closed and you press "c" or the close switch, nothing happens! I'm just testing some limit switches that i have to see if they are suitable, but a magnet mounted on the actuator and an N/O reed switch is all that’s required. Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
VK6MRG Guru Joined: 08/06/2011 Location: AustraliaPosts: 347 |
I need to remove the PAUSE statement and have something like WHEN pin(3) = high (or low) then GOTO (or whatever is next to be done) Any ideas? Its easier to ask forgiveness than to seek permission! ............VK6MRG.............VK3MGR............ |
||||
Page 1 of 2 |
Print this page |