I'm working on a project that when a button is pressed will fade on/off multiple LEDs (two in the code below) for a specified time and play an audio file. Right now the code as is just constantly fades the LEDs on/off without the button press. The button works to play the audio file but when it is playing the LEDs just go to a high state and stop fading until the audio file stops.
The code I have so far seems to be close but I'm not sure if it can do what I'm wanting or if there a lines in the code just in the wrong place.
Using a Teensy 4.1 with the Audio Shield Rev. D
Thanks for any insight...
//Play a .wav file and flash LEDs when button is pressed:
//Only flashes LEDs when button is not pressed and playing wav file,
//otherwise LEDs run forever. When button is pressed LEDs just go
//into HIGH state (all on)
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
const int buttonPin = 33; // Pin connected to the button
const int ledPin = 13; // Pin connected to the onboard LED
const int chipSelect = BUILTIN_SDCARD;
const int ledPin1 = 36; // Pin for the first LED
const int ledPin2 = 14; // Pin for the second LED
unsigned long previousMillis1 = 0; // Stores the previous time for LED 1
unsigned long previousMillis2 = 0; // Stores the previous time for LED 2
const long interval1 = 18; // Interval between brightness changes for LED 1
const long interval2 = 25; // Interval between brightness changes for LED 2
int brightness1 = 0; // Current brightness of the first LED
int brightness2 = 0; // Current brightness of the second LED
int fadeAmount1 = 5; // How much to fade LED 1
int fadeAmount2 = 3; // How much to fade LED 2
bool isPlaying = false; // Flag to indicate if the audio is playing
AudioPlaySdWav playSdWav1;
AudioOutputI2S i2s1;
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
AudioMemory(20);
sgtl5000_1.enable();
sgtl5000_1.volume(0.4);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
Serial.println("Setup complete.");
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to change brightness for LED 1
if (currentMillis - previousMillis1 >= interval1) {
// Save the last time you changed brightness for LED 1
previousMillis1 = currentMillis;
// Update the brightness of LED 1
analogWrite(ledPin1, brightness1);
// Change brightness for LED 1
brightness1 = brightness1 + fadeAmount1;
// Reverse direction when reaching max or min brightness for LED 1
if (brightness1 <= 0 || brightness1 >= 255) {
fadeAmount1 = -fadeAmount1;
}
if (digitalRead(buttonPin) == LOW && !isPlaying) { // If button is pressed and audio is not playing
digitalWrite(ledPin, HIGH); // Turn on LED to indicate playback
isPlaying = true;
Serial.println("Playing .wav file");
playSdWav1.play("Tiger.wav"); // Play the .wav file
while (playSdWav1.isPlaying()) { // Wait until playback is finished
delay(100);
}
isPlaying = false;
digitalWrite(ledPin, LOW);// Turn off LED
}
}
// Check if it's time to change brightness for LED 2
if (currentMillis - previousMillis2 >= interval2) {
// Save the last time you changed brightness for LED 2
previousMillis2 = currentMillis;
// Update the brightness of LED 2
analogWrite(ledPin2, brightness2);
// Change brightness for LED 2
brightness2 = brightness2 + fadeAmount2;
// Reverse direction when reaching max or min brightness for LED 2
if (brightness2 <= 0 || brightness2 >= 255) {
fadeAmount2 = -fadeAmount2;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
}
The code I have so far seems to be close but I'm not sure if it can do what I'm wanting or if there a lines in the code just in the wrong place.
Using a Teensy 4.1 with the Audio Shield Rev. D
Thanks for any insight...
//Play a .wav file and flash LEDs when button is pressed:
//Only flashes LEDs when button is not pressed and playing wav file,
//otherwise LEDs run forever. When button is pressed LEDs just go
//into HIGH state (all on)
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
const int buttonPin = 33; // Pin connected to the button
const int ledPin = 13; // Pin connected to the onboard LED
const int chipSelect = BUILTIN_SDCARD;
const int ledPin1 = 36; // Pin for the first LED
const int ledPin2 = 14; // Pin for the second LED
unsigned long previousMillis1 = 0; // Stores the previous time for LED 1
unsigned long previousMillis2 = 0; // Stores the previous time for LED 2
const long interval1 = 18; // Interval between brightness changes for LED 1
const long interval2 = 25; // Interval between brightness changes for LED 2
int brightness1 = 0; // Current brightness of the first LED
int brightness2 = 0; // Current brightness of the second LED
int fadeAmount1 = 5; // How much to fade LED 1
int fadeAmount2 = 3; // How much to fade LED 2
bool isPlaying = false; // Flag to indicate if the audio is playing
AudioPlaySdWav playSdWav1;
AudioOutputI2S i2s1;
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
AudioMemory(20);
sgtl5000_1.enable();
sgtl5000_1.volume(0.4);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
Serial.println("Setup complete.");
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to change brightness for LED 1
if (currentMillis - previousMillis1 >= interval1) {
// Save the last time you changed brightness for LED 1
previousMillis1 = currentMillis;
// Update the brightness of LED 1
analogWrite(ledPin1, brightness1);
// Change brightness for LED 1
brightness1 = brightness1 + fadeAmount1;
// Reverse direction when reaching max or min brightness for LED 1
if (brightness1 <= 0 || brightness1 >= 255) {
fadeAmount1 = -fadeAmount1;
}
if (digitalRead(buttonPin) == LOW && !isPlaying) { // If button is pressed and audio is not playing
digitalWrite(ledPin, HIGH); // Turn on LED to indicate playback
isPlaying = true;
Serial.println("Playing .wav file");
playSdWav1.play("Tiger.wav"); // Play the .wav file
while (playSdWav1.isPlaying()) { // Wait until playback is finished
delay(100);
}
isPlaying = false;
digitalWrite(ledPin, LOW);// Turn off LED
}
}
// Check if it's time to change brightness for LED 2
if (currentMillis - previousMillis2 >= interval2) {
// Save the last time you changed brightness for LED 2
previousMillis2 = currentMillis;
// Update the brightness of LED 2
analogWrite(ledPin2, brightness2);
// Change brightness for LED 2
brightness2 = brightness2 + fadeAmount2;
// Reverse direction when reaching max or min brightness for LED 2
if (brightness2 <= 0 || brightness2 >= 255) {
fadeAmount2 = -fadeAmount2;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
}