Unit 24: Caught in a number net

 

 

 

Code for Sketch One: 


#include 
#include 

// Pin definitions for the 4-digit display
const int CLK = 12; // Connect to CLK pin of display
const int DIO = 13; // Connect to DIO pin of display

TM1637TinyDisplay display(CLK, DIO);

// Keypad settings
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};

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

unsigned long lastTime = 0;
int scrollPosition = 0;
String currentInput = ""; // Start with empty string

void setup() {
  display.begin();
  display.showString("----"); // Blank or dashes initially
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    Serial.print("Key Pressed: ");
    Serial.println(key);

    if (key == '*') {
      currentInput = ""; // Reset input
      scrollPosition = 0;
      display.showString("----"); // Indicate reset visually
    } 
    else if (key == '#') {
      currentInput = "3107"; // Display 3107 when # is pressed
      scrollPosition = currentInput.length();
      display.showString(currentInput.c_str()); // Show "3107"
      Serial.print("Input Confirmed: ");
      Serial.println(currentInput); // Confirm entry in serial monitor
    }
    else if (key >= '0' && key <= '9') {
      currentInput += key;
      if (currentInput.length() > 4) {
        currentInput.remove(0, 1); // Keep last 4 digits
      }
      scrollPosition = currentInput.length();
    }

    display.clear();
    display.showString(currentInput.c_str());
  }

  if (millis() - lastTime >= 300) {
    if (scrollPosition > 0) {
      display.clear();
      display.showString(currentInput.c_str() + (4 - scrollPosition));
      scrollPosition--;
    }
    lastTime = millis();
  }
}


  

Code for Sketch Two (Alternate event):


#include 
#include 

// Pin definitions for the 4-digit display
const int CLK = 12; // Connect to CLK pin of display
const int DIO = 13; // Connect to DIO pin of display

TM1637TinyDisplay display(CLK, DIO);

// Keypad settings
const byte ROWS = 4; // Number of rows
const byte COLS = 3; // Number of columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3};   // Connect to the column pinouts of the keypad

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

String currentInput = ""; // Start with an empty input

void setup() {
  display.begin(); // Initialize the display
  display.showString("0000"); // Display "0000" initially
  Serial.begin(9600); // Initialize serial communication for debugging
}

void loop() {
  char key = keypad.getKey(); // Read the pressed key

  if (key) { // If a key is pressed
    Serial.print("Key Pressed: ");
    Serial.println(key); // Output to serial for debugging

    // Handle special keys '*' and '#'
    if (key == '*' || key == '#') {
      currentInput = ""; // Reset the input
      display.showString("0000"); // Reset display to "0000"
    }
    // Handle digit keys (0-9)
    else if (key >= '0' && key <= '9') {
      currentInput += key; // Append the pressed key to current input

      // Keep only the last 4 digits of the input
      if (currentInput.length() > 4) {
        currentInput.remove(0, 1); // Keep the last 4 characters
      }

      // Check if the current input matches the secret code
      if (currentInput == "3107") {
        display.showString("6623"); // Display the secret code "6623"
      } else {
        display.showString(currentInput.c_str()); // Show the current input
      }
    }
  }
}

  

 

Notes: