8 parallel BCD inputs / if BCD number ## output ## comes high

Hello guys!

I'm looking for a simple code that reads 8 inputs and converts those inputs to decimal numbers and react on the number with an ouptut high

I0 activate reading
I1 BCD 1
I2 BCD 2
I3 BCD 4
I4 BCD 8
I5 BCD 10
I6 BCD 20
I6 BCD 40
I7 BCD 80

something like a simple if I1 is high add1 if I2 is high add 2 and so on

and for output

if BCD number is 10 -> output 8 high (latch until reboot or a new BCD number shows up)

the BCD numbers come from a CNC machine from the 1980s and is convertig M commands to BCD and I want to add a simple extension so you can use all M commands to do things.,..

oh and is a thruthtable possible ? highest M umber is 99 lowest is 1
serial monitor output of the BCD numbers would be fine - but only if it changes, no flooding :slight_smile:

can someone draw me an example ? that should be not that much of code but I dont know where to start

my lack of programming needs to be fixed - this is where I start :)))

all the best

What about

num = I1*1 + I2*2 + I4*4 ... +I8*80;

If you can spend an 8 bit port and connect the inputs to that port in the right sequence then you can read the result without any further calculation. The same for an output port.

num = PINXin;
PORTXout = num;

It's BCD, so yes, you can grab all the bits, but then you need a simple conversion from a byte holding two BCD digits to an integer be it byte or int or whatever, which would be 0..99.

int theNumber =   (10 * ((in & 0xf0) / 16)) +  (in & 0xf);

if in was the byte you collected.

a7

For a digital value indeed a conversion is required.
Not required is a conversion from byte into int.

Right. Any integer type that will hold a value 0..99 can be used, like I said.

a7

oh wow that was fast! thank you!

ok can we start a bit earlier?

I know what setup and void is but then things get complicated,...
I can read what code does and kinda understand it nut cant write it - so basicly a total n00b

before I start to copy together and you guys need to correct it several times, can someone PLEASE stitch together the full program?

I know thats your free time guys! and I also know that this here is no programming service, but in sake of time and energy from you guys telling me several times what I did wrong I think its faster if someone can PLEASE stitch it together, and I try to modify it by myself,..

sorry for that kinda question, I know you have beter things to do and I could now invest a couple hours to get where I'm able but you know - its easier to understand for me on an example..

this is what i could make :slight_smile:


pinMode(0, INPUT);    // signal comming in
pinMode(1, INPUT);    // signal 1
pinMode(2, INPUT);    // signal 2
pinMode(3, INPUT);    // signal 4
pinMode(4, INPUT);    // signal 8
pinMode(5, INPUT);    // signal 10
pinMode(6, INPUT);    // signal 20
pinMode(7, INPUT);    // signal 40
pinMode(8, INPUT);    // signal 80

pinMode(9, OUTPUT);    // output for Mxx SIGNAL

int BCDnum = 0; // for incoming BCD data

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}


void loop() {


 // send data only when you receive data:

int BCDnum = I1*1 + I2*2 + I3*4 +I4*8 + I5*10 + I6*20 +I7*40 +I8*80;



 if (BCDnum > 0 && I0 >= 1) {

    // say what you got:
    Serial.print("I received: ");
    Serial.println(BCDnum, DEC);

if(BCDnum =21){

digitalWrite(9, HIGH); // sets the digital pin 13 on

  }



}

ok, I got a bit further, but it is still not working

I should not use I0 and I1 when working with serial right?

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);

pinMode(0, INPUT);    // BCD number coming in
pinMode(1, INPUT);    // BCD number 
pinMode(2, INPUT);    // BCD number 
pinMode(3, INPUT);    // BCD number 
pinMode(4, INPUT);    // BCD number 
pinMode(5, INPUT);    // BCD number 
pinMode(6, INPUT);    // BCD number 
pinMode(7, INPUT);    // BCD number 
pinMode(8, INPUT);    // BCD number 

pinMode(11, OUTPUT);    // sets the digital pin 9 as output

}

