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

De la breadboard au PCB : Jeu Simon Says – Partie 1

Composants et fournitures

Arduino UNO 1
Plaque d'essai grand format (breadboard) 1
LED (génériques, 4 couleurs : vert, rouge, jaune, bleu) 4
Boutons poussoirs 12 mm 4
Buzzer piézo 1
Fils de connexion 12
Résistance 10 kΩ (pull-down boutons) 4
Résistance 330 Ω (pour LED) 4
Câble USB-A vers B 1

Applications et services en ligne

À propos de ce projet

Introduction

Le classique jeu Simon Says : reproduisez une séquence lumineuse en appuyant sur les boutons correspondants. Pour Hackster Live, nous avons présenté l'outil de conception PCB Upverter. Ce projet ludique et simple est idéal pour apprendre à passer du breadboard au PCB.

La série comporte trois parties :

  1. Partie 1 : Construction du jeu Simon Says (cet article)
  2. Partie 2 : Conception du PCB avec Upverter
  3. Partie 3 : Fraisage du PCB avec une CNC

Ce que vous apprendrez

Logiciel

Téléchargez la dernière version de l'IDE Arduino si ce n'est pas déjà fait.

Configuration matérielle

Inspiré de l'exemple SparkFun Inventor's Kit, légèrement ajusté. Voir le schéma Fritzing ci-dessous.

(Les images du schéma Fritzing et du montage réel sont disponibles dans le projet original.)

Code

Le jeu offre de nombreuses variantes. J'ai écrit le code from scratch en partant du câblage SparkFun. Si vous ne voulez pas coder, utilisez le mien ci-dessous, adaptez celui de SparkFun, ou cherchez d'autres codes compatibles.

Mon code inclut des commentaires détaillés et des Serial.print de débogage pour suivre le déroulement sur le moniteur série.

Vidéo du jeu en action

Note : Dans la vidéo, le nombre de séquences pour gagner est réglé sur 5 (modifiable via #define NUMBERTOWIN 5).

Maintenant, passez à la Partie 2 : Conception du PCB avec Upverter.

Code

Simon Says – Code Arduino

int ledArray[] = {12, 9, 5, 2};   // ledArray[0]=vert, [1]=rouge, [2]=jaune, [3]=bleu
int buttonArray[] = {13, 10, 6, 3}; // même ordre
int pinCount = 4;
int buzzer = 8;
int ledState = 0;
int gameState = 0;
#define NUMBERTOWIN 5  // nombre de séquences pour gagner – modifiez pour difficulté
int gameValues[NUMBERTOWIN];
int roundNum = 0;
unsigned long previousMillis = 0;
unsigned long interval1 = 500;

// Prototypes
void setLed(int ledNum);
void displayClue(int roundNum);
boolean waitForInput(int buttonNum);

void setup() {
  Serial.begin(9600);
  Serial.println("setup");
  for (int Pin = 0; Pin < pinCount; Pin++) {
    pinMode(ledArray[Pin], OUTPUT);
  }
  for (int Pin = 0; Pin < pinCount; Pin++) {
    pinMode(buttonArray[Pin], INPUT_PULLUP); // résistances internes
    // (j'ai ajouté des 10k externes après coup, optionnel)
  }
  randomSeed(analogRead(0)); // graine aléatoire sur bruit analogique
}

void loop() {
  if (gameState == 0) { // État pré-jeu : animation LEDs
    int b0 = digitalRead(buttonArray[0]);
    int b1 = digitalRead(buttonArray[1]);
    int b2 = digitalRead(buttonArray[2]);
    int b3 = digitalRead(buttonArray[3]);
    if (b0 && b1 && b2 && b3) { // aucun bouton pressé (pull-up = HIGH au repos)
      pre_game();
    } else { // un bouton pressé -> démarrage
      Serial.println("button Pressed");
      setLed(-1); // tout éteindre
      roundNum = 0;
      delay(1000);
      gameState = 1;
    }
  }
  if (gameState == 1) gamePlay();
  if (gameState == 2) { // Victoire
    delay(500);
    winning_sound();
    winDisplay(); winDisplay(); winDisplay(); loseDisplay();
    Serial.println("winDisplay");
    delay(1000);
    gameState = 0;
  }
  if (gameState == 3) { // Défaite
    delay(500);
    losing_sound();
    loseDisplay(); loseDisplay(); loseDisplay();
    Serial.println("loseDisplay");
    delay(1000);
    gameState = 0;
  }
}

void pre_game() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval1) {
    if (ledState == 0) { Serial.println("pre green"); setLed(0); ledState = 1; }
    else if (ledState == 1) { Serial.println("pre red"); setLed(1); ledState = 2; }
    else if (ledState == 2) { Serial.println("pre yellow"); setLed(2); ledState = 3; }
    else if (ledState == 3) { Serial.println("pre blue"); setLed(3); ledState = 0; }
    previousMillis = currentMillis;
  }
}

