注釈

こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。

参加する理由は?

  • エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。

  • 学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。

  • 独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。

  • 特別割引:最新製品の独占割引をお楽しみください。

  • 祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。

👉 私たちと一緒に探索し、創造する準備はできていますか?[ ここ]をクリックして今すぐ参加しましょう!

ゲーム - 数当てゲーム

数当てゲームは、あなたとあなたの友人が交代で数字(0~99)を入力するエンターテイメント性の高いパーティーゲームです。 各数字の入力により範囲が狭まり、プレイヤーが正解を当てるまで続きます。 正解を当てたプレイヤーは敗者と宣言され、罰を受けます。 例えば、秘密の数字が51であり、プレイヤー1が50を入力した場合、 数字の範囲のプロンプトは50~99に変わります。プレイヤー2が70を入力した場合、数字の範囲は50~70になります。 プレイヤー3が51を入力した場合、彼らが不運な人です。 このゲームでは、数字を入力するためにIRリモートコントローラを使用し、結果を表示するためにLCDを使用します。

必要なコンポーネント

このプロジェクトでは、以下のコンポーネントが必要です。

一式を購入するのが便利です。こちらがリンクです:

名称

このキットのアイテム数

リンク

Elite Explorer Kit

300+

Elite Explorer Kit

以下のリンクから個別に購入することもできます。

コンポーネント紹介

購入リンク

Arduino Uno R4 WiFi

-

ブレッドボード

購入

ジャンパーワイヤー

購入

I2C LCD1602

購入

赤外線レシーバー

購入

配線図

../_images/10_guess_number_bb.png

回路図

../_images/10_guess_number_schematic.png

コード

注釈

  • ファイル 10_guess_number.ino は、パス elite-explorer-kit-main\fun_project\10_guess_number で直接開けます。

  • または、このコードをArduino IDEにコピーしてください。

注釈

ライブラリをインストールするには、Arduinoライブラリマネージャーで 「IRremote」「LiquidCrystal I2C」 を検索し、インストールしてください。

