Binary counting using 12 LEDs

I got it to work on my end, and the way I did it was just using regular ints, however I think you need it in HEX, but anyways here is my way.

What you need to do is look at the incoming char and see if it does not equal '#' AND '*', and then store it in another variable.
Next convert that variable into an int with "var = (Key - '0')"
Now you press the # or * and using if statements for each, you either add or subtract the variable and put the result in a new variable.

Note I have a 4x4 keypad, and I used my 'A' key to clear my variables back to zero. I suggest you have something similar, otherwise you will need to set the final result back to 0 manually. OR, you can just hold the value and adjust it as you please.

I really don't know any other way to explain how to do this, other than just giving you the code. This should be clear enough to understand and get it to work.

Hi guys,
I did my best and modified my code following your suggestions.
Now this is my code:

/* Binary counting using 12 LEDs and a keypad to control 
the increasing or decreasing rate by the value 
of the key pressed*/

#include <Keypad.h>
int i;
int x; // variable to be used for the
       // storage of the last sequence
int ledPin[12]={13,12,11,10,9,8,7,6,5,4,3,2};
int number;
int counter;


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(){
  
   for(i=0; i<12; i++)
   {  
  pinMode(ledPin[i], OUTPUT);      // sets the digital pins as output
   }
}

void loop(){
  
  char key = customKeypad.getKey();
  
  if (key != '*' && key !='#'){
    
    number = key - '0' ;
    
    char key = customKeypad.getKey();
    
    if (key = '#') // increment binary counter
    {
     for(counter = 0; counter < 4096; counter = counter + number){
    
      digitalWrite(ledPin[i], HIGH);
      delay (1000);
    }
    }
    
    else if (key = '*') // decrement binary counter
    {
      for(counter = 0; counter < 4096; counter = counter - number ){
      digitalWrite(ledPin[i], HIGH);
      delay (1000);
      }
      
    }
    
  } //end of if
   
 }

It now compiles without errors but I feel there is still something missing.
Can you have a look please?
Thanks again for your suggestions.
Regards

I would like to give to key '0' on keypad the value 10.
Also, I'm not sure that I should state the most and least significant bits in my code
and how to memorize the value in binary of the last sequence.
It would be interesting if I could stop the counter (with LEDs on HIGH)
when pressed any key
until the start of the new counter, so I can prove that the last sequence
plus or minus the new key pressed adds up to the new sequence.

There are still issues in your code, but at the moment, it looks like you want to enter a number and then count up to it. So for that part, I would get rid of the 4096 and replace that with "number" and just have counter increment or decrement to it.

Also digitalWrite(ledpin*, HIGH) does not do anything right now, because it does not know what "i" is. Change i to counter.*
Let the dissection begin.
```

  • void loop(){
     
      char key = customKeypad.getKey();
      if(key) {  //blocking from anything not needed.

if (key != '' && key !='#'){
      number = key - '0' ;
  } // end of sort
   
  if (key == '#') // increment binary counter
  {
    for(counter ; counter < number; counter++){ // Set counter = 0, at the top of code.
      digitalWrite(ledPin[counter], HIGH);
      delay (1000);
      } // end of inc counter
  } //end of '#'
   
  if (key == '
') // decrement binary counter
  {
      for(counter; counter > number; 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 dec counter
  } // end of '
} //end of if(key)
} // end of loop

```
Now, you only have 12 LEDs so why not enter 2 numbers and then decide to dec or inc them? Enter 1 and 2 to make 12, or 1 and 0 to get 10. It will be a little more coding but, give it a try. With these counters, the way you have them set up, you don't particularly need to you HEX anymore.

HazardsMind:
Now, you only have 12 LEDs so why not enter 2 numbers and then decide to dec or inc them? Enter 1 and 2 to make 12, or 1 and 0 to get 10. It will be a little more coding but, give it a try. With these counters, the way you have them set up, you don't particularly need to you HEX anymore.

This is a great idea and seems much simpler.
Perhaps there is no more need to store the last LEDs configuration.
Using all the numbers from 0 to 99 seems more interesting.
Thank you for your help.
Hope I can do it.
Regards

Hi guys,
Sorry to bother you again but it still doesn't work.
This is my code:

/* Binary counting using 12 LEDs and a keypad to control 
the increasing or decreasing rate by the value 
of the key pressed*/

#include <Keypad.h>

int ledPin[12]={13,12,11,10,9,8,7,6,5,4,3,2};
int number=0;
int keyPressed=0;
int counter=0;
char firstKey;
long previousMillis = 0;
long interval = 1000;

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
   keyPressed= keyPressed+1;
   if (key != '*' && key !='#'){
    
    number =key - '0' ; // convert key to its value
  } // end of sort
   if (keyPressed ==1){
     firstKey=key;
   }
   if ( keyPressed==2){
  Serial.println(firstKey);
 
  //Serial.println(number);  
    
    if (key == '#') // increment binary counter
    {
       Serial.println("i"+firstKey);
       //digitalWrite(13, HIGH);
       //delay (1000);
     for(counter=0; counter< number; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
       unsigned long currentMillis = millis();
       if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      
     } // end of increment counter
    } // end of '#'
    
    
    if (key == '*') // decrement binary counter
    {
      for(counter; counter < number; counter-- ){
        if (counter < 0) counter = 0; // checks to see if
        // counter does past zero then set it back to zero
      digitalWrite(ledPin[counter], LOW);
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      } // end of decrement counter
      
    } // end of '*'
    keyPressed=0;
   } // end of keycount
  } //end of if(key)
   
 } // end of loop

