当前位置:网站首页>Arduino tutorial - Simon games
Arduino tutorial - Simon games
2022-07-06 06:53:00 【Wonderful drifting of coal】
Simon game Simon Game
《 Simon game 》 Is a puzzle leisure games , The rules of the game are , Let players remember the lighting sequence of lights of different colors , Click on the lights in turn , If the order is the same as AI The order of giving is the same , Then the game continues and increases the difficulty , Otherwise the game is over , Reset game .
On-line arduino Simon game
Code
/** Simon Game for ATTiny85 Copyright (C) 2018, Uri Shaked Licensed under the MIT License. */
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include "pitches.h"
/* Constants - define pin numbers for leds, buttons and speaker, and also the game tones */
byte buttonPins[] = {
1, 2, 3, 4};
#define SPEAKER_PIN 0
#define MAX_GAME_LENGTH 100
int gameTones[] = {
NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
/* Global variales - store the game state */
byte gameSequence[MAX_GAME_LENGTH] = {
0};
byte gameIndex = 0;
/** Set up the GPIO pins */
void setup() {
// The following line primes the random number generator. It assumes pin A0 is floating (disconnected)
randomSeed(analogRead(1));
// Disable ADC - saves about 324.5uA in sleep mode!
ADCSRA = 0;
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, INPUT);
}
ISR(PCINT0_vect) {
GIMSK &= ~0b00100000; // Turn off pin change interrupts
sleep_disable();
}
void sleep() {
sleep_enable();
noInterrupts();
GIMSK |= 0b00100000; // Turn on pin change interrupts
for (byte i = 0; i < 4; i++) {
PCMSK |= 1 << buttonPins[i];
}
interrupts();
sleep_cpu();
}
// The sound-producing function
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{
// http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
int x;
long delayAmount = (long)(1000000 / frequencyInHertz);
long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));
pinMode(speakerPin, OUTPUT);
for (x = 0; x < loopTime; x++) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin, LOW);
delayMicroseconds(delayAmount);
}
pinMode(speakerPin, INPUT);
}
/** Lights the given led and plays the suitable tone */
void lightLedAndPlaySound(byte ledIndex) {
pinMode(buttonPins[ledIndex], OUTPUT);
digitalWrite(buttonPins[ledIndex], LOW);
beep(SPEAKER_PIN, gameTones[ledIndex], 300);
pinMode(buttonPins[ledIndex], INPUT_PULLUP);
}
/** Plays the current sequence of notes that the user has to repeat */
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
lightLedAndPlaySound(currentLed);
delay(50);
}
}
/** Waits until the user pressed one of the buttons, and returns the index of that button */
byte readButton() {
for (;;) {
for (int i = 0; i < 4; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
return i;
}
}
sleep();
}
}
/** Plays the tone associated with a specific button + debouncing mechanism */
void playButtonTone(byte buttonIndex) {
beep(SPEAKER_PIN, gameTones[buttonIndex], 150);
// Wait until button is released.
while (digitalRead(buttonPins[buttonIndex]) == LOW);
delay(50);
}
/** Play the game over sequence, and report the game score */
void gameOver() {
gameIndex = 0;
delay(200);
// Play a Wah-Wah-Wah-Wah sound
beep(SPEAKER_PIN, NOTE_DS5, 300);
beep(SPEAKER_PIN, NOTE_D5, 300);
beep(SPEAKER_PIN, NOTE_CS5, 300);
for (int i = 0; i < 200; i++) {
beep(SPEAKER_PIN, NOTE_C5 + (i % 20 - 10), 5);
}
delay(500);
}
/** Get the user input and compare it with the expected sequence. If the user fails, play the game over sequence and restart the game. */
void checkUserSequence() {
for (int i = 0; i < gameIndex; i++) {
byte expectedButton = gameSequence[i];
byte actualButton = readButton();
playButtonTone(actualButton);
if (expectedButton == actualButton) {
/* good */
} else {
gameOver();
return;
}
}
}
/** Plays an hooray sound whenever the user finishes a level */
void levelUp() {
beep(SPEAKER_PIN, NOTE_E4, 150);
beep(SPEAKER_PIN, NOTE_G4, 150);
beep(SPEAKER_PIN, NOTE_E5, 150);
beep(SPEAKER_PIN, NOTE_C5, 150);
beep(SPEAKER_PIN, NOTE_D5, 150);
beep(SPEAKER_PIN, NOTE_G5, 150);
}
/** The main game loop */
void loop() {
// Add a random color to the end of the sequence
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
playSequence();
checkUserSequence();
delay(300);
if (gameIndex > 0) {
levelUp();
delay(300);
}
}
边栏推荐
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- Huawei equipment configuration ospf-bgp linkage
- UDP攻击是什么意思?UDP攻击防范措施
- LeetCode - 152 乘积最大子数组
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
- Machine learning plant leaf recognition
- Automated test environment configuration
- CS certificate fingerprint modification
- Successfully solved typeerror: data type 'category' not understood
- 顶测分享:想转行,这些问题一定要考虑清楚!
猜你喜欢

Reflex WMS中阶系列3:显示已发货可换组

指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
![[advanced software testing step 1] basic knowledge of automated testing](/img/3d/f83f792e24efc39f00c0dc33936ce8.png)
[advanced software testing step 1] basic knowledge of automated testing

Fedora/REHL 安装 semanage

RichView TRVStyle 模板样式的设置与使用
![[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)

Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks

Thesis abstract translation, multilingual pure human translation

How to reconstruct the class explosion caused by m*n strategies?

How to translate professional papers and write English abstracts better
随机推荐
Basic commands of MySQL
Use shortcut LNK online CS
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
Reflex WMS中阶系列3:显示已发货可换组
Py06 dictionary mapping dictionary nested key does not exist test key sorting
Biomedical English contract translation, characteristics of Vocabulary Translation
How to convert flv file to MP4 file? A simple solution
Huawei equipment configuration ospf-bgp linkage
电子书-CHM-上线CS
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Leetcode - 152 product maximum subarray
GET 和 POST 请求类型的区别
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Pallet management in SAP SD delivery process
Data security -- 13 -- data security lifecycle management
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
ROS学习_基础