Tree House Control System -
kmc123
Posts: 33
Hi everyone,
I'm building my boys a Tree House and the whole thing will be controlled by a BS2. Currently my code looks at an RFID R/W module (Hidden in a tree stump) and a switch (In the tree house) and will raise or lower the stairs if a valid tag is read or the switch is pressed.
I'll be adding alarm system functionality, sun tracking for the solar panels, etc... soon, but wanted some input from you first.
Please take a look at my code and let me know if you see any ways to clean it up / speed it up (Works as is, but I know it is not the most elegant )
Also - I want to add a 4x4 matrix keypad but as it is I have 8 pins left and don't want to use all 8 for the keypad (I need quite a few for the items mentioned above) - Can someone point me to a schematic and sample code for using a 4x4 keypad with some kind of interface that uses fewer pins? I know I saw something on a forum (Not sure if it was this one or not) that talked about the ability to do it, but I'd need a wiring diagram and sample code...
Anyway - Here is the code - Please let me know your advice...
I'm building my boys a Tree House and the whole thing will be controlled by a BS2. Currently my code looks at an RFID R/W module (Hidden in a tree stump) and a switch (In the tree house) and will raise or lower the stairs if a valid tag is read or the switch is pressed.
I'll be adding alarm system functionality, sun tracking for the solar panels, etc... soon, but wanted some input from you first.
Please take a look at my code and let me know if you see any ways to clean it up / speed it up (Works as is, but I know it is not the most elegant )
Also - I want to add a 4x4 matrix keypad but as it is I have 8 pins left and don't want to use all 8 for the keypad (I need quite a few for the items mentioned above) - Can someone point me to a schematic and sample code for using a 4x4 keypad with some kind of interface that uses fewer pins? I know I saw something on a forum (Not sure if it was this one or not) that talked about the ability to do it, but I'd need a wiring diagram and sample code...
Anyway - Here is the code - Please let me know your advice...
' ============================================================================================= ' ' File...... TreeHouse.bs2 ' Purpose... Master Tree House Control Code ' Author.... Kevin M. Cook ' E-mail.... [EMAIL="kevin@cookfamily.com"]kevin@cookfamily.com[/EMAIL] ' Updated... 01/15/2012 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ============================================================================================= ' -----[ Program Description ]----------------------------------------------------------------- ' This program is the heart of the Cook Boys Tree House Automation Control - It controlls the ' solar panel alignment, the lighting, the security systems, access control and staircase. ' -----[ I/O Pin Definitions ]----------------------------------------------------------------- ' PIN 0 Pins 1-7 are for future use as inputs from door / window contacts, cds cell sun ' PIN 1 tracker and tracker motors, etc... ' PIN 2 ' PIN 3 ' PIN 4 ' PIN 5 ' PIN 6 ' PIN 7 RFID_TX PIN 8 ' Connects to 28440 RFID R/W Module SIN RFID_RX PIN 9 ' Connects to 28440 RFID R/W Module SOUT LCD PIN 10 ' 27979 4x20 LCD and Piezo Speaker MAN_SWITCH PIN 11 ' Manual switch to raise and lower stairs from within the Tree House DOWN_LIMIT PIN 12 ' Limit switch that indacates stairs are at the "Down" position UP_LIMIT PIN 13 ' Limit switch that indacates stairs are at the "Up" position DOWN_RELAY PIN 14 ' Output to the "Down" motor relay UP_RELAY PIN 15 ' Output to the "Up" motor relay ' -----[ Constants ]--------------------------------------------------------------------------- #SELECT $STAMP #CASE BS2, BS2E, BS2PE T2400 CON 396 T9600 CON 84 T19K2 CON 32 #CASE BS2SX, BS2P T2400 CON 1021 T9600 CON 240 T19K2 CON 110 #ENDSELECT RfidBaud CON T9600 ' set Baud rate to 9600 for the RFID module LcdBaud CON T19K2 ' set Baud rate to 19200 for the LCD module LcdBkSpc CON $08 ' move cursor left LcdRt CON $09 ' move cursor right LcdLF CON $0A ' move cursor down 1 line LcdCls CON $0C ' clear LCD (use PAUSE 5 after) LcdCR CON $0D ' move pos 0 of next line LcdBLon CON $11 ' backlight on LcdBLoff CON $12 ' backlight off LcdOff CON $15 ' LCD off LcdOn1 CON $16 ' LCD on; cursor off, blink off LcdOn2 CON $17 ' LCD on; cursor off, blink on LcdOn3 CON $18 ' LCD on; cursor on, blink off LcdOn4 CON $19 ' LCD on; cursor on, blink on LcdLine0 CON $80 ' move to line 0, column 0 LcdLine1 CON $94 ' move to line 1, column 0 LcdLine2 CON $A8 ' move to line 2, column 0 LcdLine3 CON $BC ' move to line 3, column 0 'LcdCC0 CON $F8 ' define custom char 0 'LcdCC1 CON $F9 ' define custom char 1 'LcdCC2 CON $FA ' define custom char 2 'LcdCC3 CON $FB ' define custom char 3 'LcdCC4 CON $FC ' define custom char 4 'LcdCC5 CON $FD ' define custom char 5 'LcdCC6 CON $FE ' define custom char 6 'LcdCC7 CON $FF ' define custom char 7 Safety_Timer CON 6000 ' safety timer for motor protection ' -----[ Variables ]--------------------------------------------------------------------------- Idx VAR Byte ' index User VAR Byte ' stores user number for ID purposes Alarm_Count VAR Byte ' stores count of alarms for beep loop Buf VAR Byte(12) ' 12-byte array for data Buffer Stair_Timer VAR Word ' safety timer to stop motor burnout ' -----[ EEPROM Data ]------------------------------------------------------------------------- ' C#---- Data---------------------------------- 'CC0 DATA LcdCC0, $0E, $1F, $1C, $18, $1C, $1F, $0E, $00 'CC1 DATA LcdCC1, $0E, $1F, $1F, $18, $1F, $1F, $0E, $00 'CC2 DATA LcdCC2, $0E, $1F, $1F, $1F, $1F, $1F, $0E, $00 'Smiley DATA LcdCC3, $00, $0A, $0A, $00, $11, $0E, $06, $00 'Msg2 DATA "IS VERY COOL! ", 3 ' -----[ Initialize ]-------------------------------------------------------------------------- HIGH LCD ' setup serial output pin PAUSE 500 ' allow LCD to initialize SEROUT LCD, LcdBaud, [LcdBLon, LcdOn1, LcdCls] ' initialize PAUSE 500 SEROUT LCD, LcdBaud, ["Tree House", LcdCR] SEROUT LCD, LcdBaud, ["Control System", LcdCR] SEROUT LCD, LcdBaud, ["Version 1.0", LcdCR] SEROUT LCD, LcdBaud, ["Ryan & Matthew ROCK!"] PAUSE 5000 ' -----[ Program Code ]------------------------------------------------------------------------ Main: SEROUT LCD, LcdBaud, [LcdCls] PAUSE 5 'GOSUB Check_Manual ' Check manual switch 'GOSUB Read_Legacy ' Look For a EM4102 Read-only tag GOSUB Check_Manual ' Check manual switch GOSUB Read_Tag ' Look for a EM4x50 Read/Write Tag GOTO Main ' Keep looking :) ' -----[ Subroutines ]------------------------------------------------------------------------- Check_Manual: SEROUT LCD, LcdBaud, ["Check Manual Switch", LcdCR] IF MAN_SWITCH = 0 THEN ' Check To see If the manual switch is pressed - 0 = Pressed, 1 = Not SEROUT LCD, LcdBaud, ["Serial number: N/A", LcdCR] User = 0 GOTO Authorized ENDIF RETURN ' --------------------------------------------------------------------------------------------- Read_Tag: ' Look for a EM4x50 Read/Write Tag SEROUT LCD, LcdBaud, ["Check for RW Tag", LcdCR] SEROUT RFID_TX, RfidBaud, ["!RW", $01, 32] ' Instruct what to read SERIN RFID_RX, RfidBaud, [STR Buf\5] ' Get status byte and data bytes IF Buf(0) = $01 THEN Good_RW_Read ' If no errors, (A Good Read) RETURN ' --------------------------------------------------------------------------------------------- Read_Legacy: ' Look for a EM4102 read-only tag SEROUT LCD, LcdBaud, ["Check for RO Tag", LcdCR] SEROUT RFID_TX, RfidBaud, ["!RW", $0F] ' Instruct what to read SERIN RFID_RX, RfidBaud, [STR Buf\12] ' Get header and data IF Buf(0) = $0A THEN Good_RO_Read ' If no errors, (A Good Read) RETURN ' --------------------------------------------------------------------------------------------- Good_RW_Read: ' Getting to this point means that a RW tag was read. ' Show the tag's Serial Number SEROUT LCD, LcdBaud, ["Serial Number: ", LcdCR] FOR Idx = 1 TO 4 SEROUT LCD, LcdBaud, [HEX2 Buf(Idx)] NEXT ' Look to see if the serial number of the tag that was read is an authorized tag. IF Buf(1) = $01 AND Buf(2) = $95 AND Buf(3) = $D0 AND Buf(4) = $AC THEN User = 1 ' Kevin's Dog Tags GOTO Authorized ENDIF IF Buf(1) = $01 AND Buf(2) = $95 AND Buf(3) = $CA AND Buf(4) = $37 THEN User = 2 ' Ryan's Dog Tags GOTO Authorized ENDIF IF Buf(1) = $01 AND Buf(2) = $95 AND Buf(3) = $C9 AND Buf(4) = $5F THEN User = 3 ' Matthew's Dog Tags GOTO Authorized ENDIF ' If this spot is hit, the tag was read, but NOT authorized. GOSUB NOT_Authorized GOTO Main ' Do it all over again! ' --------------------------------------------------------------------------------------------- Good_RO_Read: ' Getting to this point means that a RO tag was read. ' Show the tag's Serial Number. SEROUT LCD, LcdBaud, ["Serial Number: ", LcdCR] FOR Idx = 1 TO 10 ' Display the data (ignore final \r byte sent by the reader) SEROUT LCD, LcdBaud, [Buf(Idx)] NEXT ' Look to see if the serial number of the tag that was read is an authorized tag. ' 4C182B6B55 = Home Again Injectable Pet Tracking Microchip Implant Kit from Amazon IF Buf(1) = $4C AND Buf(2) = $18 AND Buf(3) = $2B AND Buf(4) = $6B AND Buf(4) = $55 THEN User = 10 ' Home Again Implant Chip GOTO Authorized ENDIF ' If this spot is hit, the tag was read, but NOT authorized. GOSUB NOT_Authorized GOTO Main ' Do it all over again! ' --------------------------------------------------------------------------------------------- NOT_Authorized: SEROUT LCD, LcdBaud, [LcdCls, "Access DENIED!!!"] GOSUB Alarm RETURN ' --------------------------------------------------------------------------------------------- Authorized: SEROUT LCD, LcdBaud, [LcdCls, "Access Granted for: "] SELECT User CASE = 0 SEROUT LCD, LcdBaud, ["Manual Switch", LcdCR] CASE = 1 SEROUT LCD, LcdBaud, ["Kevin M. Cook", LcdCR] CASE = 2 SEROUT LCD, LcdBaud, ["Ryan M. Cook", LcdCR] CASE = 3 SEROUT LCD, LcdBaud, ["Matthew P. Cook", LcdCR] CASE = 10 SEROUT LCD, LcdBaud, ["Home Again Chip", LcdCR] ENDSELECT Stair_Timer = 0 ' 0 = Pressed / Closed, 1 = Not Pressed / Open IF UP_LIMIT = 0 AND DOWN_LIMIT = 1 THEN ' Stairs ARE at the top most position. ' Lower Stairs Here SEROUT LCD, LcdBaud, ["Lowering Stairs...", LcdCR] DO UNTIL DOWN_LIMIT = 0 HIGH DOWN_RELAY Stair_Timer = Stair_Timer + 1 IF Stair_Timer = Safety_Timer THEN LOW DOWN_RELAY SEROUT LCD, LCDBAUD, ["Safety Overide", LcdCR] PAUSE 3000 GOTO Main ENDIF LOOP LOW DOWN_RELAY SEROUT LCD, LcdBaud, ["Stairs Lowered...", LcdCR] PAUSE 5000 ELSEIF UP_LIMIT = 1 AND DOWN_LIMIT = 0 THEN ' Stairs ARE at the bottom most position. ' Raise Stairs Here SEROUT LCD, LcdBaud, ["Raising Stairs...", LcdCR] DO UNTIL UP_LIMIT = 0 HIGH UP_RELAY Stair_Timer = Stair_Timer + 1 IF Stair_Timer = Safety_Timer THEN LOW UP_RELAY SEROUT LCD, LCDBAUD, ["Safety Overide", LcdCR] PAUSE 3000 GOTO Main ENDIF LOOP LOW UP_RELAY SEROUT LCD, LcdBaud, ["Stairs Raised...", LcdCR] PAUSE 3000 ELSEIF UP_LIMIT = 1 AND DOWN_LIMIT = 1 THEN ' Stairs are somewhere between the low and high positions. SEROUT LCD, LcdBaud, ["Stairs are in limbo.", LcdCR] GOSUB ALARM PAUSE 3000 ELSEIF UP_LIMIT = 0 AND DOWN_LIMIT = 0 THEN ' Error - Impossible for stairs to be in both positions, Short? SEROUT LCD, LcdBaud, ["Error", LcdCR] GOSUB Alarm PAUSE 3000 ENDIF GOTO Main ' Do it all over again! ' --------------------------------------------------------------------------------------------- Alarm: DO WHILE Alarm_Count < 1 SEROUT LCD, LcdBaud, [$E6] ' Play G SEROUT LCD, LcdBaud, [$E1] ' Play D Alarm_Count = Alarm_Count + 1 LOOP Alarm_Count = 0 RETURN ' --------------------------------------------------------------------------------------------- END ' -----[ End of File ]-------------------------------------------------------------------------
Comments
You have a cool idea to build a tree house with your children that is different from the basic tree house that I built in the late 70s. In order to do 4x4 matrix keypad, you will need all 8 pins that you don't want to use. Which leaves out a standard one that Parallax sells. But, their is a way to get around this problem, by multiplexing inputs for the "Up and Down Limit Switches, door / window contacts and the Manual switch to raise and lower stairs. You can do the same thing for the non analog outputs. You did a great and neat job with your code and think you will do good with any changes that you make in the code.
-Phil
'
Build it and we will help you code it!!!!!!!!!!!!
'
Post some pics of the tree fort and I will send you a serial keypad and the code to use it.(really simple code)
I've got a bunch of pics at http://www.kevincook.net/Hobbies/Woodworking/TreeHouse/index.htm
-Phil
That's what I said too, Wow. The Wow part was, from looking at your other page on the website, that you have an automated paintball gun that will protect the tree house. With a big thanks to Walt, you have a serial keypad to use with the tree house. I am wondering what will happen when someone enters the wrong combo on the keypad?
BTW, I will be happy to help as well with code and designing a custom PCB for this project if you want.
I sooo love this place!!! William - I am pretty sure I will use a water blaster on the turret instead of the paintball gun now We were trying out the paintball guns and man do they hit hard! I don't want anyone to get hurt, so I'm thinking water blaster
'
Saw the pics...,Great tree fort!
'
I got the PM...I'll send the key pad out tomorrow.
'
Have fun with it.
'
Great work and effort!!!!!!!!!!
Thanks Walt! I'll let you know when it arrives. Is there somewhere you can point me to for sample code or specs?
'
I sent the key-pad and an example code to use it.
'
The code is really generic, I wrote it for a "fit all application".(super simple)
'
I threw in a few different resister that are handy to have around too.
'
Using these resister in series with the I/O pins will help to avoid burning out an I/O pin on your stamp in the event of a coding SNAFU.
'
I pride myself with the fact that I have never burned-out an I/O pin on a micro since I started playing with them in 2000'
'
The other resister are for pull-ups/pull-downs and LEDs
'
Please post some more PICs of your project as it progress's
'
P.S. the key-pad only needs one I/O pin
http://allaboutee.com/2011/07/15/one-ouput-keypad-circuit-design/
If you Gosub somewhere (IMO) you should always return to the calling statement from that sub.
I noticed, for example, you Gosub checkmanual....and following the most simple path you then Goto authorized and from there in many places you Goto main...having never executed the return from the original gosub checkmanual.
I would Gosub checkmanual...get a result and return, completing that thread...then gosub authorize....decide whether to authorize....etc.
Also, I clean up my initialization by throwing it all into my first gosub and throw all that text down at the bottom of the listing. That helps show where the main program actually is.
I think all of this can be broken down into steps/decisions easily enough. that's just my first take. of course, I've only been playing with Pbasic for a month or so.....so it's still hard for me to just read it like a romance novel.......o.k. bad analogy....i don't read those things.
Is there any other code you know of to look at?
Thanks again!!!
I'll look into that - I'm rewriting it for a BS2px right now - trying to get more code space...
Perfect time for me to try your suggestions! (I know what I really need is the propeller, but that's such a leap for me right now - I want to get it going on the BS first)
The keypad Walt hooked me up with uses a 3 pin cable - (Ground, Power, and 1 i/o pin) - This is exactly what I needed...
Is the keypad based on the same 1-pin design used in the article posted by Scott4? I'm wondering what the BS code looks like for that.
In the .... IF Buf(1) = $4C AND Buf(2) = $18 AND Buf(3) = $2B AND Buf(4) = $6B AND Buf(4) = $55 THEN
User = 10 ' Home Again Implant Chip....note that you checked Buf(4) twice....right here ^.
Secondly....I kinda ran into this before. Rather than having a long logical test on each element of the array...well....there's a better way. I just can't think of it right now. There's just something not right about doing it that way.
Another random thot....if you're running out of variable space (I have to go check about constants in that regard) note how the commands will be grouped....some having high nibble of $0x, some with $1x, $Fx etc. You can combine a high nibble with a low nibble to create the various groups and save a little. Like if cursor = $10.....cursor + $8, or cursor + $9
uuhh....time to study my visual basic now.
'
There is no other code for this key-pad, Other than what I sent you,That I know of. Its a one of type deal.It is very easy to use.
If you need some more help with this. Just post it here on your thread,I'll be watching.
'
It currently has 20 lag bolts holding it up and I'm not sure of the shear strength of them, but even if it was only 400 pounds that would be 8000 pounds before it would fail, and I'm not going to be close to that weight, (And I'd be willing to bet that the shear strength of those bolts is way over 400 pounds)
It's located at our cabin in Blowing Rock, NC - the rhodos are ALL OVER the place up there!!!
I have NOT considered taking it down later - I don't even want to go there yet
I set up a really nice 500 foot zipline for the boys and I'm thinking of moving it to the Treehouse location when it's all done, but right now it goes over a small pond which is really cool, and I really don't want to think of ANOTHER project yet
Here is a video of my 94 year old grandmother going down the zipline (2 years ago) :
http://www.youtube.com/watch?v=MDhGqPUDLfM
I hope I can be like her when I'm 94!!!
Oh - Yes - The keypad interpreter is a 1 pin design...
I bought the nets you see in those pics from an online surplus store and the shipping was almost as much as the nets!!!
How old are your boys?