Go Down

Topic: Binary counting using 12 LEDs (Read 19168 times) previous topic - next topic

littlewolf

Thank you very much
I did what you said but it still doesn't work.
I put the serial.println(sum); after
the end of the second if (end of else if in the above code)
and deleted the code you said.
I can assure you that my circuit is properly connected and it works
when I use either the keypad or LEDs separately.
Perhaps there is still something wrong in my code.
Waiting for your answer.
Regards

littlewolf

I forgot to say that I get zero if I press
any key or combinations of keys.

HazardsMind

What about a serial print of number1 and 2 when you press them?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

If this is not what you want then I give up.
Code: [Select]
#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];
        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< sum; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
      //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; // checks to see if
      // counter does past zero then set it back to zero
      digitalWrite(ledPin[counter], LOW);
      //delay(1000);

    } // end of decrement counter

  } // end of '*'

}//end of loop



My GitHub:
https://github.com/AndrewMascolo?tab=repositories

littlewolf

Thanks,
It's much better.
It shows integer numbers on the LEDs
Perhaps I cannot expect more since this
project was a nightmare.
Just wondering, has anyone done it before?
I mean, to control a binary counting with a keypad.
HazardsMind is right to give up. I understand he's had enough.
Imagine how I feel, with less experience in programming.
I guess I have to give up too, since there is no way to do it.
Thank you again for your help.
Regards

lloyddean

In other words if you don't know what you want, or how it's to work, how can we help you  program it or debug it.

It's just not possible.

See my earlier suggestion and don't just ignore it as an unnecessary  inconvenience it's the basis of programming.

AWOL

Quote
It shows integer numbers on the LEDs

You were expecting floating point, perhaps?
Imaginary numbers?

littlewolf


In other words if you don't know what you want, or how it's to work, how can we help you  program it or debug it.

It's just not possible.

Thank you for your reply.
I can assure you that I payed attention to all the messages for this topic
and I also clarified many times what I want.
Shall I do it again?
The keypad is in decimal and the LEDs are in binary.
The 12 bits in binary gives 4096 in decimal.
I want to get two numbers and an operator(either increment or decrement)
at the time and to convert it to binary.
Therefore, I want to either count up or down in binary
by the value of the number entered (in decimal).
Since two numbers (in decimal) gives the maximum value of 99, there is
enough room for the counter to go up to 4096.
I want the LEDs to show the different combinations of binary numbers
according to the chosen value.
Hope it's clear.

littlewolf


Quote
It shows integer numbers on the LEDs

You were expecting floating point, perhaps?
Imaginary numbers?

No. I was expecting binary numbers.

AWOL

Quote
No. I was expecting binary numbers.

And what did you get?
(I'm sorry - I'm really struggling to see your problem here)

littlewolf

For example, if I press 2+3+#
I get the first 5 LEDs HIGH
Instead I want to count up by this value in binary

AWOL

#56
Apr 04, 2013, 11:16 am Last Edit: Apr 04, 2013, 11:19 am by AWOL Reason: 1
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
Code: [Select]
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"

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

littlewolf


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
Code: [Select]
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

littlewolf


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


Go Up