Help with project

Hi, Newbie Arduino programmer here!

For a school project, my group and I are trying to make a rock band style game for people with dementia. We are having some problems getting our code working the way we want. I would love any input as I am totally lost right now!

What we want is:

  1. Start button press = program start

  2. Starts “backgroud.wav” audio file from SD card and light/sound cue sequence
    -Turn button 1’s WS2812 RGB LED on/off at a set time from the start of “backgroud.wav” (4 LEDs per
    button, data line connected to each button e.g btn1 = led0, led1, led2, led3)

  • While button 1 LEDs are on, any button press (from btn1) will play an audio file from SD card (Play
    “sound1.wav”)

The audio file will change each time led is turned on throughout the song

The thing we need help with:

Changing the audio file a button plays each time it lights up

Creating a start button for the program

stopping the program and waiting for the start button to be repressed before restarting the light sequence

Any help would be amazing!

Code we currently have:

"//
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
//
#include <bitswap.h>
#include <chipsets.h>
#include <color.h>
#include <colorpalettes.h>
#include <colorutils.h>
#include <controller.h>
#include <cpp_compat.h>
#include <dmx.h>
#include <FastLED.h>
#include <fastled_config.h>
#include <fastled_delay.h>
#include <fastled_progmem.h>
#include <fastpin.h>
#include <fastspi.h>
#include <fastspi_bitbang.h>
#include <fastspi_dma.h>
#include <fastspi_nop.h>
#include <fastspi_ref.h>
#include <fastspi_types.h>
#include <hsv2rgb.h>
#include <led_sysdefs.h>
#include <lib8tion.h>
#include <noise.h>
#include <pixelset.h>
#include <pixeltypes.h>
#include <platforms.h>
#include <power_mgt.h>
//
#include <SPI.h>
#include <SD.h>
//
#define LED_PIN 2
#define NUM_LEDS 30
TMRpcm tmrpcm;
CRGB leds[NUM_LEDS];
int vol;
int bts;
int bt1;
int bt2;
int bt3;
int bt4;
int bt5;
int bt6;
int bt7;
int bt8;
void setup() {
//volume
pinMode(A0, INPUT);

//buttons
pinMode(22, INPUT);
pinMode(23, INPUT);
pinMode(24, INPUT);
pinMode(25, INPUT);
pinMode(26, INPUT);
pinMode(27, INPUT);
pinMode(28, INPUT);
pinMode(29, INPUT);
//
}

void loop() {
int vol = analogRead(A0);
tmrpcm.setVolume(vol);
int bt1 = digitalRead(22);
int bt2 = digitalRead(23);
int bt3 = digitalRead(24);
int bt4 = digitalRead(25);
int bt5 = digitalRead(26);
int bt6 = digitalRead(27);
int bt7 = digitalRead(28);
int bt8 = digitalRead(29);
//
tmrpcm.play(“background.wav”);
//
bts ++;
delay(681);
bts ++;
delay(681);
//
// Button 1
leds[0] = CRGB(50, 255, 20);
leds[1] = CRGB(50, 255, 20);
leds[2] = CRGB(50, 255, 20);
leds[3] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[0] = CRGB(0, 0, 0);
leds[1] = CRGB(0, 0, 0);
leds[2] = CRGB(0, 0, 0);
leds[3] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

// Button 2
leds[4] = CRGB(50, 255, 20);
leds[5] = CRGB(50, 255, 20);
leds[6] = CRGB(50, 255, 20);
leds[7] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[4] = CRGB(0, 0, 0);
leds[5] = CRGB(0, 0, 0);
leds[6] = CRGB(0, 0, 0);
leds[7] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

// Button 3
leds[7] = CRGB(50, 255, 20);
leds[8] = CRGB(50, 255, 20);
leds[9] = CRGB(50, 255, 20);
leds[10] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[7] = CRGB(0, 0, 0);
leds[8] = CRGB(0, 0, 0);
leds[9] = CRGB(0, 0, 0);
leds[10] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

// Button 4
leds[11] = CRGB(50, 255, 20);
leds[12] = CRGB(50, 255, 20);
leds[13] = CRGB(50, 255, 20);
leds[14] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[11] = CRGB(0, 0, 0);
leds[12] = CRGB(0, 0, 0);
leds[13] = CRGB(0, 0, 0);
leds[14] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

// Button 5
leds[15] = CRGB(50, 255, 20);
leds[16] = CRGB(50, 255, 20);
leds[17] = CRGB(50, 255, 20);
leds[18] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[15] = CRGB(0, 0, 0);
leds[16] = CRGB(0, 0, 0);
leds[17] = CRGB(0, 0, 0);
leds[18] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

// Button 6
leds[19] = CRGB(50, 255, 20);
leds[20] = CRGB(50, 255, 20);
leds[21] = CRGB(50, 255, 20);
leds[22] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[19] = CRGB(0, 0, 0);
leds[20] = CRGB(0, 0, 0);
leds[21] = CRGB(0, 0, 0);
leds[22] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

// Button 7
leds[23] = CRGB(50, 255, 20);
leds[24] = CRGB(50, 255, 20);
leds[25] = CRGB(50, 255, 20);
leds[26] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[23] = CRGB(0, 0, 0);
leds[24] = CRGB(0, 0, 0);
leds[25] = CRGB(0, 0, 0);
leds[26] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

// Button 8
leds[27] = CRGB(50, 255, 20);
leds[28] = CRGB(50, 255, 20);
leds[29] = CRGB(50, 255, 20);
leds[30] = CRGB(50, 255, 20);
FastLED.show();
delay(681);
//^time light is on and button is active for sound file
leds[27] = CRGB(0, 0, 0);
leds[28] = CRGB(0, 0, 0);
leds[29] = CRGB(0, 0, 0);
leds[30] = CRGB(0, 0, 0);
FastLED.show();
delay(681);
//^time to next button led on

//
if (bts == 1&&bt1==1){
tmrpcm.play(“sound1.wav”);
}

if (bts == 2&&bt2==1){
tmrpcm.play(“sound2.wav”);
}

if (bts == 3&&bt3==1){
tmrpcm.play(“sound3.wav”);
}

if (bts == 4&&bt4==1){
tmrpcm.play(“sound4.wav”);
}

if (bts == 5&&bt5==1){
tmrpcm.play(“sound5.wav”);
}

if (bts == 6&&bt6==1){
tmrpcm.play(“sound6.wav”);
}

if (bts == 7&&bt7==1){
tmrpcm.play(“sound7.wav”);
}

if (bts == 8&&bt8==1){
tmrpcm.play(“sound8.wav”);
}

//
if(bts == 9){
int bts = 0;
}
//
}
"

