I need help with my code

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);
}

Welcome to the forum

What works and what doesn't ?

      if ((digitalRead(4) == HIGH) | (digitalRead(3) == HIGH)) {  //check for incorrect button press
        tmrpcm.play("incorrect.wav");                         //play wrong sound
        flash;
        z = 421;
      }

this bit for all 3 sections of it

im sorry if my code is not easy to work with. I'm new to Arduino and don't exactly know how to format everything

Have a pair of these ()

thank you! i put those there on all the other ones though so that doesn't explain it not working for all three of them right?

Confirm you want bitwise OR.

I think i found the problem. in the void flash() section (towards the top) it uses the randnum instead of the actual led pins thank you uk helibob and semperidem for helping me find the problem. have a good day!

Will logical OR make any meaningful difference?

yes i want it to go if 3 OR 2 is high

No, but the OP being new, should be aware of the difference as it may bite the in ass in other sketches.

Can you guys try to explain what you are saying simply i dont really understand. what is bitwise? and the difference between what?

| Bitwise OR

Use the Bitwise expression is when you want to modify a variable by changing some or any of the individual bits of the variable.

See


|| Logical OR

Use the Boolean expression when you want to know something about a variable

See

Logical Operators in Arduino IDE: OR (||), AND (&&), and NOT (!).

thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.