🔍

Keypad as Keyboard Simulator

#include <Arduino.h>
#include <Keypad.h>
#include "Keyboard.h"

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'T', 'X', 'H', 'D'}
};
byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4, 3}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad keypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
  delay(100);
   

  Keyboard.begin();
  
}
  
void loop(){

  
  char key = keypad.getKey();
  
  if (key){
   
    Keyboard.write(key);
  }

  delay(10);
}

basic p5.js code


let mole;
let score = 0;

function setup() {
  createCanvas(400, 400);
  mole = new Mole();
}

function draw() {
  background(220);
  
  // Display the score
  textSize(20);
  text("Score: " + score, 10, 30);

  // Update and display the mole
  mole.update();
  mole.display();
}

function keyPressed() {
  // Check if the correct key was pressed to hit the mole
  if (keyCode === mole.key) {
    mole.hit();
    score++;
  }
}

class Mole {
  constructor() {
    this.x = random(width);
    this.y = random(height);
    this.size = 50;
    this.key = int(random(65, 90)); // Generate a random key code between A and Z
    this.visible = false;
    this.timer = 0;
  }
  
  update() {
    // Make the mole appear randomly
    if (millis() > this.timer) {
      this.visible = !this.visible;
      
      // Set the timer for the next appearance
      if (this.visible) {
        this.timer = millis() + random(500, 2000);
      } else {
        this.timer = millis() + random(500, 1000);
      }
    }
  }
  
  display() {
    if (this.visible) {
      fill(255, 0, 0);
      ellipse(this.x, this.y, this.size);
      fill(255);
      textAlign(CENTER, CENTER);
      textSize(20);
      text(String.fromCharCode(this.key), this.x, this.y);
    }
  }
  
  hit() {
    // Move the mole to a new random position
    this.x = random(width);
    this.y = random(height);
    this.visible = false;
  }
}