DESIGNFORHEALTH.ino (5.2 KB)

You’ve gone a bit mad with the include directives there. If you forget to include a file the compiler will let you know. You don’t need to include every header file available.

After pondering how to explain what you may need to do, I thought it may be easier to just provide you with a skeleton of how I might approach it. It is not complete, there may be errors, I may have misunderstood what you want, but there may be something useful in it for you:

#include <TMRpcm.h>
#include <FastLED.h>

#define NUM_LEDS    30
#define NUM_BUTTONS 8
#define LEDS_PER_BUTTON 4

const byte LedPin = 2;
const byte VolumePin = A0;
const byte StartButtonPin = 3;

TMRpcm tmrpcm;
CRGB leds[NUM_LEDS];
unsigned long nowMs, startTimeMs;
byte activeButton;

const byte ButtonPins[NUM_BUTTONS] = {
  22,
  23,
  24,
  25,
  26,
  27,
  28,
  29
};
const char * const ButtonFileNames[NUM_BUTTONS] = {
  "sound1.wav",
  "sound2.wav",
  "sound3.wav",
  "sound4.wav",
  "sound5.wav",
  "sound6.wav",
  "sound7.wav",
  "sound8.wav"
};
const byte ButtonLeds[NUM_BUTTONS][LEDS_PER_BUTTON] = {
  { 0, 1, 2, 3 },
  { 4, 5, 6, 7 },
  { 7, 8, 9, 10 },
  { 11, 12, 13, 14 },
  { 15, 16, 17, 18 },
  { 19, 20, 21, 22 },
  { 22, 23, 24, 25 },
  { 26, 27, 28, 29 }
};
const unsigned long ButtonTimesMs[NUM_BUTTONS] {
  681,
  1362,
  2043,
  2724,
  3405,
  4086,
  4767,
  5448
};

enum { Idle, FirstButton, NextButton, Waiting, Stopping } state;

void enableButton(byte buttonNumber, bool enable) {
  for (unsigned i = 0; i < LEDS_PER_BUTTON; ++i) {
    leds[ButtonLeds[buttonNumber][i]] = (enable) ? CRGB(50, 255, 20) : CRGB(0, 0, 0);
  }
  FastLED.show();
}

void setup() {
  pinMode(VolumePin, INPUT); // TODO: you could use INPUT_PULLUP and avoid an external pull-up/down resistor
  pinMode(StartButtonPin, INPUT);

  for (unsigned i = 0; i < NUM_BUTTONS; ++i) {
    pinMode(ButtonPins[i], INPUT);
  }
}

void loop() {
  nowMs = millis();

  //int vol = analogRead(VolumePin);
  //tmrpcm.setVolume(vol); // TODO: setVolume takes a char argument, an int of 0-1023 will overflow it; check how to use setVolume properly
  // you might find this function useful: https://www.arduino.cc/reference/en/language/functions/math/map/

  switch (state) {
    case Idle:
      if (digitalRead(StartButtonPin) == HIGH) {
        startTimeMs = nowMs;
        activeButton = 0;
        tmrpcm.play("background.wav"); // TODO: see MultiTrack_2Pins example to see how to do this correctly
        state = FirstButton;
      }
      break;
    case FirstButton:
      if (nowMs - startTimeMs > ButtonTimesMs[activeButton]) {
        enableButton(activeButton, true);
        state = Waiting;
      }
      break;
    case NextButton:
      //TODO:
      // if the last button has been processed, switch state to Stopping
      // otherwise, if we have reached the active button time, illuminate the active button and switch state to Waiting
      break;
    case Waiting:
      // TODO:
      // check if button corresponding to activeButton has been pressed
      // if yes then extinguish button, start playing button wav file, increment activeButton, switch state to NextButton
      // otherwise, see if we have exceeded the desired button illumination time, if so extinguish button, increment activeButton and switch state to NextButton
      break;
    case Stopping:
      //TODO:
      // whatever stop stuff, maybe wait for background to end
      // then back to Idle state
      break;
  }
}