WayneChan
Published

UNO Membrane Switch Control And Display

Pressed on the matrix button, the corresponding number of LEDs can be illuminated, and the number is displayed on the dot matrix.

IntermediateFull instructions provided6 hours499
UNO Membrane Switch Control And Display

Things used in this project

Hardware components

The Most Complete MEGA2560 Project Starter Kit
Kuongshun Electronic The Most Complete MEGA2560 Project Starter Kit
×1

Story

Read more

Code

Key control and display

C/C++
//We always have to include the library
#include "LedControl.h"
#include <Keypad.h>

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to LOAD(CS)
 pin 10 is connected to the CLK 
 We have only a single MAX72XX.
 */
byte ledPin[ ] = {A1,A2,A3,A4,A5,A6,A7,A8,A9};     //Create an array with LED pins
int ledDelay(250);    //Vary the delay between the lights
int dir = 1;
int currentLED = 0;
unsigned long changeTime;

const byte ROWS = 4;                     //four rows
const byte COLS = 4;                    //four columns
byte rowPins[ROWS] = {9, 8, 7, 6};     //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2};    //connect to the column pinouts of the keypad
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
 
byte zero[8] = {0x0,0x38,0x44,0x44,0x44,0x44,0x38,0x0};  //0
byte one[8] = {0x8,0x18,0x28,0x8,0x8,0x8,0x8,0x3E};       //1
byte two[8] = {0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E};     //2
byte three[8] = {0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0};       //3
byte four[8] = {0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8};        //4
byte five[8] = {0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0};        //5
byte six[8] = {0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0};       //6
byte seven[8] = {0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8};         //7
byte eight[8] = {0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E};   //8
byte nine[8] = {0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E};     //9
byte A[8] = {0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22};     //A
byte B[8] = {0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0};    //B
byte C[8] = {0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0};   //C
byte D[8] = {0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0};  //D

byte biglove[8] = {0x0,0x66,0xFF,0xFF,0xFF,0x7E,0x3C,0x18,};      //Big heart
byte smile[8]=   {0x3C,0x42,0xA5,0x81,0xA5,0x99,0x42,0x3C};      //smiling face
byte hashtag[8]= {0x24,0x24,0xFF,0x24,0x24,0xFF,0x24,0x24};    //#
byte xin[8]= {0x81,0x42,0x24,0x18,0xFF,0x24,0x42,0x81};       //*
LedControl lc=LedControl(12,10,11,1);
 
void setup(){
  for (int x=0; x<9; x++)    //Set all pins to output mode
    {
      pinMode(ledPin[x], OUTPUT);
    }
    changeTime = millis();
 lc.shutdown(0,false);         //At startup, MAX72XX is in power-saving mode
 lc.setIntensity(0,8);        //Set brightness to maximum
 printByte(smile);           //display smiling face
 delay(500);               
 printByte(biglove);           
 delay(500);              
}
void led0(){
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<9; x++) { 
    analogWrite(ledPin[x], 0); } 
    changeTime = millis();
  }
}
void led1(){
  if((millis() - changeTime) > ledDelay)  
  {
    for (int x=0; x<1; x++) { 
    digitalWrite(ledPin[x], HIGH); } 
    for (int x=1; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); } 
    changeTime = millis();
  }
}

void led2(){
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<2; x++) { 
    digitalWrite(ledPin[x], HIGH); } 
    for (int x=2; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); } 
    changeTime = millis();
  }
  
}

void led3(){
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<3; x++) { 
    digitalWrite(ledPin[x], HIGH); } 
    for (int x=3; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); } 
    changeTime = millis();
  }
}

void led4(){
  if((millis() - changeTime) > ledDelay)  
  {
    for (int x=0; x<4; x++) { 
    digitalWrite(ledPin[x], HIGH); } 
    for (int x=4; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); } 
    changeTime = millis();
  }
}

void led5(){
  if((millis() - changeTime) > ledDelay)  
  {
    for (int x=0; x<5; x++) { 
    digitalWrite(ledPin[x], HIGH); }
    for (int x=5; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); }  
    changeTime = millis();
  }
}

void led6(){
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<6; x++) { 
    digitalWrite(ledPin[x], HIGH); } 
    for (int x=6; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); } 
    changeTime = millis();
  }
}

void led7(){
  if((millis() - changeTime) > ledDelay)   
    for (int x=0; x<7; x++) { 
    digitalWrite(ledPin[x], HIGH); } 
    for (int x=7; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); } 
    changeTime = millis();
  }
}

void led8(){
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<8; x++) { 
    digitalWrite(ledPin[x], HIGH); }
    for (int x=8; x<9; x++) { 
    digitalWrite(ledPin[x], LOW); }  
    changeTime = millis();
  }
}

void led9(){
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<9; x++) { 
    digitalWrite(ledPin[x], HIGH); } 
    changeTime = millis();
  }
}

void ledA(){
  
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<9; x++) { 
    analogWrite(ledPin[x], 255);
    delay(50);
    analogWrite(ledPin[x], 0);
    delay(50);
    } 
    for (int x=10; x>0; x--) { 
    analogWrite(ledPin[x], 255);
    delay(50);
    analogWrite(ledPin[x], 0);
    delay(50);
    } 
    changeTime = millis();
}
}

void ledB(){
  
  if((millis() - changeTime) > ledDelay)   
  {
    for (int x=0; x<9; x+=2) { 
    analogWrite(ledPin[x], 255);
    delay(100);
    analogWrite(ledPin[x], 0);
    delay(100);
    } 
    for (int x=10; x>0; x-=2) { 
    analogWrite(ledPin[x], 255);
    delay(100);
    analogWrite(ledPin[x], 0);
    delay(100);
    } 
    changeTime = millis();
}
}
void loop(){ 
      
    char customKey = customKeypad.getKey();
    switch(customKey){
    case '0':  lc.clearDisplay(0);   printByte(zero);   led0(); break;
    case '1':  lc.clearDisplay(0);   printByte(one);    led1(); break; 
    case '2':  lc.clearDisplay(0);   printByte(two);    led2(); break;
    case '3':  lc.clearDisplay(0);   printByte(three);  led3();break;
    case '4':  lc.clearDisplay(0);   printByte(four);   led4();break;
    case '5':  lc.clearDisplay(0);   printByte(five);   led5();break;
    case '6':  lc.clearDisplay(0);   printByte(six);    led6();break; 
    case '7':  lc.clearDisplay(0);   printByte(seven);  led7();break;
    case '8':  lc.clearDisplay(0);   printByte(eight);  led8();break;
    case '9':  lc.clearDisplay(0);   printByte(nine);   led9();break;
    case 'A':  lc.clearDisplay(0);   printByte(A);      ledA();break; 
    case 'B':  lc.clearDisplay(0);   printByte(B);      ledB();break;
    case 'C':  lc.clearDisplay(0);   printByte(C);   break;
    case 'D':  lc.clearDisplay(0);   printByte(D);   break;
    case '#':  lc.clearDisplay(0);   printByte(hashtag);     break;
    case '*':  lc.clearDisplay(0);   printByte(xin);         break; 
    default:                                                 break;
  }
}
//Lattice display function
void printByte(byte character [])
{
   int i = 0;
  for(i=0;i<8;i++)
  {
    lc.setRow(0,i,character[i]);
  }
}

Credits

WayneChan

WayneChan

21 projects • 1 follower
Arduino, Raspberry Pi & 3D

Comments