It only works when I insert the first input then, nothing.
I mean that it turns all the LEDs on (the number key pressed)
and keeps them on.
What about the binary counting?
Regards

Any suggestions...?

Try this.
http://arduino.cc/forum/index.php/topic,137352.30.html

This is posted with honest intent, not to criticize or discourage.

I posted some code earlier in an attempt to help, but then upon rereading your requirements decided that your requirements are ambiguous and deleted the post.

My suggestion (which you've seen stated here by others more than once) is to CLEARLY define your requirements.

Post them, accept feed back, modify and repost the requirements and looping until they are completely stated and can be coded from.

Without good documented requirements you haven't much of a chance in completing your task as you don't even know what it is you're trying to accomplish let alone getting useful assistance for someone who only thinks they know what you want.

I'm listening ...

Concerning your current code the following snip-it, occurring twice, does nothing

unsigned long currentMillis = millis();
if ( (currentMillis - previousMillis) > interval )
{
    previousMillis = currentMillis;
}

If a multiple association condition test succeeds then you may safely use 'else' to exclude additional tests.

		if ( keyPressed == 1 )
		{
			...
		}
		else if ( keyPressed == 2 )
		{
			... 

			if ( key == '#' )
			{
				... 
			}
			else if ( key == '*' )
			{
				... 
			}

			... 
		}

lloyddean:
My suggestion (which you've seen stated here by others more than once) is to CLEARLY define your requirements.

The reason I'm trying to use "millis" is because when using "delay" the keypad
reads the input only once then does nothing.
I am trying to use the keypad to control the binary counting on LEDs.
There are number keys (0,1,2,3,4,5,6,7,8,9) and operator keys (* and #).
I want to use the operator keys to either increase (pressing #) or decrease
(pressing *) the binary counting by the value of the number keys pressed.
Perhaps it would be nice to read two number keys at the time (going up to 99)
followed by an operator key.
To make it even more simpler:
2+5+'#' would count up by 25 (in binary)
I will try your last suggestion and also "HasardsMind's" suggestion.
In fact I'm working now on them.
I'll let you know about my progress.
Regards

I think I made the matter worse.
The topic suggested by "HazardsMind" is not very useful.
I changed the if statements as suggested by "lloiddean"
and I think it should be fine. Then, I tried to add the first two keys
together and to store the result into the "sum".
Now, I get instead very big numbers instead (for example 9+9= 114 ).
The LEDs don't blink any more and I am not very sure about the loop
for getting the third key.
This is my code:

/* Binary counting using 12 LEDs and a keypad to control 
the increasing or decreasing rate by the value 
of the key pressed*/

#include <Keypad.h>

int ledPin[12]={13,12,11,10,9,8,7,6,5,4,3,2};
int number=0;
int keyPressed=0;
int counter=0;
int sum=0;
char firstKey;
char secondKey;
long previousMillis = 0;
long interval = 1000;

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
   keyPressed= keyPressed+1;
   if (key != '*' && key !='#'){
    
    number =key - '0' ; // convert key to its value
  } // end of sort
   if (keyPressed ==1){
     firstKey=key;
   }
  else if ( keyPressed==2){
    secondKey=key;
    sum=firstKey+secondKey;
  Serial.println(sum);
 
  //Serial.println(number); 
  if (keyPressed==3){
   keyPressed=key;  // I'm not sure about this loop
    
    if (key == '#') // increment binary counter
    {
       Serial.println("i"+firstKey);
       //digitalWrite(13, HIGH);
       //delay (1000);
     for(counter=0; counter< number; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
       unsigned long currentMillis = millis();
       if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      
     } // end of increment counter
    } // end of '#'
    
    
    else if (key == '*') // decrement binary counter
    {
      for(counter; counter < number; counter-- ){
        if (counter < 0) counter = 0; // checks to see if
        // counter does past zero then set it back to zero
      digitalWrite(ledPin[counter], LOW);
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      } // end of decrement counter
      
    } // end of '*'
    } // end of keyPressed == 3
    keyPressed=0;
   } // end of keycount
  } //end of if(key)
   
 } // end of loop

littlewolf:
Now, I get instead very big numbers instead (for example 9+9= 114 ).

Guess what the ASCII value of '9' is? 57. In

 sum=firstKey+secondKey;

you are adding the characters ('0'..'9'), not the numbers (0..9).

spatula:
Guess what the ASCII value of '9' is? 57. In

 sum=firstKey+secondKey;

you are adding the characters ('0'..'9'), not the numbers (0..9).

You are right.
I managed to do the code for addition.

#include <Keypad.h>

int ledPin[12]={13,12,11,10,9,8,7,6,5,4,3,2};
int number1=0;
int number2=0;
int keyPressed=0;
int counter=0;
int sum=0;
char firstKey;
char secondKey;
long previousMillis = 0;
long interval = 1000;

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
   keyPressed= keyPressed+1;
   if (key != '*' && key !='#'){
    
  // number =key - '0' ; // convert key to its value
  } // end of sort
   if (keyPressed ==1){
     number1= key - '0';
     firstKey=key;
   }
  else if ( keyPressed==2){
    number2= key - '0';
    secondKey=key;
    sum=(number1 *10)+number2;
  Serial.println(sum);
 
  //Serial.println(number); 
  if (sum){
    char key = customKeypad.getKey();
    // I'm not sure about this loop
    
    if (key == '#') // increment binary counter
    {
       Serial.println("i"+firstKey);
       //digitalWrite(13, HIGH);
       //delay (1000);
     for(counter=0; counter< sum; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
       unsigned long currentMillis = millis();
       if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      
     } // end of increment counter
    } // end of '#'
    
    
    else if (key == '*') // decrement binary counter
    {
      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);
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      } // end of decrement counter
      
    } // end of '*'
    } // end of sum
    keyPressed=0;
   } // end of keycount
  } //end of if(key)
   
 } // end of loop

