Make a Token Display with Arduino, Pixel LED Seven Segment & Keypad

 Make a Token Display with Arduino, Pixel LED Seven Segment & Keypad









Explanation
This document explains the line-by-line working of an Arduino project that uses a
3-digit Pixel LED seven-segment display and a 4x4 keypad to create a token display
system.

Libraries Used
#include <Adafruit_NeoPixel.h>
#include <Keypad.h>
These libraries allow control of WS2812/NeoPixel LEDs and a 4x4 matrix keypad.

Configuration
#define LED_PIN    6
#define NUM_DIGITS 3
#define LEDS_PER_SEGMENT 4
#define SEGMENTS_PER_DIGIT 7
#define TOTAL_LEDS (NUM_DIGITS * SEGMENTS_PER_DIGIT * LEDS_PER_SEGMENT)
Defines the LED pin and calculations for total number of LEDs required to simulate
7-segment digits.

NeoPixel Strip Initialization
Adafruit_NeoPixel strip(TOTAL_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
Initializes the NeoPixel strip with total LED count and communication parameters.

Digit to Segment Mapping
const byte digitMap[10][7] = {...};
This maps numbers 0-9 to their corresponding 7-segment on/off pattern.

Keypad Setup
Defines the characters, rows, and columns of the keypad and associates them with
Arduino pins.

Global Variables
displayNumber: Stores the current number displayed.
enteredNumber: Holds digits typed before pressing '#'.
currentColor: LED color for the segments (default red).

Setup Function
Initializes Serial, NeoPixel strip, and clears the display.
Loop Function
Calls handleKeypad() to process keypad input and showNumber() to update display.
handleKeypad() Function

Processes key inputs:
- 0-9: Builds the entered number string.
- '#': Converts entered string to integer and updates display.
- '*': Clears the current entry.
- 'A': Increments display number.
- 'B': Decrements display number.
- 'C': Resets display to 0.
showNumber() Function
Splits the number into individual digits and uses digitMap to determine which
segments to light up.
Each segment lights up a group of LEDs in the configured color.

////////////////////////////////////////////

// Three digit token display using pixel led seven segments arduino and 4x4 keypad

/*
Key Action
0-9 Type digits
# Confirm entry, update display
* Clear current entry
A Increment number
B Decrement number
C Reset to 0

*/

#include <Adafruit_NeoPixel.h>
#include <Keypad.h>

// === CONFIGURATION ===
#define LED_PIN    6
#define NUM_DIGITS 3
#define LEDS_PER_SEGMENT 4
#define SEGMENTS_PER_DIGIT 7
#define TOTAL_LEDS (NUM_DIGITS * SEGMENTS_PER_DIGIT * LEDS_PER_SEGMENT)

Adafruit_NeoPixel strip(TOTAL_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

const byte digitMap[10][7] = {
  {1,1,1,1,1,1,0}, {0,0,1,1,0,0,0}, {0,1,1,0,1,1,1}, {0,1,1,1,1,0,1},
  {1,0,1,1,0,0,1}, {1,1,0,1,1,0,1}, {1,1,0,1,1,1,1}, {0,1,1,1,0,0,0},
  {1,1,1,1,1,1,1}, {1,1,1,1,1,0,1}
};

int displayNumber = 0;
uint32_t currentColor = strip.Color(255, 0, 0);  // red by default

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String enteredNumber = "";

// === FUNCTION PROTOTYPES ===
void handleKeypad();
void showNumber(int num);

void setup() {
  Serial.begin(9600);
  strip.begin();
  strip.show();
  Serial.println("=== Arduino Token Display Started ===");
}

void loop() {
  handleKeypad();
  showNumber(displayNumber);
}

void handleKeypad() {
  char key = keypad.getKey();
  if (key) {
    Serial.print("Key pressed: ");
    Serial.println(key);

    if (key >= '0' && key <= '9') {
      enteredNumber += key;
      Serial.print("Entered so far: ");
      Serial.println(enteredNumber);
    }
    else if (key == '#') {
      if (enteredNumber.length() > 0) {
        displayNumber = enteredNumber.toInt();
        Serial.print("Display updated to: ");
        Serial.println(displayNumber);
        enteredNumber = "";
      }
    }
    else if (key == '*') {
      enteredNumber = "";
      Serial.println("Entry cleared.");
    }
    else if (key == 'A') {
      displayNumber++;
      if (displayNumber > 999) displayNumber = 0;
      Serial.print("Incremented: ");
      Serial.println(displayNumber);
    }
    else if (key == 'B') {
      displayNumber--;
      if (displayNumber < 0) displayNumber = 999;
      Serial.print("Decremented: ");
      Serial.println(displayNumber);
    }
    else if (key == 'C') {
      displayNumber = 0;
      Serial.println("Reset to 0.");
    }
  }
}

void showNumber(int num) {
  strip.clear();
  int digits[NUM_DIGITS];

  // Break number into individual digits
  for (int i = NUM_DIGITS - 1; i >= 0; i--) {
    digits[i] = num % 10;
    num /= 10;
  }

  // Draw each digit
  for (int d = 0; d < NUM_DIGITS; d++) {
    Serial.print("Drawing digit ");
    Serial.print(d);
    Serial.print(": ");
    Serial.println(digits[d]);

    for (int s = 0; s < SEGMENTS_PER_DIGIT; s++) {
      if (digitMap[digits[d]][s]) {
        int startLED = (d * SEGMENTS_PER_DIGIT + s) * LEDS_PER_SEGMENT;
        for (int l = 0; l < LEDS_PER_SEGMENT; l++) {
          strip.setPixelColor(startLED + l, currentColor);
        }
      }
    }
  }
  strip.show();
}

/////////////////////////////////////////////////////

No comments:

Post a Comment

Wifi Home automation Diy kit

 Wifi Home automation Diy kit By Manmohan Pal Wifi Home Automation Kit Hi, This is Manmohan Pal, I am glad to present a DIY kit for Home Aut...