Re-acting to Hex inputs

Following on from my (hopefully) successful build of the mp3 player, I want to choose a track (from 16 available) to play by responding to a logical input across 4 input pins ie 0-F Hex. My idea is to use one of these to select the inputs then a separate input (push button) as a "trigger" to read the other 4 inputs and re-act accordingly via something like a CASE instruction. I can work out most of this myself except how to transpose the 4 inputs from a hex to a decimal output that I can use in the CASE code. I'm thinking it's going to be a logical string of 'ands' and 'nots' eg if p1 = 1 and p2 = 0 and p3 = 1 and p4 = 0 then "variable dec" = 10.

I hope that makes sense. If it helps to understand my request: I am building a model railway with a station. The station will have a speaker that makes various announcements such as "The train now standing at platform........." etc.
 

hippy

Technical Support
Staff member
You should be able to read the four bits of that switch and form a 4-bit number which can then be used to select things, using SELECT CASE or otherwise. For example -

Code:
b0 = pinsB & $0F
Select Case b0
  Case $0 : ...
  Case $1 : ...
  Case $2 : ...
Getting the pins into b0 ( or any other variable ) might require some inversion and even bit reassignment but is easy enough to do. For example an entirely generic routine might be as follows. LEGx_LOW indicates pin value when low -

Code:
Symbol LEG0 = pinB.2 : Symbol LEG0_LOW = 0
Symbol LEG1 = pinB.1 : Symbol LEG1_LOW = 0
Symbol LEG2 = pinB.3 : Symbol LEG2_LOW = 0
Symbol LEG3 = pinB.6 : Symbol LEG3_LOW = 0

b0   = 0
bit0 = LEG0 ^ LEG0_LOW
bit1 = LEG1 ^ LEG1_LOW
bit2 = LEG2 ^ LEG2_LOW
bit3 = LEG3 ^ LEG3_LOW
Select Case b0
  Case $0 : ...
That could be optimised as required and appropriate for whatever hardware you have.

If you only want to select a track from the number that is pretty easy to do. Once you have created a hex number, that is also a decimal number, so simply feed that to the routine which plays the song.
 

westaust55

Moderator
As a follow on to hippy's response, PICAXE chip like almost all processors/computers work in binary.
Decimal and hexadecimal (even octal) are only mechanisms to help us humans understand the numeric values.

Whether you use a binary, hex or decimal notation in your code, the PICAXE in effect always considers the binary value.

Thus if you read in the data from 4 consecutive pins as per hippy's example the select options can be in hex or decimal and either will work.
 

hippy

Technical Support
Staff member
First thing to do is to work out which PICAXE pins you will connect your hex switch to.

Code-wise it is easier if you select x.0, x.1, x.2, x.3 pins (x=C,B,A or D) and wire them in the order which gives the correct value for switch positions. Second best is any contiguous set of four pins. But any input pins can be used in any order.

Most hex switches are active low and require pull-up resistors so choosing pins which have internal pull-up resistors will simplify the hardware, but we are only talking four resistors.

Once the pins are chosen it is reasonably easy to make the code suit the hardware and members here can help with that if you encounter problems or cannot see how the code works.

If you don't understand any particular code or need assistance on that; please feel free to ask.
 
First thing to do is to work out which PICAXE pins you will connect your hex switch to.

Code-wise it is easier if you select x.0, x.1, x.2, x.3 pins (x=C,B,A or D) and wire them in the order which gives the correct value for switch positions. Second best is any contiguous set of four pins. But any input pins can be used in any order.

Most hex switches are active low and require pull-up resistors so choosing pins which have internal pull-up resistors will simplify the hardware, but we are only talking four resistors.

Once the pins are chosen it is reasonably easy to make the code suit the hardware and members here can help with that if you encounter problems or cannot see how the code works.

If you don't understand any particular code or need assistance on that; please feel free to ask.
Hi Hippy, I'm still struggling with this. I can get the data into pinsb but I can't seem to copy that into a variable. I am using an 18M2 and have selected B.0, B.1, B.2 and B.3 as my 4 hex inputs plus C5 as my trigger. The idea of the trigger is the program will loop waiting on C.5, I select an input with the switch (1-F) and then "press" the trigger to read it in. At the moment I have not got beyond trying the code out using the emulator. Sorry, don't know how to insert code here so added it as an attachment
 

Attachments

hippy

Technical Support
Staff member
I am using an 18M2 and have selected B.0, B.1, B.2 and B.3 as my 4 hex inputs plus C5 as my trigger.
You could start with this test code ...

Code:
#Picaxe 18M2
#Terminal 4800
Do
  Do : Loop While pinC.5 = 0
  b0 = pinsB & %00001111
  SerTxd( "Switch value = ", #b0, CR, LF )
  Pause 500
Loop
That should Simulate and also work in a real chip.
 
You could start with this test code ...

Code:
#Picaxe 18M2
#Terminal 4800
Do
  Do : Loop While pinC.5 = 0
  b0 = pinsB & %00001111
  SerTxd( "Switch value = ", #b0, CR, LF )
  Pause 500
Loop
That should Simulate and also work in a real chip.
Sir, you are a genius! Can you please break down line 5 for me and explain what is happening?

Oh, and how do I insert code in a post for future reference? Thanks.
 

hippy

Technical Support
Staff member
Line 5, "b0 = pinsB & %00001111" is reading all the Port B pins, ignoring those pins not used for the switch input, and putting the result into the b0 variable. Because it is an AND (&) function, only bits which are a 1 in the mask on the right of the & (%00001111) are kept. Bit 7 is on the left, bit 0 is in the right.
 
Ah sorry, I think I might have misled you there. What I meant was I was still having trouble with my program, not problems attaching code to posts, which I've yet to try. Regarding the program and the reason for starting the thread, I'm proud to announce I got there in the end and I now have the solution I set out to achieve, and it is no small thanks to the knowledgeable souls on here for showing me the way. So once again, many thanks.

Terry
 
Top