Industriële fabricage
Industrieel internet der dingen | Industriële materialen | Onderhoud en reparatie van apparatuur | Industriële programmering |
home  MfgRobots >> Industriële fabricage >  >> Manufacturing Technology >> Productieproces

Pixel Chaser: Interactieve één-tik LED-spel met Arduino Nano

Onderdelen en benodigdheden

OnderdeelAantal
Arduino Nano R31
Adafruit NeoPixel Ring: WS2812 5050 RGB LED1
Solderloos breadboard (half formaat)1
Weerstand 10k ohm1
SparkFun Drukknop 12mm1
Draadbruggen1

Apps en diensten

  • Arduino IDE

Over dit project

Als voormalige gamedeveloper ken ik de heilige graal van mobiele games: de ultieme "one-tap" game. Ik wilde zien of ik een geweldige één-klik "fysica"-game kon maken met een microcontroller, een knop en visuele feedback.

Welkom bij PIXEL CHASER!

Een eenvoudige build – laten we beginnen!

Wat heb je nodig?

  • Arduino Nano (of elke microcontroller met 2 vrije I/O-pinnen)
  • RGB "NeoPixel" ring of strip
  • Momentane drukknop
  • 10k weerstand
  • Breadboard(s)
  • Draden

Stap 1: Nano plaatsen

Zet de Nano aan één uiteinde van het breadboard met de USB-connector aan de rand.

Stap 2: Knop plaatsen

Plaats de knop aan de andere kant, zo dicht mogelijk aan de rand voor gemakkelijke toegang.

Stap 3: Pull-down weerstand

Plaats de 10k-weerstand tussen één kant van de knop en de GND-rail. Dit zorgt voor een standaard LOW-toestand. De weerstand voorkomt een kortsluiting tussen VCC en GND bij indrukken.

Stap 4: VCC aan knop

Verbind de andere kant van de knop met de VCC-rail. De knop gaat HIGH wanneer ingedrukt.

Stap 5: Knop naar D3

Verbind de GND-kant van de knop met D3 op de Nano. We lezen D3 voor de knopstatus.

Stap 6: NeoPixel aansluiten

VCC → VCC-rail, GND → GND-rail, DI (Data In) → D4 op de Nano.

Stap 7: Voeding breadboard

5V-pin Nano → VCC-rail, GND-pin Nano → GND-rail.

Stap 8: Code uploaden & spelen!

Upload de code en geniet van het spel.

Volg me

Code

Pixel Chaser Game - Arduino Code
/* ---------------------------------------------------------------------------
Pixel Chaser Game - v1.0.0 - 24/03/2019
AUTHOR/LICENSE: Seon Rozenblum - seon@unexpectedmaker.com
Copyright 2016 - GNU GPL v3
LINKS: Blog: unexpectedmaker.com | YouTube: youtube.com/c/unexpectedmaker
DISCLAIMER: "as is", geen support, geen garantie.
--------------------------------------------------------------------------- */

#include <Adafruit_NeoPixel.h>
#include "OneButton.h"

#define PIXELPIN 4
#define NUMPIXELS 24
#define BUTTON_TAP 3

OneButton buttonTAP(BUTTON_TAP, false);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIXELPIN, NEO_GRB + NEO_KHZ800);

int playerIndex = -1, playerIndexTrail = -1, playerDirection = 1;
float nextMove = 0, currentPlayerSpeed = 150;
unsigned long countdown = 0;
int enemyIndex = -1, coinIndex = -1;
int score = 0, bestScore = 0, lastScore = 0;
bool gameOver = false;

void setup() {
  pinMode(BUTTON_TAP, INPUT);
  buttonTAP.attachClick(singleClick);
  buttonTAP.attachLongPressStart(longClick);
  pixels.begin();
  pixels.setBrightness(20);
  countdown = millis() + 2000;
}

void loop() {
  buttonTAP.tick();
  if (gameOver) return;
  SetLevel();
  if (countdown > millis()) { pixels.show(); return; }
  DisplayPlayer();
  pixels.show();
}

void ClearLevel() {
  for (int i = 0; i < NUMPIXELS; i++) pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  pixels.show();
}

void BestScore() {
  for (int i = 0; i < NUMPIXELS; i++) {
    if (i < bestScore) pixels.setPixelColor(i, pixels.Color(255, 155, 0));
    else pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  }
  if (lastScore < bestScore) {
    for (int i = 0; i < lastScore; i++) pixels.setPixelColor(i, pixels.Color(255, 0, 0));
  }
  pixels.show();
}

