Go Down

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

UKHeliBob

Does adding or subtracting the number entered on the keypad change the number to be displayed OK ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

littlewolf

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.

HazardsMind

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

littlewolf

#63
Apr 04, 2013, 03:25 pm Last Edit: Apr 04, 2013, 03:28 pm by littlewolf Reason: 1
Thanks,
I changed the code following the last suggestion.
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];
       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.

AWOL

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

HazardsMind

#65
Apr 04, 2013, 03:32 pm Last Edit: Apr 04, 2013, 03:36 pm by HazardsMind Reason: 1
Quote
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);
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

littlewolf


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

HazardsMind

This is wrong
Quote
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
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

littlewolf

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

AWOL

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.

HazardsMind

#70
Apr 04, 2013, 04:56 pm Last Edit: Apr 04, 2013, 05:09 pm by HazardsMind Reason: 1
Quote
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?
Code: [Select]
for(counter=0; counter < 12; counter++)
      {
       byte My_bits=bitRead(sum,counter);
      digitalWrite(counter, My_bits);
      //delay(1000);

      }

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

UKHeliBob

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

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

HazardsMind

Quote
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

littlewolf

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

AWOL

Quote
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?

Go Up