I am working on a project and need someone to help me with my code. Most of it works but i cant figure out what is wrong with it.
The goal is to have three lights above three buttons, and when the light turns on you have to press the button below it. if you get it correct (you hit the button underneath the powered led) it flashes the three lights in sequence plays a tune and repeats a random light.
If you get it wrong the powered led is supposed to flash twice play a incorrect buzzer sound and move on to any random light. the flash doesn't work and the sound doesn't play. the only thing that works is the switches.
here is the code:
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
int ledpin[] = {7, 6, 5}; // setting led ints
int buttonpin[] = {4, 3, 2}; // setting button ints
int randnum = 0; // setting random number ints
void setup() {
tmrpcm.speakerPin=9; //setting speaker pin
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin)){ //checking if SDcard is in place and functioning
Serial.println("SD fail");
return;
}
for (int x = 5; x < 14; x++) { //setting up led pins
pinMode(x, OUTPUT);
Serial.println(x);
}
for (int y = 2; y < 5; y++) { // setting up button pins
pinMode(y, OUTPUT);
Serial.println(y);
}
randomSeed(analogRead(0)); //preparing for random number generation
tmrpcm.setVolume(4); //setting volume
}
void light () { //creating correct lights
delay(250);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
delay(250);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
delay(250);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
delay(250);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
delay(250);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
delay(250);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
void flash() { //creating incorrect lights
delay(250);
digitalWrite(randnum, LOW);
delay(250);
digitalWrite(randnum, HIGH);
delay(250);
digitalWrite(randnum, LOW);
delay(250);
digitalWrite(randnum, HIGH);
}
void loop() {
randnum = random(3); //generating random number
Serial.println(randnum);
if (randnum == 0) {
digitalWrite(5, LOW); //turning on light
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
for (int v = 0; v < 421; v++) { //15 second loop
if ((digitalRead(3) == HIGH) | (digitalRead(2) == HIGH)) { //check for incorrect button press
tmrpcm.play("incorrect.wav"); //play wrong sound
flash();
v = 421;
}
Serial.println(digitalRead(4));
delay(31);
if (digitalRead(4) == HIGH) { //check for correct button press
tmrpcm.play("correct.wav"); //play right sound
light();
v = 421; //if correct button is pressed finish timer
}
}
}
if (randnum == 1) {
digitalWrite(5, LOW); //turning on light
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
for (int w = 0; w < 421; w++) { //15 second loop
if ((digitalRead(4) == HIGH) | (digitalRead(2) == HIGH)) { //check for incorrect button press
tmrpcm.play("incorrect.wav"); //play wrong sound
flash();
w = 421;
}
Serial.println(digitalRead(3));
delay(31);
if (digitalRead(3) == HIGH) { //check for correct button press
tmrpcm.play("correct.wav"); //play right sound
light();
w = 421; //if correct button is pressed finish timer
}
}
}
if (randnum == 2) {
digitalWrite(5, HIGH); //turning on light
digitalWrite(6, LOW);
digitalWrite(7, LOW);
for (int z = 0; z < 421; z++) { //15 second loop
if ((digitalRead(4) == HIGH) | (digitalRead(3) == HIGH)) { //check for incorrect button press
tmrpcm.play("incorrect.wav"); //play wrong sound
flash;
z = 421;
}
Serial.println(digitalRead(2));
delay(31);
if (digitalRead(2) == HIGH) { //check for correct button press
tmrpcm.play("correct.wav"); //play right sound
light();
z = 421; //if correct button is pressed finish timer
}
}
}
delay(400);
}