Professional Documents
Culture Documents
Simon Dice
Simon Dice
#define NOTE_B0 31
#define melodyPin 3
#define led1 7
#define led2 6
#define led3 5
#define led4 4
int sensorValue = 0;
void setup() {
Serial.begin(9600);
pinMode(melodyPin,OUTPUT);
//led
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
Serial.println("Iniciado");
generarSecuencia();
}
void loop() {
if(juego) {
sensorValue = analogRead(sensorPin);
int boton = 5;
if(boton < 4) {
jugador[indice] = boton;
tocarNota(boton);
if(jugador[indice] == secuencia[indice]) {
Serial.println("Nota igual a secuencia");
indice++;
if(indice < nivel) {
Serial.println("Espera la siguiente nota");
} else {
Serial.println("completo nivel");
nivel++;
juego = false;
}
} else {
Serial.println("game over");
gameOver();
generarSecuencia();
}
}
} else {
if(nivel > 0) {
Serial.println("Reproduce melodia");
for (int i = 0; i < nivel; i = i + 1) {
Serial.print(secuencia[i]);
Serial.print(" ");
jugador[i] = -1;
tocarNota(secuencia[i]);
}
Serial.println(" ");
} else {
tocarNota(0);
Serial.println("Reproduce melodia");
//generarSecuencia();
}
juego = true;
indice = 0;
}
delay(200);
}
void generarSecuencia() {
Serial.println("Genera secuencia");
for (int i = 0; i < 80; i = i + 1) {
secuencia[i] = random(0,3);
jugador[i] = -1;
}
juego = false;
indice = 0;
nivel = 0;
}
void gameOver() {
int noteDuration = 1000 / 2;
buzz(melodyPin,NOTE_B0, noteDuration,0);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
buzz(melodyPin, 0, noteDuration,0);
}
void iniciar() {
int size = sizeof(melody) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
tocarNota(thisNote);
}
}