Decade Thumb Wheel Switch Problem

Hi

I have 4 thumbwheel switches so that I can choose any number between 0000 and 9999 and
their output is in binary coded decimal.

I wish to read this binary number and use it to set the value of a variable called Pulses which will then drive a stepper motor driver

The ultimate aim is to be able to send a string of pulses to the stepper motor and the number of pulses can be anything between o and 9999

Driving the stepper is not a problem , i know how to do that, but assigning the variable 'pulses' with the number set by the switches has got me scratching my head

I could use a large arduino like the mega and just assign all the switches 16 outputs to 16 input pins , but I want to keep this as small as I can so obviously I need to read each decade one at a time so that I only need 4 input pins

I could use four , 1 of 4 line demultiplexers like the 74HC139 bringing the number of inputs to 4

Lets say I assign each of these 4 possible inputs to a variable 'units' , 'tens' , 'hundreds' , 'thousands'

Now is where I get stuck , I assume I need to

convert each of these 4 variables which are in binary to decimal numbers

and then I need to join the 4 seperate decimal numbers up to make a 4 decade single number

How do I code to do this ? or is there a better way

Regards
Don

I think I replaced my coffee with a Cotion of Ponfusion 'cause i'm not getting it...

Could you perhaps post a schematic of the setup your thinking off? And perhaps a link for the tumbwheels so we can check the output?

Hi C-F-K

Thanks for replying

The switches are similar to this http://uk.rs-online.com/web/p/thumbwheel-switches/1614802/

they have 5 terminals 1 is the common input the other four are outputs for 1,2,4,8

by selecting a number on the switch 0-9 it gives out its binary equivalent
by using 4 of the switches I can get the BCD output of any number from 0 to 9999
but that would be 16 binary outputs requiring 16 input pins on the arduino

my plan was to use multiplexers but I now realize I already have some quad tri state buffers
so will use those instead , a bit like this ( see attachment )

By putting all the chips into tristate mode except the one I want to read I can read each of the 4 switches in turn but only use 4 input pins rather than reading them all at once and using 16 pins

What I am asking for help on is....

How do I code it to take the number on the 4 arduino inputs (that is in 4 bit binary) and store it into a variable ( lets say its called units ) but store it as a decimal number
then when I have assigned the values to the 4 variables 'unit' , 'tens' , 'hundreds' , 'thousands'
join those 4 variables into one variable that holds the complete number

For Example

I read in the units switch as b0100 and store it in the variable 'units' 4
I read in the tens switch as b1000 and store it in the variable 'tens' as 8
I read in the hundred switch as b0110 0100 and store it in the variable 'hundreds' as 6
I read in the thousands switch as 1001 and store it in the variable 'thousands' as 9

then store all of the four variables we just assigned combined into a new variable
called 'steps' as 9684

I then know how to use that variable to output that many steps to a stepper motor

Incidentally , if you are interested the project is to use a stepper motor to issue
small known quantities of solder paste from a hypodermic syringe for when I am
using SMD devices

The 4 switches set the amount of solder delivered which I set dependant on the
size of component I am mounting , just a few steps of the stepper motor for 0603s
and more steps for 0805s or 1206

Don

Schematic.pdf (12 KB)

Hi,
You can use 4 inputs and 4 outputs to read 4 BCD switches.
With diode switching as in the attached diagram, you read 4 input pins, and assign 1 to any that are low, when you pull the appropriate output low on each switch.

Tom... :slight_smile:

Adding it all together is as simple as

steps = (units) + (tens* 10) + (hundreds* 100) + (thousands * 1000);

TomGeorge:
Hi,
You can use 4 inputs and 4 outputs to read 4 BCD switches.
With diode switching as in the attached diagram, you read 4 input pins, and assign 1 to any that are low, when you pull the appropriate output low on each switch.

Tom... :slight_smile:

Thats neat little circuit , I never thought of that

Well thats sorted the switching out .

and C=F-K has told me how to add the 4 variables into a single number

All I need to find out now is if there is an easier way to convert the 4 bit binary number into a decimal
than using a loop with something like

if units = 1 and tens = 0 and 100s = 0 and thousands = 0 then steps = 1
units = and tens = 1 and 100s = 0 and thousands = 0 then steps = 2
units = 1 and tens = 1 and 100s = 0 and thousands = 0 then steps = 3
etc
etc

Don

C-F-K:
Adding it all together is as simple as

steps = (units) + (tens* 10) + (hundreds* 100) + (thousands * 1000);

I felt quite silly after reading that . It seems so obvious now

lets put it down as a age thing

Don

DonRecardo:
All I need to find out now is if there is an easier way to convert the 4 bit binary number into a decimal
than using a loop with something like

Do you have a piece of code where you read the thumbwheels? It's much easier to select the right approach...

C-F-K:
Do you have a piece of code where you read the thumbwheels? It's much easier to select the right approach...

Not yet I dont but after reading Tom Georges post I assumed it would be on the lines of

Send a low output to the units switch and read the 4 inputs one at a time into 4 variables for bits
o then 1 then 2 then 3

these 4 variables then need to to be made into a decimal number which I store in the variable 'units'

send the output to the units switch high again and repeat the proceedure for the tens switch , then hundreds and lastly thousands

then use your code to make one single 4 digit decimal number from the 4 single decimal numbers stored in units tens hundreds and thousands

lastly store this number in the variable 'steps' which is what I use to control the output to my stepper driver

Don

DonRecardo:
Not yet I dont but after reading Tom Georges post I assumed it would be on the lines of

Send a low output to the units switch and read the 4 inputs one at a time into 4 variables for bits
o then 1 then 2 then 3

these 4 variables then need to to be made into a decimal number which I store in the variable 'units'

send the output to the units switch high again and repeat the proceedure for the tens switch , then hundreds and lastly thousands

then use your code to make one single 4 digit decimal number from the 4 single decimal numbers stored in units tens hundreds and thousands

lastly store this number in the variable 'steps' which is what I use to control the output to my stepper driver

Don

Wow, that's complicated.

I was thinking something more like:

Example:

     1  NO
     2  YES     2
     4  YES     4
     8  NO
   
    10  NO
    20  YES    20
    40  NO
    80  NO
  
   100  NO
   200  NO
   400  YES   400
   800  NO
 
  1000  YES  1000
  2000  NO
  4000  NO
  8000  NO
  
      Total  1426  <-- this is the number we want

In other words:
Each output is "worth" a certain amount, from 1 to 8000. (Think of them as bills or coins of different values.)
You treat each output as either a "yes" or a "no". (I don't know whether your "yes" is a HIGH or LOW, so you have to figure this piece out yourself.) Then you add together the total "value" of all the "yes" inputs.
Really, it's no more complicated than counting the cash in your wallet.

odometer:
Wow, that's complicated.

In other words:
Each output is "worth" a certain amount, from 1 to 8000. (Think of them as bills or coins of different values.)
You treat each output as either a "yes" or a "no". (I don't know whether your "yes" is a HIGH or LOW, so you have to figure this piece out yourself.) Then you add together the total "value" of all the "yes" inputs.
Really, it's no more complicated than counting the cash in your wallet.

Thats another great idea . And it makes sense to me which is always useful

So If I say to start with the var 'units' = 0 and assume that I am working on active low
if the first bit is not high then units = units + 1
if the second bit is not high then units = units + 2
if the third bit is not high then units = units + 4
if the fourth bit is not high then units = units + 8

and of course at this point the var units now hold the number I want and in the right decimal format

Thats really neat

I find Forums so instructive
I sit here at home trying to work the answer out but once you come up with something you get tunnel vision
and cant see any other option, but asking on here you get so many more great points of view

Thanks to every one that chipped in

Don