10_guess_number.ino
  1/*
  2  This code is for an Arduino Uno R4 board setup with an I2C LCD1602 display 
  3  and an Infrared (IR) Receiver. The program facilitates a guessing game where 
  4  a random number is generated. The user then uses an IR remote control to guess 
  5  this number. Feedback is provided on the LCD1602 display, and the generated 
  6  random number is also displayed on the Serial Monitor.
  7
  8  Board: Arduino Uno R4 
  9  Component: I2C LCD1602 and Infrared Receiver
 10  Library: https://www.arduino.cc/reference/en/libraries/liquidcrystal-i2c/ (LiquidCrystal I2C by Frank de Brabander)
 11           https://github.com/Arduino-IRremote/Arduino-IRremote (IRremote by shirriff, z3t0, ArminJo)
 12*/
 13
 14
 15#include <Wire.h>
 16#include <LiquidCrystal_I2C.h>
 17#include <IRremote.h>
 18
 19const int IR_RECEIVE_PIN = 5;  // Define the pin number for the IR Sensor
 20String lastDecodedValue = "";  // Variable to store the last decoded value
 21
 22LiquidCrystal_I2C lcd(0x27, 16, 2);
 23
 24// Variables for game state
 25int currentGuess = 0;       // Current input number
 26int pointValue = 0;  // Target number
 27int upper = 99;      // Current upper limit for guessing
 28int lower = 0;       // Current lower limit for guessing
 29
 30void setup() {
 31  lcd.init();
 32  lcd.backlight();
 33  Serial.begin(9600);                                     // Start serial communication at 9600 baud rate
 34  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);  // Start the IR receiver
 35  initNewValue();                                         // Initialize a new game round
 36}
 37
 38void loop() {
 39  if (IrReceiver.decode()) {
 40    bool numberMatched = 0;
 41    // Serial.println(IrReceiver.decodedIRData.command);
 42    String num = decodeKeyValue(IrReceiver.decodedIRData.command);
 43    if (num != "ERROR" && num != lastDecodedValue) {
 44      Serial.println(num);
 45      lastDecodedValue = num;  // Update the last decoded value
 46    }
 47
 48    // Handle different IR commands
 49    if (num == "POWER") {
 50      initNewValue();  // Start new game if POWER button pressed
 51    } else if (num == "CYCLE") {
 52      numberMatched = detectPoint();
 53      lcdShowInput(numberMatched);
 54    } else if (num >= "0" && num <= "9") {
 55      currentGuess = currentGuess * 10;
 56      currentGuess += num.toInt();
 57      if (currentGuess >= 10) {
 58        numberMatched = detectPoint();
 59      }
 60      lcdShowInput(numberMatched);
 61    }
 62    IrReceiver.resume();  // Enable receiving of the next value
 63  }
 64}
 65
 66void initNewValue() {
 67
 68  // Generate a new target number
 69  randomSeed(analogRead(A0));  // Seed random number generator
 70  pointValue = random(99);     // Generate target number
 71
 72  upper = 99;
 73  lower = 0;
 74
 75  // Display welcome message
 76  lcd.clear();
 77  lcd.print("    Welcome!");
 78  lcd.setCursor(0, 1);
 79  lcd.print("  Guess Number!");
 80
 81  currentGuess = 0;
 82
 83  // Output target for debugging
 84  Serial.print("point is ");
 85  Serial.println(pointValue);
 86}
 87
 88bool detectPoint() {
 89  // Check if guess is correct, too high, or too low
 90  if (currentGuess > pointValue) {
 91    if (currentGuess < upper) upper = currentGuess;
 92  } else if (currentGuess < pointValue) {
 93    if (currentGuess > lower) lower = currentGuess;
 94  } else if (currentGuess == pointValue) {
 95    currentGuess = 0;
 96    return true;
 97  }
 98  currentGuess = 0;
 99  return false;
100}
101
102void lcdShowInput(bool numberMatched) {
103  lcd.clear();
104  if (numberMatched == 1) {
105    lcd.setCursor(0, 0);
106    lcd.print("The number is ");
107    lcd.print(pointValue);
108    lcd.setCursor(0, 1);
109    lcd.print(" You've got it! ");
110    delay(5000);
111    initNewValue();
112    return;
113  }
114  lcd.print("Enter number:");
115  lcd.print(currentGuess);
116  lcd.setCursor(0, 1);
117  lcd.print(lower);
118  lcd.print(" < Point < ");
119  lcd.print(upper);
120}
121
122
123String decodeKeyValue(long irCode) {
124  // Map IR codes to corresponding commands
125  switch (irCode) {
126    case 0x16:
127      return "0";
128    case 0xC:
129      return "1";
130    case 0x18:
131      return "2";
132    case 0x5E:
133      return "3";
134    case 0x8:
135      return "4";
136    case 0x1C:
137      return "5";
138    case 0x5A:
139      return "6";
140    case 0x42:
141      return "7";
142    case 0x52:
143      return "8";
144    case 0x4A:
145      return "9";
146    case 0x9:
147      return "+";
148    case 0x15:
149      return "-";
150    case 0x7:
151      return "EQ";
152    case 0xD:
153      return "U/SD";
154    case 0x19:
155      return "CYCLE";
156    case 0x44:
157      return "PLAY/PAUSE";
158    case 0x43:
159      return "FORWARD";
160    case 0x40:
161      return "BACKWARD";
162    case 0x45:
163      return "POWER";
164    case 0x47:
165      return "MUTE";
166    case 0x46:
167      return "MODE";
168    case 0x0:
169      return "ERROR";
170    default:
171      return "ERROR";
172  }
173}

どのように機能しますか?

  1. ライブラリのインポートとグローバル変数の定義:

    3つのライブラリがインポートされます:I2C通信用の Wire 、LCDディスプレイの制御用の LiquidCrystal_I2C 、赤外線リモートコントローラからの信号を受信する IRremote 。 ゲームの状態と設定を保存するためのいくつかのグローバル変数が定義されています。

  2. setup()

    LCDディスプレイを初期化し、バックライトをオンにします。 9600のボーレートでシリアル通信を開始します。 赤外線レシーバーを起動します。 初期ゲーム状態を設定するために initNewValue() 関数を呼び出します。

  3. loop()

    赤外線リモートコントローラからの信号が受信されたかどうかを確認します。 受信した赤外線信号をデコードします。 デコードされた値(数字またはコマンド)に基づいてゲームの状態を更新するか、対応するアクションを実行します。

  4. initNewValue()

    analogRead を使用してランダム数のシードを初期化し、毎回異なるランダム数が生成されることを保証します。 0から98の間でランダムな数字を生成し、それを当てる必要がある幸運な数字とします。 上限と下限のプロンプトをリセットします。 LCDにウェルカムメッセージを表示します。 入力された数字をリセットします。

  5. detectPoint()

    プレイヤーの入力した数字と幸運な数字の関係を確認します。 入力した数字が幸運な数字より大きい場合、上限のプロンプトを更新します。 入力した数字が幸運な数字より小さい場合、下限のプロンプトを更新します。 プレイヤーが正しい数字を入力した場合、入力をリセットし、trueを返します。

  6. lcdShowInput()

    プレイヤーの入力と現在の上限・下限のプロンプトをLCDに表示します。 プレイヤーが正しく当てた場合、成功メッセージを表示し、ゲームを再開する前に5秒間一時停止します。