Fabrication industrielle
Internet des objets industriel | Matériaux industriels | Entretien et réparation d'équipement | Programmation industrielle |
home  MfgRobots >> Fabrication industrielle >  >> Manufacturing Technology >> Processus de fabrication

Créez un jeu interactif sur LCD avec Arduino UNO

Composants et fournitures

Arduino UNO ×1
Écran LCD 16x2 à rétroéclairage RGB (Adafruit) ×1
Plaque d'essai (breadboard) ×1
Résistance 220 Ω ×1
Bouton-poussoir 12 mm (SparkFun) ×1
Fils de liaison (génériques) ×1
Câble USB-A vers B ×1

Applications et services en ligne

À propos de ce projet

Les pièces nécessaires sont présentées ci-dessus. Commencez avec l'Arduino non alimenté. Ne branchez le câble USB qu'au moment de programmer et tester le jeu.

Connexions

Reliez le 5V de l'Arduino à la rangée rouge (alimentation) à l'extrémité gauche du breadboard. Reliez le GND à la rangée noire/bleue (masse).

Le module LCD possède un connecteur mâle 16 broches dessous. Insérez-le dans le breadboard comme indiqué. Broches (de gauche à droite) :

Avec de courts fils :

Avec de longs fils :

Insérez le bouton-poussoir à gauche de l'écran, enjambant la rainure centrale. Reliez une broche du haut à la rangée noire (court fil), l'autre broche du haut à la broche 2 de l'Arduino.

Code

LCD_Game.ino (Arduino)

#include <LiquidCrystal.h>
#define PIN_BUTTON 2
#define PIN_AUTOPLAY 1
#define PIN_READWRITE 10
#define PIN_CONTRAST 12
#define SPRITE_RUN1 1
#define SPRITE_RUN2 2
#define SPRITE_JUMP 3
#define SPRITE_JUMP_UPPER '.'
#define SPRITE_JUMP_LOWER 4
#define SPRITE_TERRAIN_EMPTY ' '
#define SPRITE_TERRAIN_SOLID 5
#define SPRITE_TERRAIN_SOLID_RIGHT 6
#define SPRITE_TERRAIN_SOLID_LEFT 7
#define HERO_HORIZONTAL_POSITION 1
#define TERRAIN_WIDTH 16
#define TERRAIN_EMPTY 0
#define TERRAIN_LOWER_BLOCK 1
#define TERRAIN_UPPER_BLOCK 2
// ... définitions des positions du héros ...
LiquidCrystal lcd(11, 9, 6, 5, 4, 3);
static char terrainUpper[TERRAIN_WIDTH + 1];
static char terrainLower[TERRAIN_WIDTH + 1];
static bool buttonPushed = false;

void initializeGraphics() {
  // Création des caractères personnalisés (sprites)
  static byte graphics[] = { ... };
  for (int i = 0; i < 7; ++i) lcd.createChar(i + 1, &graphics[i * 8]);
  for (int i = 0; i < TERRAIN_WIDTH; ++i) {
    terrainUpper[i] = SPRITE_TERRAIN_EMPTY;
    terrainLower[i] = SPRITE_TERRAIN_EMPTY;
  }
}

void advanceTerrain(char* terrain, byte newTerrain) {
  // Défilement du terrain vers la gauche par demi-caractères
}

bool drawHero(byte position, char* terrainUpper, char* terrainLower, unsigned int score) {
  // Dessine le héros, détecte les collisions, affiche le score
}

void buttonPush() { buttonPushed = true; } // Interruption

void setup() {
  pinMode(PIN_READWRITE, OUTPUT); digitalWrite(PIN_READWRITE, LOW);
  pinMode(PIN_CONTRAST, OUTPUT); digitalWrite(PIN_CONTRAST, LOW);
  pinMode(PIN_BUTTON, INPUT); digitalWrite(PIN_BUTTON, HIGH);
  pinMode(PIN_AUTOPLAY, OUTPUT); digitalWrite(PIN_AUTOPLAY, HIGH);
  attachInterrupt(0, buttonPush, FALLING); // Broche 2 = interruption 0
  initializeGraphics();
  lcd.begin(16, 2);
}

