Binary counting using 12 LEDs

It shows some LEDs on but it's not in binary.

Ok so then what is it outputting?

Oh, you need to add +2 to the counter, because your leds start at pin 2. So its, digitalWrite(counter + 2, My_bits);

AWOL:

It shows some LEDs on but it's not in binary.

But you're not going to describe any of it.

OK, good luck with the rest of your project.

Of course I'm going to let you know about my progress after so much trouble. HazardsMind is right, so I added 2 to the counter. Now I can insert any number in binary up to 99 then I can count up by one, by pressing '#' repeatedly. However, I cannot count down, or subtract in binary. Addition only works when adding 1, one at the time That's it.

This is wrong

for(counter; counter > sum; counter-- )

you still need to count up to 12, because your reading the position of the bits in the sum. Check out this link. http://wiring.org.co/reference/bitRead_.html

You are right.
Thanks
What about the two remaining problems?
Counting up by any number and
Counting down
In other words I want to be able to make
binary addition and subtraction
Regards

The Arduino is quite capable of performing addition and subtraction.

If you've got the display of any given binary number between 0 and 4095 solved, then isolating your remaining problems should be trivial.

Counting up by any number and
Counting down

Ok, so this would mean you need to save the old number and just add or subtract it from the new number. Once you have the sum or difference of the two, THEN you turn your LEDs on/off.

I suggest you make this, its own function and just pass in the sum or difference. You do know how to make functions that allow data to be inserted, right?

for(counter=0; counter < 12; counter++)
      {
       byte My_bits=bitRead(sum,counter); 
      digitalWrite(counter, My_bits);
      //delay(1000);

      }
  for(counter=0; counter<= 12; counter++)

Probably doing no harm, but how many LEDs have you got, 12 or 13 ?

    for(counter; counter > sum; counter-- )

What is the value of counter and sum when this loop executes ?

You could turn the display of the binary representation of your sum variable into a function and call it when necessary. Get it working once and use it many times.

for(counter=0; counter<= 12; counter++) Probably doing no harm, but how many LEDs have you got, 12 or 13 ?

I caught that and fixed the code I gave him. Now did he see the fix, I dont know, most likely not.

Hi guys,
Thank you for the last replies.
Just wondering what is missing now
To get a number displayed on the LEDs I usually
get that number plus 1
So I modified
sum=(number[0] *10)+number[1];
to
sum=(number[0] *10)+number[1]-1;
Now I get the right answer on LEDs but the wrong
one on the serial monitor(since new sum is sum minus 1)
I tried a different combination of:
counter+2 or counter +1 but it only works properly
when I use counter+2.
When using counter+1 I usually get sum minus half

but it only works properly when I use counter+2.

If, instead of "counter", you used the word "outputPinIndex" or "binaryPlace", would that make it easier to understand?

If you do

sum = aNumber;

then Serial.print the number and output it to the LEDs in binary and get a different number displayed on each, then obviously one or other is wrong and I know which one my bet would be on. Do I remember correctly that your bit to pin conversion needed an offset of 2 because your LED pin numbers start at 2 ?

Have you tried

Serial.println(sum, BIN);

to get a binary output to the serial monitor ?

Have you written a function to input the number in binary to the LEDs as I suggested in an earlier post ? That way you only have one place to look for the problem and put it right and you can test the function in a very simple program before you commit it to the main one,

I tried now what you said but the results are the same:
the correct answer on LEDs and sum-1 on serial monitor.
This is the code:

#include <Keypad.h>

int ledPin[12]={
  13,12,11,10,9,8,7,6,5,4,3,2};

int number[]={0,0};
int count = 0;
int keyPressed=0;
int counter=0;
int sum=0;
char firstKey;
char secondKey;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  
  {'4','5','6'},
  
  {'7','8','9'},
  
  {'*','0','#'}
};


byte rowPins[ROWS] = {31, 33, 35, 37}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {39, 41, 43}; //connect to the column pinouts of the keypad

Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);

  for(counter=0; counter<12; counter++)
  {  
    pinMode(ledPin[counter], OUTPUT);      // sets the digital pins as output
  }
}

void loop(){

  char key = customKeypad.getKey();
  /*if (key) {
   Serial.println(key);
   }*/
  if (key) { // blocking from anything not needed
    if (key != '*' && key !='#'){
      Serial.print(key);
      Serial.print(" ");
      number[count] = key - '0';
      if(count == 1) {
        count = 0; 
        sum=(number[0] *10)+number[1]-1;
        Serial.print('\t');
        Serial.println(sum,BIN);
      }
      else {
        count++; 
      } 
    }


  }
  // end of else if

  //Serial.println(number);

  if (key == '#') // increment binary counter
  {
    Serial.println(sum++);
    
    for(counter=0; counter<= 12; counter++)
      {
       byte My_bits=bitRead(sum,counter); 
      digitalWrite(counter+2, My_bits);
      //delay(1000);

      } // end of increment counter
  } // end of '#'


  if (key == '*') // decrement binary counter
  {
    Serial.println(sum--);
    for(counter; counter > sum; counter-- )
    {
      if (counter < 0) counter = 0; 
      byte My_bits = bitRead(sum,counter);
      digitalWrite(counter+2, My_bits);
      //delay(1000);

    } // end of decrement counter

  } // end of '*'

}//end of loop