void gamePlay() {
  Serial.println("gamePlay");
  for (int i = 0; i < NUMBERTOWIN; i++) {
    gameValues[i] = floor(random(0, 4));
    Serial.println(gameValues[i], DEC);
  }
  Serial.print("Starting Round "); Serial.println(roundNum, DEC);
  for (int r = 0; r < NUMBERTOWIN; r++) {
    displayClue(roundNum);
    for (int i = 0; i <= roundNum; i++) {
      Serial.print("Waiting for input "); Serial.print(i+1, DEC);
      Serial.print(" of "); Serial.print(roundNum+1, DEC); Serial.println();
      if (waitForInput(gameValues[i])) {
        if (i == roundNum) {
          Serial.println("Correct");
          if (roundNum == NUMBERTOWIN - 1) {
            Serial.println("You Win!");
            gameState = 2; return;
          } else { Serial.println("Continue"); }
        }
      } else {
        Serial.println("You Lose!");
        gameState = 3; return;
      }
    }
    roundNum++;
    delay(1000);
  }
}

void setLed(int ledNum) {
  if (ledNum < 0) { // tout éteindre
    for (int i = 0; i < pinCount; i++) digitalWrite(ledArray[i], LOW);
  } else { // allumer seulement ledNum
    for (int i = 0; i < pinCount; i++) {
      digitalWrite(ledArray[i], (i == ledNum) ? HIGH : LOW);
    }
  }
}

void displayClue(int roundNum) {
  setLed(-1);
  for (int i = 0; i <= roundNum; i++) {
    setLed(gameValues[i]);
    delay(750);
    setLed(-1);
    delay(250);
  }
}

boolean waitForInput(int buttonNum) {
  while (true) {
    int b0 = digitalRead(buttonArray[0]);
    int b1 = digitalRead(buttonArray[1]);
    int b2 = digitalRead(buttonArray[2]);
    int b3 = digitalRead(buttonArray[3]);
    if (!b0 || !b1 || !b2 || !b3) { // un bouton pressé (LOW avec pull-up)
      delay(250); // anti-rebond
      // allumer LED correspondante
      if (buttonNum == 0) { digitalWrite(ledArray[0], HIGH); delay(250); digitalWrite(ledArray[0], LOW); }
      else if (buttonNum == 1) { digitalWrite(ledArray[1], HIGH); delay(250); digitalWrite(ledArray[1], LOW); }
      else if (buttonNum == 2) { digitalWrite(ledArray[2], HIGH); delay(250); digitalWrite(ledArray[2], LOW); }
      else if (buttonNum == 3) { digitalWrite(ledArray[3], HIGH); delay(250); digitalWrite(ledArray[3], LOW); }
      // vérification
      if (buttonNum == 0 && b0 == 0) { Serial.println("0 Pressed"); return true; }
      else if (buttonNum == 1 && b1 == 0) { Serial.println("1 Pressed"); return true; }
      else if (buttonNum == 2 && b2 == 0) { Serial.println("2 Pressed"); return true; }
      else if (buttonNum == 3 && b3 == 0) { Serial.println("3 Pressed"); return true; }
      else return false;
    }
  }
}

void winDisplay() {
  for (int i = 0; i < 4; i++) { digitalWrite(ledArray[i], HIGH); delay(75); digitalWrite(ledArray[i], LOW); delay(75); }
  for (int i = 2; i >= 0; i--) { digitalWrite(ledArray[i], HIGH); delay(75); digitalWrite(ledArray[i], LOW); delay(75); }
}

void winning_sound() {
  Serial.println("buzzer1");
  tone(buzzer, 294, 250); delay(200);
  tone(buzzer, 294, 250); delay(200);
  tone(buzzer, 294, 250); delay(200);
  tone(buzzer, 392, 500); delay(500);
  tone(buzzer, 392, 250); delay(200);
  tone(buzzer, 440, 250); delay(200);
  tone(buzzer, 392, 250); delay(200);
  tone(buzzer, 440, 250); delay(200);
  tone(buzzer, 494, 500);
}

void loseDisplay() {
  for (int i = 0; i < 4; i++) digitalWrite(ledArray[i], HIGH);
  delay(200);
  for (int i = 0; i < 4; i++) digitalWrite(ledArray[i], LOW);
  delay(200);
}

void losing_sound() {
  Serial.println("losing_sound");
  tone(buzzer, 98, 250); delay(250);
  tone(buzzer, 93, 250); delay(250);
  tone(buzzer, 87, 250); delay(250);
}

Schémas

Fichier Fritzing : simon_says_fritzing_Yb8EdKPqD4.fzz (disponible dans le projet original).

Processus de fabrication

  1. Construire un jeu gyroscopique temps réel avec Arduino Nano et capteur MPU-6050
  2. Dé numérique Arduino sur écran LCD 16x2 – Générateur aléatoire à bouton poussoir
  3. Créer votre propre jeu de roulette à 37 LED avec Arduino Nano
  4. Manette de jeu USB Arduino DIY – Construisez votre propre gamepad haute performance
  5. Jeu interactif joystick + LEDs avec Arduino
  6. Détecteur de distance portable – Kit DIY Arduino Nano avec HC-SR04, alimentation 18650 et boîtier imprimé 3D
  7. Pixel Chaser : Jeu LED interactif à une touche avec Arduino Nano
  8. Fabrication aéronautique pour petits ateliers : guide pratique
  9. Guide complet de la fabrication de PCB 4 couches – Processus étape par étape