Binary counting using 12 LEDs

So you're saying instead of five LEDs lighting (unary), you want to see 101? (where 1 is a lit LED)

If that's all you want, then rip out this bit

for(counter=0; counter< sum; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
      //delay(1000);

    }

and substitute code based on "bitRead", "digitalWrite" and "sum"

Can we review what you have got working and what still needs to be done ?

Can you output the current number to the Serial monitor ? Forget the LEDs for now. Can you add to or subtract from the number using the keypad ?

If you have got that far, then converting the number to binary for display on the LEDs should be relatively easy, but let's solve one problem at a time.

AWOL: So you're saying instead of five LEDs lighting (unary), you want to see 101? (where 1 is a lit LED)

If that's all you want, then rip out this bit

for(counter=0; counter< sum; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
      //delay(1000);

    }



and substitute code based on "bitRead", "digitalWrite" and "sum"

Yes, that's exactly what I want

UKHeliBob: Can we review what you have got working and what still needs to be done ?

Can you output the current number to the Serial monitor ? Forget the LEDs for now. Can you add to or subtract from the number using the keypad ?

If you have got that far, then converting the number to binary for display on the LEDs should be relatively easy, but let's solve one problem at a time.

OK, I pressed some combinations of keys and this is the result: 1+ 2 12 1+ 9 19 2+ 7 27 1+ 1 11 11 1+2 12 12 1+0 10 10 0+0 0 0+5 5 5 0+2 2 2 1 0 This is what I got from my serial monitor. The numbers on the left are the combinations of two keys. On the right is the sum Single numbers on the left is what I see on the LEDs (the LEDs turned on) If I press a big number (bigger than 12) all the LEDs will turn on then, by pressing '*' many times I can subtract from the sum and consequently turn LEDs off, one at the time. If I start with: 0+1 then I can press '#' many times and turn LEDs on, one at the time Hope this explains everything

Does adding or subtracting the number entered on the keypad change the number to be displayed OK ?

Yes, that's right. I'm using the code posted by HazardsMind since the last code I posted is not able to turn on the LEDs.

substitute code based on "bitRead", "digitalWrite" and "sum"

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

Thanks, I changed the code following the last suggestion. This is the code:

#include 

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];
        Serial.print('\t');
        Serial.println(sum);
      }
      else {
        count++; 
      } 
    }


  }
  // end of else if

  //Serial.println(number);

  if (key == '#') // increment binary counter
  {
    Serial.println(sum++);
    //digitalWrite(13, HIGH);
    //delay (1000);
    for(counter=0; counter<= 12; counter++)
      {
       byte My_bits=bitRead(sum,counter); 
      digitalWrite(counter, 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, My_bits);
      //delay(1000);

    } // end of decrement counter

  } // end of '*'

}//end of loop


]

It shows some LEDs on but it's not in binary. At least I know how to count in binary. I guess I am very close to my solution.

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.

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 [u]or[/u] 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,