Traffic monetization

Hamster-O-Meter [kichimi.co.uk]
 

Hamster-O-Meter

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.

Method

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)

Source Code

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.

Code

hamster.bas
'''''''''''''''''''''''''''
' 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

Explanation

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.

load_previous

	READ 0,b0
	READ 1,b1

This code will read my two variables which will be explained later from the memory.

main

	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'.

hamster

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

  • Check to see if the hamster wheel is on its way past the reed switch
  • If it isnt then dont do anything and just continue on as normal, but if it is on its way past then..
    • Check to see if countbool is set to 0, if it isnt then dont do anything as countbool is just a marker to see if we've already counted this cycle or not. If it is then
      • Set countbool to 1, this is to mark this cycle as counted so it doesnt get counted more than once
      • Increment cycles by 1
      • Convert it to ASCII just incase there is a computer connected, so it can be sent as plain text and be human readable. it sets the variables like this:
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.

  • Send the data via serial to the host computer
	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

  • Check to see if rail = 0, if it doesnt then dont do anything, if it does then
    • Check to see if countbool = 1, if it doesnt then dont do anything, if it does then
      • Set countbool to 0, this readies the code above to be able to log another cycle

dump

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

 
hamster-o-meter.txt · Last modified: 2011/01/24 16:01 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki