BCD Help for a newbie

Trying to write a sketch that would print the number displayed on wheel. Using the BCDWheel showed in the link.

any help would be greatly appreciated,

Thank you!

Four decades of 4-input(BCD) digital values - that will require 16 digital inputs, or some form of multiplexing, or an add-on digital device called a port expander.
Tell us where that value will be displayed - LCD, Serial Monitor, ???
Does that help?
Also, what Arduino are you using, that will determine input availability.

How do you plan to connect this thumbwheel switch to the Arduino? That will determine how to read and print the numbers.

One suggestion could be to use a pcf8575 chip to read the thumbwheel 8,4,2,1 pins for each digit. The C pins could all be connected to ground. This would only require 2 Arduino pins (SDA, SCL).

Another suggestion could be to connect the pins like a switch matrix. This would require 8 Arduino digital pins, and no extra chip, but it would also be necessary to attach 16 small diodes between the thumbwheel pins and the Arduino pins.

For now, serial monitor would be fine as I am trying to understand and learn from the code.
Using a Mega.

Thank you!

What have you tried so far?

Where are you stuck?

Using the digital pins.

Ok, if using 16 digital pins, no diodes or chip would be needed.

The common (C) pins would all be connected to ground. The 8,4,2,1 pins for the 4 digits would ask be connected to Mega digital input pins.

I suggest you make a 4x4 2D array in your code to hold the 16 pin numbers.

Haven't tried anything, looking for a jump start .

So that gives you enough pins to wire each switch individually.

But start by reading just one switch.

When you've got that working, extend to reading multiple switches.

Step by step is always a good way to approach these things!

Sounds like a plan, will give it a go. So treat each wheel as having for seperate switches??

So have you thought about it?

The page shows you what the switches do - so you've got to read that, and turn it into a number.

Do you know how to read a single switch?

Do you know how to send stuff over the serial?

If not then, again, go step-by-step ...

Ok, this is how you would define the array

const byte thumbWheelPins[4][4] = {
  //1,  2,  4,  8
  {22, 23, 24, 25}, //pins for digit 0
  {26, 27, 28, 29}, //pins for digit 1
  {30, 31, 32, 33}, //pins for digit 2
  {34, 35, 36, 37}  //pins for digit 3
};

That's exactly what it does have. :+1:

Please include in your plan some way to have a time-out period when the switch positions are NOT being changed. After the time-out, read the switch values. An alternative is to have a separate push button that is pressed AFTER all the switch settings have been made.

ok, going to try and come up with something and post it.
thanks

Ok, I will hide the following suggested code so that you are not tempted to look at it unless you get stuck!

example code
const byte thumbWheelPins[4][4] = {
  //1,  2,  4,  8
  {22, 23, 24, 25}, //pins for digit 0
  {26, 27, 28, 29}, //pins for digit 1
  {30, 31, 32, 33}, //pins for digit 2
  {34, 35, 36, 37}  //pins for digit 3
};

void setup() {
  Serial.begin(115200);
  for(byte digit=0; digit<4; digit++)
    for(byte pin=0; pin<4; pin++)
      pinMode(thumbWheelPins[digit][pin], INPUT_PULLUP);
}

void loop() {
  for(byte digit=0; digit<4; digit++) {
    byte d = 0;
    byte v = 1;
    for(byte pin=0; pin<4; pin++) {
      if(digitalRead(thumbWheelPins[digit][pin]) == LOW) d += v;
      v += v;
    }
    Serial.print(d);
  }
  Serial.println();
}
  

I don't own a Mega and would not choose to! Also I would not use direct port manipulation unless there was a compelling reason do to it, because it makes the code difficult to understand and difficult to port to any other model of Arduino or compatible.

1 Like

Are you referring to using the pin numbers versus "int Digit1 = 22"

haha, i was using D1B1, D1B2, etc at least im thinking like you guys.
thanks!

Ok, here is my first draft. Please keep in mind, coding is not my forte.
I've decided to use only one wheel for now, to keep thing simple for testing.
Step by step, right?
btw PaulRB still have not looked at your code yet.


int D1B1 = 22;
int D1B2 = 23;
int D1B3 = 24;
int D1B3 = 25;


const byte thumbWheelPins[1][4] = {
  //1,  2,  4,  8
  {D1B1, D1B2, D1B3, D1B4}, //pins for digit 0
 
};



void setup() {
 

Serial.begin(9600);

pinMode(22, INPUT);
pinMode(23, INPUT); 
pinMode(24, INPUT); 
pinMode(25, INPUT); 


}

void loop() {

digits[1] = {0};
for (int i=0; i<4; i++){
   for (int j=0; j<4; j++){
       byte bit = digitalRead(thumbWheelPins[i][j];
       digit[i] |= (bit << (3-j));
   }
}


  Serial.print ("Wheel 1: ");
  Serial.println (thumbWheelPins, DEC);

  

}