How to control 4 LEDs without using too much digital write with Keypad

For a project I am working on, I need to have a keypad control LED that turns on and off. The twist is that my main function can't be digitalWrite. Should I create an array, or should I create multiple voids? The LEDs need to be controlled with different values, as in 'A' = [on, off, off, on] and 'B' = [off, on, on, on].
Here is my code:

#include <Key.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;

//char variable is for character (such as the letters, numbers, and symbols)
char keyMatrix[ROWS][COLS] = {
 {'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'},
};

byte rowPins[ROWS] = {46, 47, 48, 49};
byte colPins[COLS] = {50, 51, 52, 53};

Keypad myKeypad = Keypad(makeKeymap(keyMatrix), rowPins, colPins, ROWS, COLS);



const int rLED = 5;
const int wLED = 4;
const int gLED = 3;
const int bLED = 2;


void setup() {
 //activates the serial monitor
 Serial.begin(9600);

 for(int pin = 2; pin <= 5; pin++){
    pinMode(pin, OUTPUT);
 }
}

void loop() {
  //saving the keypress into a character variable
  char theKeypress = myKeypad.getKey();

  if(theKeypress){ 

    switch(theKeypress){ 

      case '1': 
 

      break; 

      case '2': 


      break; 

      case '3': 


      break; 
      
      case '4': 
 

      break; 

      case '5': 


      break; 

      case '6': 


      break; 
      
      case '7': 
 

      break; 

      case '8': 


      break; 

      case '9': 


      break; 
      
      case '0': 
 

      break; 

      case 'A': 


      break; 

      case 'B': 


      break; 
      
      case 'C': 
 

      break; 

      case 'D': 


      break; 

      case '#': 


      break; 

      case '*': 


      break; 

    } 

  } 
  
}

  • Write a function that receives the new LED states then updates the LEDs.
    Call this function in each switch/case
      case '1': 
          updateLEDs(yourLedStateFor_1);
          break; 

This example has zero digitalWrite() function calls, but lights up LEDs. Be sure to connect your LEDs to "PWM" pins (for your Mega2560).

Wouldn't "A" be ON OFF ON OFF and "B" be ON OFF ON ON?

It can't? Why not, pray tell?

Srsly, what does that statement even mean? I have never seen a sketch which the main function is digitalWrite().

LOL. No, it does not.

a7

1 Like

Look Ma, no digitalWrite()! Try it here:


// https://wokwi.com/projects/419438907998692353

# include <Key.h>
# include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char keyMatrix[ROWS][COLS] = {
 {'A', 'B', 'C', 'D'},
 {'E', 'F', 'G', 'H'},
 {'J', 'I', 'K', 'L'},
 {'M', 'N', 'O', 'P'},
};

byte rowPins[ROWS] = {46, 47, 48, 49};
byte colPins[COLS] = {50, 51, 52, 53};

Keypad myKeypad = Keypad(makeKeymap(keyMatrix), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
  Serial.println("\nJello Whirled!\n");

  DDRF = 0xf0;
}

void loop() {

  char theKeyPress = myKeypad.getKey();

  if (theKeyPress) { 
    Serial.println(theKeyPress);

    PORTF &= 0xf0;
    PORTF |= theKeyPress - 'A';
  }
}

a7
1 Like
  • And the Arduino this can be used with is _______________ ?
1 Like

The one in the wokwi simulation. My assumption was that it was what the OP used based on the pin numbers chosen for the keypad.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.