The idea of this project is for me to get some practice with my PIC chip. So I decided to use it to track how far my hamster (guy) runs on his wheel.
The method is simple, all i needed to do was count how many times the wheel span, which can be achieved with a Reed_switch and some simple code using my PIC chip. The maths behind this is pretty easy, all i needed to do was measure the circumference of the hamster wheel and do
circumference = π × diameter (that funny 'n' is pi)
Here is the source code, please bear in mind that its probably buggy or poorly done, but as i get better i shall be posting updated examples and expanding on this page.
''''''''''''''''''''''''''' ' PROJECT HAMSTER-O-METER ' ''''''''''''''''''''''''''' load_previous: READ 0,b0 READ 1,b1 main: symbol debugpin = b9 symbol rail = input1 symbol cycles = w0 'Set cycles to w0 symbol countbool = b10 'Set countbool to b10 because b1 will increase by 1 ' for every time b0 reaches 255 (b0 and b1 make ' up w0 [cycles]) hamster: debugpin = 1 'debug high 2 IF rail = 0 THEN IF countbool = 1 THEN countbool = 0 low 1 high 2 END IF END IF IF rail = 1 THEN IF countbool = 0 THEN countbool = 1 cycles = cycles + 1 bintoascii cycles,b4,b5,b6,b7,b8 sertxd(b4,b5,b6,b7,b8) high 1 low 2 END IF END IF GOTO dump dump: 'Dumpin WRITE 0,b0 WRITE 1,b1 GOTO hamster
Here i shall explain each part of my source code. I have seperated the code out by labels (which split the code by purpose).
In summary the code increases a variable by 1 every time a connection is made (when the hamster wheel spins). It saves the data to its internal memory and starts again, constantly checking to see if the input is high.
When the unit is powered on it loads the saved variables and starts checking.
READ 0,b0 READ 1,b1
This code will read my two variables which will be explained later from the memory.
symbol debugpin = b9 symbol rail = input1 symbol cycles = w0 symbol countbool = b10
These lines of code will make my code a bit more human readable. It means that from now on, 'debugpin' will refer to the byte 'b9', 'rail' will refer to the input 'input1', 'cycles' will refer to the 2 byte word variable 'w0' and 'countbool' will refer to the byte variable 'b10'.
Note: In this section i am only going to explain the important code
IF rail = 1 THEN IF countbool = 0 THEN countbool = 1 cycles = cycles + 1 bintoascii cycles,b4,b5,b6,b7,b8 sertxd(b4,b5,b6,b7,b8) high 1 low 2 END IF END IF
The point in this peice of code is to do the actual counting, this is one of the most important peices of code. The entire process is described below
| b8 | Units |
| b7 | Tens |
| b6 | Hundreds |
| b5 | Thousands |
| b4 | Ten Thousands |
So if i had the number 12,345, the variables would be assigned like this:
| b8 | 5 |
| b7 | 4 |
| b6 | 3 |
| b5 | 2 |
| b4 | 1 |
This means its easy to send the data via serial to a host computer to be analysed.
IF rail = 0 THEN IF countbool = 1 THEN countbool = 0 low 1 high 2 END IF END IF
In this if statement all its doing is
dump: 'Dumpin WRITE 0,b0 WRITE 1,b1 GOTO hamster
This code dumps the data into memory incase the chip loses power, read this.
This software is licensed under the GNU General Public License