#include "pitches.h"
#define SPEAKER_PIN 8
byte modeMemory = 1;
byte modebattle = 2;
byte gameMode= 0;
/* Constants - define pin numbers for LEDs,
buttons and speaker, and also the game tones: */
const byte ledPins[] = {9, 10, 11, 12, 32, 33};
const byte buttonPins[] = {2, 3, 4, 5, 6, 7};
const byte button0 = 30;
const byte button1 = 31;
const byte pinsA[] = {15, 16, 17, 18 , 19 , 20 , 21};
const byte pinsB[] = {22, 23, 24 , 25 , 26 , 27 , 28};
#define MAX_GAME_LENGTH 25
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5, NOTE_A5, NOTE_C5};
/* Global variables - store the game state */
byte gameSequence[MAX_GAME_LENGTH] = {0};
byte gameIndex = 0;
byte sequence02[6] = {0, 1, 2, 3, 4, 5}; // LED 灯数量
static int State = 0;
int numform[10][7]={
// a,b,c,d,e,f,g
{0,0,0,0,0,0,1}, //0
{1,0,0,1,1,1,1}, //1
{0,0,1,0,0,1,0}, //2
{0,0,0,0,1,1,0}, //3
{1,0,0,1,1,0,0}, //4
{0,1,0,0,1,0,0}, //5
{0,1,0,0,0,0,0}, //6
{0,0,0,1,1,1,1}, //7
{0,0,0,0,0,0,0}, //8
{0,0,0,0,1,0,0}, //9
};
/*
Set up the Arduino board and initialize Serial communication
*/
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 6; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
for(byte i=15;i<29;i++){
pinMode(i, OUTPUT);
digitalWrite(i,1);
}
pinMode(button0, INPUT_PULLUP);
pinMode(button1, INPUT_PULLUP);
pinMode(SPEAKER_PIN, OUTPUT);
// The following line primes the random number generator.
// It assumes pin A0 is floating (disconnected):
randomSeed(analogRead(A0));
}
/**
Lights the given LED and plays a suitable tone
*/
void lightLedAndPlayTone(byte ledIndex,byte t) {
digitalWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
delay(t*100);
digitalWrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
delay(t*25);
}
/**
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];
lightLedAndPlayTone(currentLed);
delay(50);
}
}*/
/**
Waits until the user pressed one of the buttons,
and returns the index of that button
*/
byte readButtons() {
while (true) {
for (byte i = 0; i < 6; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
return i;
}
}
delay(1);
}
}
/**
Play the game over sequence, and report the game score
*/
void gameOver() {
delay(200);
// Play a Wah-Wah-Wah-Wah sound
tone(SPEAKER_PIN, NOTE_DS5);
delay(300);
tone(SPEAKER_PIN, NOTE_D5);
delay(300);
tone(SPEAKER_PIN, NOTE_CS5);
delay(300);
for (byte i = 0; i < 10; i++) {
for (int pitch = -10; pitch <= 10; pitch++) {
tone(SPEAKER_PIN, NOTE_C5 + pitch);
delay(5);
}
}
noTone(SPEAKER_PIN);
delay(500);
}
/**
Get the user's input and compare it with the expected sequence.
*/
bool checkUserSequence(byte length,byte t) {
for (byte i = 0; i < length; i++) {
byte expectedButton = sequence02[i];
byte actualButton = readButtons();
lightLedAndPlayTone(actualButton,t);
if (expectedButton != actualButton) {
return false;
}
}
return true;
}
/**
Plays a hooray sound whenever the user finishes a level
*/
void playLevelUpSound() {
tone(SPEAKER_PIN, NOTE_E4);
delay(150);
tone(SPEAKER_PIN, NOTE_G4);
delay(150);
tone(SPEAKER_PIN, NOTE_E5);
delay(150);
tone(SPEAKER_PIN, NOTE_C5);
delay(150);
tone(SPEAKER_PIN, NOTE_D5);
delay(150);
tone(SPEAKER_PIN, NOTE_G5);
delay(150);
noTone(SPEAKER_PIN);
}
void show(int x){
digitalWrite(pinsA[0],numform[x][0]);
digitalWrite(pinsA[1],numform[x][1]);
digitalWrite(pinsA[2],numform[x][2]);
digitalWrite(pinsA[3],numform[x][3]);
digitalWrite(pinsA[4],numform[x][4]);
digitalWrite(pinsA[5],numform[x][5]);
digitalWrite(pinsA[6],numform[x][6]);
}
void show2(int y){
digitalWrite(pinsB[0],numform[y][0]);
digitalWrite(pinsB[1],numform[y][1]);
digitalWrite(pinsB[2],numform[y][2]);
digitalWrite(pinsB[3],numform[y][3]);
digitalWrite(pinsB[4],numform[y][4]);
digitalWrite(pinsB[5],numform[y][5]);
digitalWrite(pinsB[6],numform[y][6]);
}
/*
void number10(){
if (gameIndex <= 10) {
show(0);
show2(gameIndex-1);
}
if (gameIndex > 10) {
show(gameIndex/10);
show2(gameIndex %10 -1);
if (gameIndex % 10 ==0) {
show((gameIndex-1)/10);
show2((gameIndex-1)%10);
}
}
}
*/
/*
void memoryGame(){
// Add a random color to the end of the sequence
gameSequence[gameIndex] =random(0, 4);
gameIndex++;
number10();
if (gameIndex >MAX_GAME_LENGTH) {
show(gameIndex/10);
show2(gameIndex%10-1);
playLevelUpSound();
Serial.print("Game over! your score: ");
Serial.println(gameIndex - 1);
gameIndex = 0;
gameMode=0;
}
playSequence();
if (!checkUserSequence1()) {
gameMode=0;
gameOver();
}
delay(300);
if (gameIndex > 0) {
playLevelUpSound();
delay(300);
}
}*/
void turn(byte *array) {
for (byte i = 5; i >0; i--) {
byte j = random(i + 1);
byte temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
void turnmode46(byte length,byte t) {
turn(sequence02);
for (byte i = 0; i <length; i++) {
lightLedAndPlayTone(sequence02[i],t);
delay(50); // 延时 100 毫秒
}
gameIndex++;
delay(100);
if (!checkUserSequence(length,t)){
Serial.print("Game over! your score: ");
Serial.println((gameIndex - 1)*10);
gameMode=0;
gameOver();
gameIndex = 0;
}
delay(300);
if (gameIndex > 0) {
playLevelUpSound();
show(gameIndex);
show2(0);
delay(300);
}
if (gameIndex > 4){
show(gameIndex);
show2(0);
playLevelUpSound();
Serial.print("Game over! your score: ");
Serial.println(gameIndex*10);
gameIndex = 0;
gameMode=0;
}
}
void memoryMode5level(){
if(gameMode==modeMemory){
switch(State){
case 0:
show(0);
show2(0);
delay(500);
turnmode46(4,3);
if (gameIndex > 0) State++;
else State = 0;
break;
case 1:
if(digitalRead(button0)==LOW){
delay(500);
turnmode46(6,3);
if (gameIndex > 1) State++;
else State = 0;
}
break;
case 2:
if(digitalRead(button0)==LOW){
delay(500);
turnmode46(4,2);
if (gameIndex > 2) State++;
else State = 0;
}
break;
case 3:
if(digitalRead(button0)==LOW){
delay(500);
turnmode46(6,2);
if (gameIndex > 3) State++;
else State = 0;
}
break;
case 4:
if(digitalRead(button0)==LOW){
delay(500);
turnmode46(6,1);
if (gameIndex > 4){
show(gameIndex);
show2(0);
playLevelUpSound();
Serial.print("Game over! your score: ");
Serial.println(gameIndex*10);
gameIndex = 0;
gameMode=0;
State = 0;
}
else State = 0;
}
break;
}
}
}
/**
The main game loop
*/
void loop() {
if(digitalRead(button0)==LOW){
gameMode=modeMemory;
}
memoryMode5level();
}