Crazy project

My friends call this project “crazy” and I think
this is the best name for it.
I discussed parts of it in my previous topic and I want to thank
people who came with suggestions.,153845.0.html

My project includes an Arduino Mega, 12 LEDs and a 4/3 keypad.
I want to use the keypad to control the binary counting on LEDs.
This is my code so far:

/* 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;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {

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

void loop(){
  char key = customKeypad.getKey();
  /*if (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){
   if ( keyPressed==2){
    if (key == '#') // increment binary counter
       //digitalWrite(13, HIGH);
       //delay (1000);
     for(counter=0; counter< number; counter++){
      //digitalWrite(ledPin[counter-1], HIGH);
      digitalWrite(ledPin[counter], HIGH);
      delay (1000);
     } // 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);
      delay (1000);
      } // end of decrement counter
    } // end of '*'
   } // end of keycount
  } //end of if(key)
 } // end of loop

The schetch works but the counting still does’t work as I want.
There are a few things left to do:
Convert to binary counting
Get two number keys and an operator key at the time

By getting two number keys at the time I can increase the combinations
up to 99. Since 12 bits in binary gives the value 4096 in decimal, there is enough
room to play with number combinations and convert them to binary.

At the moment my keypad only reads two keys at the time but it behaves strangely
and I can see in the Serial Monitor that it has moments when reading the keys or not.

As I said, I want to read two number keys and an operator (either ‘*’ or ‘#’ meaning
counting up or down) at any time and perhaps to start from zero at any new counting.
Waiting for your suggestions.

I can see in the Serial Monitor that it has moments when reading the keys or not

You've got "delay(1000)" in there - what do you expect?


I can see in the Serial Monitor that it has moments when reading the keys or not

You've got "delay(1000)" in there - what do you expect?

Bulls eye! I was about to post that when reading his code.

Ever since I learned about millis(), 99% of my delay() calls are gone.

The keypad library is non-blocking which means it runs each time the loop runs. If you delay for 3 seconds then the loop will only run once every 3 seconds which means you have to hold the button down for 3 seconds before getKey() is called.

From the Arduino tutorial for Blink Without Delay

"Sometimes you need to do two things at once. For example you might want to blink an LED (or some other time-sensitive function) while reading a button press or other input."

Thanks guys. I guess, the use of millis will sort out the problem with my keypad. Now, what about the binary conversion? Should I keep dividing the result by two? Regards