void loop() {

  int I0;
  I0 = digitalRead(2);
  int I1;
  I0 = digitalRead(3);
  int I2;
  I0 = digitalRead(4);
  int I3;
  I0 = digitalRead(5);
  int I4;
  I0 = digitalRead(6);
  int I5;
  I0 = digitalRead(7);
  int I6;
  I0 = digitalRead(8);
  int I7;
  I0 = digitalRead(9);
  int I8;
  I0 = digitalRead(10);
  int BCDnum = 0;

  if (I0 = HIGH){

  int BCDnum = I1*1 + I2*2 + I3*4 + I4*8 + I5*10 + I6*20 + I7*40 + I8*80; //adding the BCD number up)

}
if (I0 = HIGH && BCDnum > 0 && BCDnum < 100) {

  Serial.print ("BCD Number: ");
  Serial.println ("BCDnum");

  if (BCDnum = 21){
    digitalWrite(10,HIGH);
  }
  
}

  
  

  
  // put your main code here, to run repeatedly:

}

You should see the bug occurring with all assignments.

yes, seen that right after the post

but there must be another misstake in the code

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);

pinMode(2, INPUT);    // BCD number coming in
pinMode(3, INPUT);    // BCD number 
pinMode(4, INPUT);    // BCD number 
pinMode(5, INPUT);    // BCD number 
pinMode(6, INPUT);    // BCD number 
pinMode(7, INPUT);    // BCD number 
pinMode(8, INPUT);    // BCD number 
pinMode(9, INPUT);    // BCD number 
pinMode(10, INPUT);    // BCD number 

pinMode(11, OUTPUT);    // sets the digital pin 9 as output

}

void loop() {

  int I0;
  I0 = digitalRead(2);
  int I1;
  I1 = digitalRead(3);
  int I2;
  I2 = digitalRead(4);
  int I3;
  I3 = digitalRead(5);
  int I4;
  I4 = digitalRead(6);
  int I5;
  I5 = digitalRead(7);
  int I6;
  I6 = digitalRead(8);
  int I7;
  I7 = digitalRead(9);
  int I8;
  I8 = digitalRead(10);
  
  int BCDnum = 0;

  if (I0 = HIGH){

  int BCDnum = I1*1 + I2*2 + I3*4 + I4*8 + I5*10 + I6*20 + I7*40 + I8*80; //adding the BCD number up)

}
if (I0 = HIGH && BCDnum > 0 && BCDnum < 100) {

  Serial.print ("BCD Number: ");
  Serial.println (78, DEC);

  if (BCDnum = 21){
    digitalWrite(10,HIGH);
  }
  
}

  Serial.print ("BCD Number: ");
  Serial.println (I0, DEC);

  delay(1000);

  
  

  
  // put your main code here, to run repeatedly:

}

Use '==' for testing equality. '=' is just an assignment.

And why you printing 78? Don't you want to print BCDnum?

Just guesses.

a7

You know the difference between a comparison and an assignment (= vs. ==)?

BTW your indentation is broken. Please do another Format in the IDE.

After you have fixed that you can run your code to find out whether it does what you want.

for testing, to see if the if statement comes true then it should print 78

thank you!

I dont get that

now i fixed something, so the serial shows the pin state now :slight_smile: but the if loops dont work yet - I will go on trying

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);

pinMode(2, INPUT);    // BCD number coming in
pinMode(3, INPUT);    // BCD number 
pinMode(4, INPUT);    // BCD number 
pinMode(5, INPUT);    // BCD number 
pinMode(6, INPUT);    // BCD number 
pinMode(7, INPUT);    // BCD number 
pinMode(8, INPUT);    // BCD number 
pinMode(9, INPUT);    // BCD number 
pinMode(10, INPUT);    // BCD number 

pinMode(11, OUTPUT);    // sets the digital pin 9 as output

}