void loop() {
  static byte heroPos = HERO_POSITION_RUN_LOWER_1;
  static byte newTerrainType = TERRAIN_EMPTY;
  static byte newTerrainDuration = 1;
  static bool playing = false;
  static bool blink = false;
  static unsigned int distance = 0;

  if (!playing) {
    drawHero(blink ? HERO_POSITION_OFF : heroPos, terrainUpper, terrainLower, distance >> 3);
    if (blink) { lcd.setCursor(0,0); lcd.print("Press Start"); }
    delay(250); blink = !blink;
    if (buttonPushed) { initializeGraphics(); heroPos = HERO_POSITION_RUN_LOWER_1; playing = true; buttonPushed = false; distance = 0; }
    return;
  }

  advanceTerrain(terrainLower, newTerrainType == TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
  advanceTerrain(terrainUpper, newTerrainType == TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);

  if (--newTerrainDuration == 0) {
    if (newTerrainType == TERRAIN_EMPTY) {
      newTerrainType = (random(3) == 0) ? TERRAIN_UPPER_BLOCK : TERRAIN_LOWER_BLOCK;
      newTerrainDuration = 2 + random(10);
    } else {
      newTerrainType = TERRAIN_EMPTY;
      newTerrainDuration = 10 + random(10);
    }
  }

  if (buttonPushed) {
    if (heroPos <= HERO_POSITION_RUN_LOWER_2) heroPos = HERO_POSITION_JUMP_1;
    buttonPushed = false;
  }

  if (drawHero(heroPos, terrainUpper, terrainLower, distance >> 3)) {
    playing = false; // Collision
  } else {
    // Mise à jour de la position du héros selon l'état du terrain
    if (heroPos == HERO_POSITION_RUN_LOWER_2 || heroPos == HERO_POSITION_JUMP_8) heroPos = HERO_POSITION_RUN_LOWER_1;
    else if (heroPos >= HERO_POSITION_JUMP_3 && heroPos <= HERO_POSITION_JUMP_5 && terrainLower[HERO_HORIZONTAL_POSITION] != SPRITE_TERRAIN_EMPTY) heroPos = HERO_POSITION_RUN_UPPER_1;
    else if (heroPos >= HERO_POSITION_RUN_UPPER_1 && terrainLower[HERO_HORIZONTAL_POSITION] == SPRITE_TERRAIN_EMPTY) heroPos = HERO_POSITION_JUMP_5;
    else if (heroPos == HERO_POSITION_RUN_UPPER_2) heroPos = HERO_POSITION_RUN_UPPER_1;
    else ++heroPos;
    ++distance;
    digitalWrite(PIN_AUTOPLAY, terrainLower[HERO_HORIZONTAL_POSITION + 2] == SPRITE_TERRAIN_EMPTY ? HIGH : LOW);
  }
  delay(100);
}

Schémas

Aucun document fourni.

Processus de fabrication

  1. Détecteur de langue TinyML avec Edge Impulse sur Arduino Nano 33 BLE Sense
  2. Construire un jeu gyroscopique temps réel avec Arduino Nano et capteur MPU-6050
  3. Dé numérique Arduino sur écran LCD 16x2 – Générateur aléatoire à bouton poussoir
  4. Créer votre propre jeu de roulette à 37 LED avec Arduino Nano
  5. Manette de jeu USB Arduino DIY – Construisez votre propre gamepad haute performance
  6. Jeu interactif joystick + LEDs avec Arduino
  7. Find Me : Localisateur d'objets intelligent avec Arduino et Bluetooth
  8. Pixel Chaser : Jeu LED interactif à une touche avec Arduino Nano
  9. Oscilloscope DIY Arduino Nano (10 Hz – 50 kHz) avec écran LCD 128×64