Now, when I press two numbers I get the right sum on the serial monitor.
However, if I press '*' I get minus 6. Similarly, for '#' I get minus 13.
How do I convert the third key to either increase the decrease the counting?
At the moment my LEDs don't do anything.
I also have to convert the input to binary counting.
How do I do it?
Regards

The reason your getting -6 and -13 for # and * is beacuse you took out,

if (key != '*' && key !='#'){

// number =key - '0' ; // convert key to its value
} // end of sort

This part was making all the button work correctly. Try to do include this with "keyPressed ==1" and "keyPressed ==2".

Also that link I gave works fine. Im guessing you didn't fully understand it, no problem though. Your method will suffice.

How do I get then the sum If I put it back?
It doesn't work.
For the sum I need:
sum=(number1 *10)+number2;
Can I do it differently?

if (key != '*' && key !='#' && keyPressed ==1 ){
     number1= key - '0';
     firstKey=key;
   }

Repeat for second key press

Thanks,
It works but I can only press two number keys at the time.
If I press either * or # it stops everything
The * and # keys still don't do anything and the counting doesn't start.
Now, what next?

According to your code, you have everything inside keypress2, and with the "key != '*' && key !='#' ", it will never get to the other buttons. You need to fix your brackets. Keypress1 is fine, now do the same thing for Keypress2. You can keep the sum inside, but everything else must come out.

Im not home right now, nor did I bring my stuff with me to work.

HazardsMind:
According to your code, you have everything inside keypress2, and with the "key != '*' && key !='#' ", it will never get to the other buttons. You need to fix your brackets. Keypress1 is fine, now do the same thing for Keypress2. You can keep the sum inside, but everything else must come out.

Thanks,
I really don't know how to close that bracket or more exactly, where is
the right place to put it. It shows me lots of errors.
Look, this is my modified code:

#include <Keypad.h>

int ledPin[12]={13,12,11,10,9,8,7,6,5,4,3,2};
int number1=0;
int number2=0;
int keyPressed=0;
int counter=0;
int sum=0;
char firstKey;
char secondKey;
long previousMillis = 0;
long interval = 1000;

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
   keyPressed= keyPressed+1;
   if (key != '*' && key !='#'){
    
  // number =key - '0' ; // convert key to its value
  } // end of sort
   if (key != '*' && key !='#'&& keyPressed ==1){
     number1= key - '0';
     firstKey=key;
    } // end of key==1
  else if (key != '*' && key !='#'&& keyPressed==2){
    number2= key - '0';
    secondKey=key;
    sum=(number1 *10)+number2;
     // end or key==2
  
  //Serial.println(number); 
  if (sum){
    char key = customKeypad.getKey();
    // I'm not sure about this loop
    
    if (key == '#') // increment binary counter
    {
       Serial.println("i"+firstKey);
       //digitalWrite(13, HIGH);
       //delay (1000);
     for(counter=0; counter< sum; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
       unsigned long currentMillis = millis();
       if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      
     } // end of increment counter
    } // end of '#'
    
    
    else if (key == '*') // decrement binary counter
    {
      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);
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
      } // end of millis
     
      } // end of decrement counter
      
    } // end of '*'
    } // end of sum
    keyPressed=0;
   } // end of keycount
   }//end of if(key)
   
  }// end of loop

If I close the bracket after "end of key==2", see above, I also
have to close the loop which is worse.
I've been trying since this morning.
I really don't know