void GameOver() {
  int a = enemyIndex, b = enemyIndex;
  for (int i = 0; i < NUMPIXELS / 2; i++) {
    pixels.setPixelColor(a, pixels.Color(255, 0, 0));
    pixels.setPixelColor(b, pixels.Color(255, 0, 0));
    a = (a + 1) % NUMPIXELS;
    b--; if (b == -1) b = NUMPIXELS;
    pixels.show(); delay(20);
  }
  delay(100);
  a = enemyIndex; b = enemyIndex;
  for (int i = 0; i < NUMPIXELS / 2; i++) {
    pixels.setPixelColor(a, pixels.Color(0, 0, 0));
    pixels.setPixelColor(b, pixels.Color(0, 0, 0));
    a = (a + 1) % NUMPIXELS;
    b--; if (b == -1) b = NUMPIXELS;
    pixels.show(); delay(20);
  }
  delay(100);
  BestScore();
}

void SetLevel() {
  if (enemyIndex < 0) {
    if (playerIndex < 0) enemyIndex = NUMPIXELS / 2;
    else {
      enemyIndex = random(0, NUMPIXELS);
      while (abs(enemyIndex - playerIndex) < (NUMPIXELS / 4)) enemyIndex = random(0, NUMPIXELS);
    }
  }
  if (coinIndex < 0) {
    coinIndex = random(0, NUMPIXELS);
    while (abs(coinIndex - playerIndex) < 7 || abs(coinIndex - enemyIndex) < 7) coinIndex = random(0, NUMPIXELS);
  }
  pixels.setPixelColor(enemyIndex, pixels.Color(255, 0, 0));
  pixels.setPixelColor(coinIndex, pixels.Color(255, 255, 0));
}

void DisplayPlayer() {
  if (nextMove < millis()) {
    nextMove = millis() + currentPlayerSpeed;
    if (playerIndexTrail >= 0) pixels.setPixelColor(playerIndexTrail, pixels.Color(0, 0, 0));
    if (playerIndex >= 0) {
      pixels.setPixelColor(playerIndex, pixels.Color(0, 100, 0));
      playerIndexTrail = playerIndex;
    }
    playerIndex += playerDirection;
    if (playerIndex < 0) playerIndex = NUMPIXELS - 1;
    else if (playerIndex == NUMPIXELS) playerIndex = 0;
    pixels.setPixelColor(playerIndex, pixels.Color(0, 255, 0));

    // Munt gegrepen?
    if (playerIndex == coinIndex) {
      enemyIndex = -1; coinIndex = -1; score++;
      currentPlayerSpeed = constrain(currentPlayerSpeed - 10, 50, 150);
      ClearLevel();
      pixels.setPixelColor(playerIndex, pixels.Color(0, 255, 0));
    }
    // Vijand geraakt?
    else if (playerIndex == enemyIndex) {
      lastScore = score;
      if (score >= bestScore) bestScore = score;
      GameOver();
      gameOver = true;
      enemyIndex = -1; coinIndex = -1; playerIndex = -1;
    }
  }
}

void singleClick() {
  if (countdown > millis()) return;
  playerDirection = -playerDirection;
}

void longClick() {
  gameOver = !gameOver;
  if (gameOver) {
    enemyIndex = -1; coinIndex = -1; playerIndex = -1; currentPlayerSpeed = 150;
    ClearLevel();
  } else {
    ClearLevel(); score = 0; currentPlayerSpeed = 150; countdown = millis() + 2000;
  }
}

Productieproces

  1. Bouw een real-time gyroscoop-spel met Arduino Nano & MPU-6050 sensor
  2. Arduino Digitale Dobbelsteen: Bouw Je Eigen LCD-willekeurige Getalgenerator
  3. Bouw een Klassiek Pong-spel op Arduino UNO met OLED-display – Stap-voor-stap Tutorial
  4. Bouw Je Eigen 37-LED Roulette-spel met Arduino Nano
  5. DIY Arduino USB Gaming Controller – Bouw Je Eigen High-Performance Gamepad
  6. Arduino Touch Breakout-spel: Bouw een Klassieke Arcade op een 2,4″ TFT LCD
  7. Bouw een Tetris-spel met Arduino Nano op een Zelfgemaakte 16×8 LED-matrix
  8. Arduino-gebaseerde Afstotende Magnetische Levitatie: Eenvoudige Schakeling & DIY Handleiding
  9. Bouw een geautomatiseerde Google Chrome Dino Game met Arduino
  10. Alexa-ingeschakelde Arduino Thuiscontroller: Slimme Automatisering Made Simple
  11. NeoPixel Matrix Pong op Arduino Nano: Bouw je eigen neon Pong-spel