UNO——simom game
学到什么
1.用UNO编写simom game程序
2.理解索引(Index)的用法
点击simom game开始在线仿真
西蒙是一个简单的电子记忆游戏:用户必须重复一个不断增长的颜色序列。通过点亮 LED 来显示序列。每种颜色还具有相应的色调。
在每个回合中,游戏将播放序列,然后等待用户根据颜色序列按下按钮来重复序列。如果用户正确重复了序列,游戏将播放"升级"声音,在序列末尾添加新颜色,然后移动到下一个回合。
游戏将继续,直到用户犯错。然后播放一个声音游戏,游戏重新启动。
硬件
项目 | 数量 | 笔记 |
---|---|---|
Arduino Uno R3 | 1 | |
5 毫米发光二极管 | 4 | 红色、绿色、蓝色和黄色 |
12 毫米按钮 | 4 | 红色、绿色、蓝色和黄色 |
电阻器 | 4 | 220Ω |
压电蜂鸣器 | 1 |
图
引脚连接
Arduino Pin | 装置 |
---|---|
12 | 红色指示灯 |
11 | 绿色发光二极管 |
10 | 蓝色发光二极管 |
9 | 黄色指示灯 |
8 | 蜂鸣器 |
5 | 红色按钮 |
4 | 绿色按钮 |
3 | 蓝色按钮 |
2 | 黄色按钮 |
- LED通过每个220Ω电阻连接。
/**
Simon Game for Arduino
Copyright (C) 2016, Uri Shaked
Released under the MIT License.
*/
#include "pitches.h"
/* Constants - define pin numbers for LEDs,
buttons and speaker, and also the game tones: */
const byte ledPins[] = {
9, 10, 11, 12};
const byte buttonPins[] = {
2, 3, 4, 5};
#define SPEAKER_PIN 8//扬声器端口
#define MAX_GAME_LENGTH 100//长度
const int gameTones[] = {
NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};//音调
/* Global variables - store the game state */
/*全局变量-存储游戏状态*/
byte gameSequence[MAX_GAME_LENGTH] = {
0};
byte gameIndex = 0;//游戏索引
/**
Set up the Arduino board and initialize Serial communication
*/
voi