Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 05, 2013, 03:01:19 pm
Thank you again
I'm thinking already of a new challenging project.
See you then.
Regards
2  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 05, 2013, 08:46:13 am
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.
3  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 05, 2013, 08:26:42 am
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
4  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 05, 2013, 07:06:36 am
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:
#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
5  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 05, 2013, 03:00:34 am
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
6  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 09:25:54 am
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
7  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 09:06:42 am
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.
8  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 08:25:23 am
Thanks,
I changed the code following the last suggestion.
This is the code:

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];
        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.
9  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 08:00:07 am
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.
10  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 07:06:03 am
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

11  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 06:53:31 am
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:
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
12  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 04:05:35 am
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
13  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 03:50:49 am
Quote
It shows integer numbers on the LEDs
You were expecting floating point, perhaps?
Imaginary numbers?
No. I was expecting binary numbers.
14  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 03:49:00 am
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.
15  Using Arduino / Programming Questions / Re: Binary counting using 12 LEDs on: April 04, 2013, 03:08:46 am
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
Pages: [1] 2 3 4