开启左侧

一个二进制的小游戏

[复制链接]
发表于 2016-11-30 11:16:07 | 显示全部楼层 |阅读模式
先来看看最终的效果吧!看完是不是跃跃欲试了呢?
FH8MGG9IV0B3UFI.LARGE.jpg

一、材料
1.Arduino控制板 x1
2.带自锁的按钮,开关皆可 x9
3.1602显示器(非IIC)x1
4.蜂鸣器 x1

如果不打算需要外壳可以不要以下材料

5.少量M3螺钉
6.3D打印机及耗材


二、软件
1.Arduino编程软件

三、步骤
1.调试开关或者按钮
  1. int ledPin = 13; // LED connected to digital pin 13
  2. int inPin = 7;   // pushbutton connected to digital pin 7
  3. int val = 0;     // variable to store the read value

  4. void setup()
  5. {
  6.   pinMode(ledPin, OUTPUT);      // sets the digital pin 13 as output
  7.   pinMode(inPin, INPUT);      // sets the digital pin 7 as input
  8. }

  9. void loop()
  10. {
  11.   val = digitalRead(inPin);   // read the input pin
  12.   digitalWrite(ledPin, val);    // sets the LED to the button's value
  13. }
复制代码
下载程序后操作开关(按钮),可以发现板子上的LED跟随按钮反应。
如果电平不稳定应采用上拉或者下拉连接,因该方式为基础,所以不再进行介绍。可自行百度,关键字 Arduino 上拉 下拉。
将如上操作重复9次以此来测试9个开关的好坏。

2.测试1602
  1. /*
  2.   LiquidCrystal Library - Hello World

  3. Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  4. library works with all LCD displays that are compatible with the
  5. Hitachi HD44780 driver. There are many of them out there, and you
  6. can usually tell them by the 16-pin interface.

  7. This sketch prints "Hello World!" to the LCD
  8. and shows the time.

  9.   The circuit:
  10. * LCD RS pin to digital pin 7
  11. * LCD Enable pin to digital pin 8
  12. * LCD D4 pin to digital pin 9
  13. * LCD D5 pin to digital pin 10
  14. * LCD D6 pin to digital pin 11
  15. * LCD D7 pin to digital pin 12
  16. * LCD R/W pin to ground
  17. * LCD VSS pin to ground
  18. * LCD VCC pin to 5V
  19. * 10K resistor:
  20. * ends to +5V and ground
  21. * wiper to LCD VO pin (pin 3)

  22. Library originally added 18 Apr 2008
  23. by David A. Mellis
  24. library modified 5 Jul 2009
  25. by Limor Fried (http://www.ladyada.net)
  26. example added 9 Jul 2009
  27. by Tom Igoe
  28. modified 22 Nov 2010
  29. by Tom Igoe

  30. This example code is in the public domain.

  31. http://www.arduino.cc/en/Tutorial/LiquidCrystal
  32. */

  33. // include the library code:
  34. #include <LiquidCrystal.h>

  35. // initialize the library with the numbers of the interface pins
  36. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

  37. void setup() {
  38.   // set up the LCD's number of columns and rows:
  39.   lcd.begin(16, 2);
  40.   // Print a message to the LCD.
  41.   lcd.print("hello, world!");
  42. }

  43. void loop() {
  44.   // set the cursor to column 0, line 1
  45.   // (note: line 1 is the second row, since counting begins with 0):
  46.   lcd.setCursor(0, 1);
  47.   // print the number of seconds since reset:
  48.   lcd.print(millis() / 1000);
  49. }
复制代码
完成接线,注意程序的开头注释已经告知了接线方式,并将如上程序下载进入Arduino。若成功则可以发现LCD上有hello world的字符显示。
若失败检查接线,并调整1602对比度。因1602也为基础,所以这里不多做解释,百度关键字 Arduino LCD1602。

