Go Down

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

UKHeliBob

If you do
Code: [Select]
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
Code: [Select]
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,
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

littlewolf

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:
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]-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


HazardsMind

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.
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 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



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

HazardsMind

These are both ints
Quote
My_number = 0; // New variable
Final_Num = 0;  // New variable


fixed
Code: [Select]

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


I knew I forgot something.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

littlewolf

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

HazardsMind

#80
Apr 05, 2013, 03:31 pm Last Edit: Apr 05, 2013, 03:42 pm by HazardsMind Reason: 1
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

littlewolf

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.

HazardsMind

#82
Apr 05, 2013, 04:23 pm Last Edit: Apr 05, 2013, 04:33 pm by HazardsMind Reason: 1
Here is why.
Quote
int number[]={
  0,0};


change it to,
Code: [Select]
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

littlewolf

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

Go Up