void loop() {

  int I0;
  I0 = digitalRead(2);
  int I1;
  I1 = digitalRead(3);
  int I2;
  I2 = digitalRead(4);
  int I3;
  I3 = digitalRead(5);
  int I4;
  I4 = digitalRead(6);
  int I5;
  I5 = digitalRead(7);
  int I6;
  I6 = digitalRead(8);
  int I7;
  I7 = digitalRead(9);
  int I8;
  I8 = digitalRead(10);
  
  int BCDnum = 0;

  if (I0 == HIGH){

  int BCDnum = I1*1 + I2*2 + I3*4 + I4*8 + I5*10 + I6*20 + I7*40 + I8*80; //adding the BCD number up)

}
if (I0 == HIGH && BCDnum > 0 && BCDnum < 100) {

  Serial.print ("BCD Number: ");
  Serial.println (78, DEC);

  if (BCDnum = 21){
    digitalWrite(10,HIGH);
  }
  
}

  Serial.print ("BCD Number: ");
  Serial.print (BCDnum);
  Serial.print ("   I/Os: ");
  Serial.print (I0);
  Serial.print (I1);
  Serial.print (I2);
  Serial.print (I3);
  Serial.print (I4);
  Serial.print (I5);
  Serial.print (I6);
  Serial.print (I7);
  Serial.println (I8);





  delay(1000);

  
  

  
  // put your main code here, to run repeatedly:

}

You did it again.

I think if you turnt up the compiler warnings in the IDE preferences, this kinda thing would be flagged for your consideration.

a7

thanks,

oh nice tip - thank you!

warning: unused variable 'BCDnum'

Haha, right. So warnings are just that. Ignore the ones that you understand are irrelevant. Here it just calls attention to the mystery of why you make so much trouble with BCDnjm and then don't do anything with it… not even print it out. :wink:

a7

any idea whats wrong in the code

Yes and sorry I didn't spot it with all the excitement around comparing and assigning.

The

int BCDnum 

is a declaration of a variable only vbisible within the { } that encompasses it.

Place

int BCDnum;

outside the loop() function and lose the "int" where it is now inside the { }.

This a "variable scope" issue, a part of the C/C++ language to come to grips with one day soon.

Yikes! Sry sry sry, now I don't even see why it compiles, so obvsly I am thinking too little. The local declarations of all you I# variables should also be a problem.

Nap time. Again, sry.

a7

tried diffrent things,

dont know where to put the int BCDnum;

only thing that not works ist the varable BCDnum and the calculationg of it

  int BCDnum = 0;

  if (I0 == HIGH){

  int BCDnum = I1*1 + I2*2 + I3*4 + I4*8 + I5*10 + I6*20 + I7*40 + I8*80; //adding the BCD number up

}

can you pls put it where it belongs ?

Here you go.

Please look closely. I think this is working to your plan.

Homework: learn (wherever you do your learning) about variable scope in C/C++.

a7

oh wow it was just the int I used... thank you!!

the working code


void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);

pinMode(2, INPUT);    // BCD number coming in
pinMode(3, INPUT);    // BCD number 
pinMode(4, INPUT);    // BCD number 
pinMode(5, INPUT);    // BCD number 
pinMode(6, INPUT);    // BCD number 
pinMode(7, INPUT);    // BCD number 
pinMode(8, INPUT);    // BCD number 
pinMode(9, INPUT);    // BCD number 
pinMode(10, INPUT);    // BCD number 

pinMode(11, OUTPUT);    // sets the digital pin 9 as output

}

void loop() {

  int I0;
  I0 = digitalRead(2);
  int I1;
  I1 = digitalRead(3);
  int I2;
  I2 = digitalRead(4);
  int I3;
  I3 = digitalRead(5);
  int I4;
  I4 = digitalRead(6);
  int I5;
  I5 = digitalRead(7);
  int I6;
  I6 = digitalRead(8);
  int I7;
  I7 = digitalRead(9);
  int I8;
  I8 = digitalRead(10);
  
  int BCDnum = 0;

  if (I0 == HIGH){

  BCDnum = I1*1 + I2*2 + I3*4 + I4*8 + I5*10 + I6*20 + I7*40 + I8*80; //adding the BCD number up

}


if (I0 == HIGH && BCDnum > 0 && BCDnum < 100) {

  Serial.print ("BCD Number: ");
  Serial.println (BCDnum, DEC);

  
  
}

if (I0 == HIGH && BCDnum == 20){
    digitalWrite(13,HIGH);
  }
  
else {

    digitalWrite(13,LOW);
  }
 
  
  // delay(1000);

 
  // put your main code here, to run repeatedly:

}