若以上调整都不存在问题,则往下继续:
Q0zAskjGnieLqhaMqfoE.jpg
如图完成接线。
  1. // include the library code:
  2. #include <LiquidCrystal.h>

  3. // initialize the library with the numbers of the interface pins
  4. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

  5. const int Pin0 = 6;  // Binary number 2^0 or 1
  6. const int Pin1 = 5;  // Binary number 2^1 or 2
  7. const int Pin2 = 4;  // Binary number 2^2 or 4
  8. const int Pin3 = 3;  // Binary number 2^3 or 8
  9. const int Pin4 = A4; // Binary number 2^4 or 16
  10. const int Pin5 = A3; // Binary number 2^5 or 32
  11. const int Pin6 = A2; // Binary number 2^6 or 64
  12. const int Pin7 = A1; // Binary number 2^7 or 128
  13. const int easyMode = A0; // Easy mode is 0-15, Hard is 0-255

  14. int BinaryValue; // Value for adding up numbers to compare to random number

  15. int correctNumber = 0; // Flag to see if the number was correct
  16. int wrongNumber = 0; // Flag to see if the number was wrong

  17. const int buzzer = 13; //Buzzer pin
  18. int freq; //frequency out

  19. const int buttonPin = 2;     // the number of the pushbutton pin

  20. int buttonState;         // variable for reading the pushbutton status
  21. long randNumber;         // variable for the random number

  22. void setup() {

  23.   lcd.begin(16, 2); // set up the LCD's number of columns and rows

  24.   pinMode(Pin0, INPUT_PULLUP);
  25.   pinMode(Pin1, INPUT_PULLUP);
  26.   pinMode(Pin2, INPUT_PULLUP);
  27.   pinMode(Pin3, INPUT_PULLUP);
  28.   pinMode(Pin4, INPUT_PULLUP);
  29.   pinMode(Pin5, INPUT_PULLUP);
  30.   pinMode(Pin6, INPUT_PULLUP);
  31.   pinMode(Pin7, INPUT_PULLUP);

  32.   pinMode(easyMode, INPUT_PULLUP);

  33.   pinMode(buttonPin, INPUT_PULLUP);

  34.   pinMode(buzzer, OUTPUT); // Set buzzer pin as OUTPUT



  35.   // if analog input pin 5 is unconnected, random analog
  36.   // noise will cause the call to randomSeed() to generate
  37.   // different seed numbers each time the sketch runs.
  38.   // randomSeed() will then shuffle the random function.
  39.   randomSeed(analogRead(5));

  40.   if (digitalRead(easyMode) == HIGH)
  41.   {
  42.     randNumber = random(0, 15);
  43.   }
  44.   else
  45.   {
  46.     randNumber = random(0, 255);
  47.   }

  48.   // Print a message to the LCD.
  49.   lcd.print("Your number is");

  50.   // set the cursor to column 0, line 1
  51.   // (note: line 1 is the second row, since counting begins with 0):
  52.   lcd.setCursor(0, 1);

  53.   lcd.print(randNumber); // Print the random number

  54. }

  55. void checkNumber() // Check switches for correct number
  56. {
  57.   if (digitalRead(Pin0) == HIGH)
  58.   {
  59.     BinaryValue = 1;
  60.   }
  61.   else
  62.   {
  63.     BinaryValue = 0;
  64.   }

  65.   if (digitalRead(Pin1) == HIGH)
  66.   {
  67.     BinaryValue = BinaryValue + 2;
  68.   }

  69.   if (digitalRead(Pin2) == HIGH)
  70.   {
  71.     BinaryValue = BinaryValue + 4;
  72.   }

  73.   if (digitalRead(Pin3) == HIGH)
  74.   {
  75.     BinaryValue = BinaryValue + 8;
  76.   }

  77.   if (digitalRead(Pin4) == HIGH)
  78.   {
  79.     BinaryValue = BinaryValue + 16;
  80.   }

  81.   if (digitalRead(Pin5) == HIGH)
  82.   {
  83.     BinaryValue = BinaryValue + 32;
  84.   }

  85.   if (digitalRead(Pin6) == HIGH)
  86.   {
  87.     BinaryValue = BinaryValue + 64;
  88.   }

  89.   if (digitalRead(Pin7) == HIGH)
  90.   {
  91.     BinaryValue = BinaryValue + 128;
  92.   }

  93.   if (BinaryValue == randNumber) // Check if switches match random number
  94.   {
  95.     correctNumber = 1;
  96.   }
  97.   else
  98.   {
  99.     wrongNumber = 1;
  100.   }

  101. }

  102. void printBinary() // Displays status of switches
  103. {
  104.   if (digitalRead(Pin7) == LOW)
  105.   {
  106.     lcd.print("0");
  107.   }
  108.   else
  109.   {
  110.     lcd.print("1");
  111.   }

  112.   if (digitalRead(Pin6) == LOW)
  113.   {
  114.     lcd.print("0");
  115.   }
  116.   else
  117.   {
  118.     lcd.print("1");
  119.   }
  120.   if (digitalRead(Pin5) == LOW)
  121.   {
  122.     lcd.print("0");
  123.   }
  124.   else
  125.   {
  126.     lcd.print("1");
  127.   }
  128.   if (digitalRead(Pin4) == LOW)
  129.   {
  130.     lcd.print("0");
  131.   }
  132.   else
  133.   {
  134.     lcd.print("1");
  135.   }
  136.   lcd.print(" ");
  137.   if (digitalRead(Pin3) == LOW)
  138.   {
  139.     lcd.print("0");
  140.   }
  141.   else
  142.   {
  143.     lcd.print("1");
  144.   }
  145.   if (digitalRead(Pin2) == LOW)
  146.   {
  147.     lcd.print("0");
  148.   }
  149.   else
  150.   {
  151.     lcd.print("1");
  152.   }
  153.   if (digitalRead(Pin1) == LOW)
  154.   {
  155.     lcd.print("0");
  156.   }
  157.   else
  158.   {
  159.     lcd.print("1");
  160.   }
  161.   if (digitalRead(Pin0) == LOW)
  162.   {
  163.     lcd.print("0");
  164.   }
  165.   else
  166.   {
  167.     lcd.print("1");
  168.   }
  169. }

  170. void loop() {

  171.   attachInterrupt(digitalPinToInterrupt(buttonPin), checkNumber, FALLING); // Wait for pushbutton to be pressed, when pressed check to see if correct number is inputted

  172.   lcd.setCursor(7, 1);

  173.   printBinary(); // Display status of switches

  174.   if (wrongNumber == 1)
  175.   {

  176.     lcd.clear();
  177.     lcd.setCursor(0, 0);
  178.     lcd.print("Try again");
  179.     lcd.setCursor(0, 1);
  180.     lcd.print(randNumber);
  181.     tone(buzzer, 200); // Play wrong answer tone
  182.     delay(400);
  183.     noTone(buzzer);     // Stop sound...
  184.     wrongNumber = 0;
  185.     correctNumber = 0;
  186.   }

  187.   if (correctNumber == 1)
  188.   {
  189.     tone(buzzer, 600); // Play correct answer tone
  190.     delay(100);
  191.     tone(buzzer, 1000);
  192.     delay(100);
  193.     tone(buzzer, 800);
  194.     delay(100);
  195.     noTone(buzzer);     // Stop sound...
  196.     lcd.clear();
  197.     lcd.setCursor(0, 0);
  198.     lcd.print("Correct!");
  199.     lcd.setCursor(0, 1);
  200.     lcd.print(randNumber);
  201.     lcd.print(" is ");

  202.     printBinary(); // Display status of switches

  203.     delay(3000);

  204.     lcd.clear();
  205.     lcd.setCursor(0, 0);
  206.     // Print a message to the LCD.
  207.     lcd.print("Your number is");
  208.     // set the cursor to column 0, line 1
  209.     // (note: line 1 is the second row, since counting begins with 0):
  210.     lcd.setCursor(0, 1);

  211.     if (digitalRead(easyMode) == HIGH)
  212.     {
  213.       randNumber = random(0, 15);
  214.     }
  215.     else
  216.     {
  217.       randNumber = random(0, 255);
  218.     }


  219.     lcd.print(randNumber);

  220.     correctNumber = 0;
  221.     wrongNumber = 0;
  222.   }

  223. }
复制代码
下载如下程序。即可开始游戏。

游戏玩法:
选择难度,系统将随机跳出十进制数字,你需要经过换算以后,计算出对应的二进制数,然后通过8个开关输入进去,二进制最高位在8个开关的最左侧。
简单模式是15以内的数字,困难模式是255以内的数字。

若你具有外壳,那可以做出如下的效果:


以上是所有教程,这里加一个附加题
因该接法的1602占用过多IO口,所以可以改成通过IIC控制的1602,可以自行学习一下如何修改。

文末贴出原文地址和该小作品的Github,感谢作者无私提出如此创意:
https://create.arduino.cc/projec ... rending___&offset=5
https://github.com/keebie81/BinaryGame

回复

使用道具 举报

发表于 2016-12-2 10:44:04 | 显示全部楼层
一级棒!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩课程推荐

关于我们

帮助中心

商务合作

点击这里给我发消息

关注微信公众号

Copyright © 2016-2017 i创学院 - 上海享渔教育科技有限公司 Powered by Discuz!     ( 沪ICP备16033954号-1 )