I modified it a little, it should work now. Also now you can set the number of digits you want to enter, with “limit”: default is 1.

#include <Keypad.h>

int ledPin[12]={
  13,12,11,10,9,8,7,6,5,4,3,2};

int number[]={
  0,0};
int limit = 1; //  1= 00 - 99, 2 = 000 - 999, 3 = 0000 - 4095 New variable, THE NUMBER OF DIGITS YOU NEED TO ENTER.
int count = 0;

int counter=0;
My_number = 0; // New variable
Final_Num = 0;  // New variable


const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  
  {'4','5','6'},
  
  {'7','8','9'},
  
  {'*','0','#'}
};


byte rowPins[ROWS] = {31, 33, 35, 37}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {39, 41, 43}; //connect to the column pinouts of the keypad

Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);

  for(counter=0; counter<12; counter++)
  {  
    pinMode(ledPin[counter], OUTPUT);      // sets the digital pins as output
  }
}

void loop(){

  char key = customKeypad.getKey();
  /*if (key) {
   Serial.println(key);
   }*/
  if (key) { // blocking from anything not needed
    if (key != '*' && key !='#'){
      Serial.print(key);
      Serial.print(" ");
      number[count] = key - '0';
      if(count == limit) {
        count = 0;

      if( limit == 3)  My_number = (number[0] *1000) + (number[1] * 100) + (number[2] * 10) + number[3];
      else if( limit == 2) My_number = (number[0] *100) + (number[1] * 10) + number[2];
      else My_number = (number[0] *10) + number[1] ;

        Serial.print('\t');
        Serial.println(My_number);
      }
      else {
        count++; 
      } 
    }


  }
  // end of else if

  //Serial.println(number);

  if (key == '#') // increment binary counter
  {
    Final_Num += My_number;
    if(Final_Num > 4095) { 
          Final_Num = 4095;
     }
    Serial.println(Final_Num);
    //digitalWrite(13, HIGH);
    //delay (1000);
    for(counter = 0; counter < 12; counter++)
      {
       byte My_bits=bitRead(Final_Num,counter); 
      digitalWrite(counter + 2, My_bits);
      //delay(1000);

      } // end of increment counter
  } // end of '#'


  if (key == '*') // decrement binary counter
  {
    Final_Num -= My_number;
    if(Final_Num < 0) { 
          Final_Num = 0;
     }
    Serial.println(Final_Num);
    for(counter = 0; counter < 12; counter++)
    { 
      byte My_bits = bitRead(Final_Num,counter);
      digitalWrite(counter + 2, My_bits);
      //delay(1000);

    } // end of decrement counter

  } // end of '*'

}//end of loop

These are both ints

My_number = 0; // New variable
Final_Num = 0; // New variable

fixed

int My_number = 0; // New variable
int Final_Num = 0;  // New variable

I knew I forgot something.

Hi guys, I want to thank everyone who helped me sort out this code. Now is working and I can do the things I wanted to do: a basic binary calculator with addition and subtraction and with the display of the results on LEDs. I've taken so long to answer because I wanted to make sure it works, so I tested it many times. However, may I ask you the last question? There is still something I don't understand. Sometimes it doesn't take all the input (all the keys pressed) and so affecting the results and I'm trying to figure out this strange behavior. Is there anything to do with the delay? Thanks guys for everything. Regards

What does it not take? As for right now, it is set to only take 00 - 99, as set by the limit. Since you only have 12 LEDs, you can only go as high as 4095, anything higher and it will automatically make it 4095. Unless you want rollover in which case you would need to add more code.

It's not about rollover. Imagine I insert 12, then 12 again, then I take a few seconds break. It stops everything and I have to start again from zero since it doesn't take any more input. Perhaps the little break might stop the key input. I don't think this is a big problem. Thanks for asking.

Here is why.

int number[]={ 0,0};

change it to,

int number[]={
  0,0,0,0};

You might be able to modify the code a little more, to where you can enter any number and have it count. So right now if limit is 1, you need to enter 2 digits. 0 2 => 2 or 1 9=> 19. But you can modify it so that if you press 5 # it should just add 5, without needing to enter 0 5. Its something you should be able to do on your own.

Thank you again I'm thinking already of a new challenging project. See you then. Regards