Doorbell with Audio and LED strip

Status
Not open for further replies.

bogofoo

New member
I'm working up a 6-button doorbell for a shared office. The goal is to play a song snipped (WAV file) and animate a string of 77 LEDs for the duration of the song. Each playback/animation is triggered by a pushbutton. The following code works for shorter LED strips (10 are fine, but 20 aren't). With more LEDs, it latches during playback. The "latch" behavior is that it stops playing mid-file and emits a buzz via the audio out and the LED strip stops animating. I suspected an interrupt issue and switched from FastLED to the WS2812serial library which I read is non-blocking. However, the issue exists using either library. I suspect there is something problematic with my logic for testing buttons etc?

If anyone has suggestions, I'm happy to hear them! Thanks,

Code:
// Teensy 3.2 with Audio Shield implemented as 6-button doorbell

#include <Audio.h>
#include <SD.h>
#include <Bounce.h>
#include <WS2812Serial.h>

// How many leds are in the strip?
//const int numled = 77;
const int numled = 10;

// Data pin that led data will be written out over
const int pin = 1; 

byte drawingMemory[numled*3];         //  3 bytes per LED
DMAMEM byte displayMemory[numled*12]; // 12 bytes per LED

WS2812Serial leds(numled, displayMemory, drawingMemory, pin, WS2812_GRB);

#define RED    0xFF0000
#define GREEN  0x00FF00
#define BLUE   0x0000FF
#define YELLOW 0xFFFF00
#define PINK   0xFF1088
#define ORANGE 0xE05800
#define WHITE  0xFFFFFF
#define BLACK  0x000000

long randNumber;
const int buttonPin1 = 2;
Bounce pushbutton1 = Bounce(buttonPin1, 20);
const int buttonPin2 = 3;
Bounce pushbutton2 = Bounce(buttonPin2, 20);
const int buttonPin3 = 4;
Bounce pushbutton3 = Bounce(buttonPin3, 20);
const int buttonPin4 = 5;
Bounce pushbutton4 = Bounce(buttonPin4, 20);
const int buttonPin5 = 16;
Bounce pushbutton5 = Bounce(buttonPin5, 20);
const int buttonPin6 = 17;
Bounce pushbutton6 = Bounce(buttonPin6, 20);

AudioPlaySdWav           playSdWav1;
AudioOutputI2S           i2s1;
AudioConnection          patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection          patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;


void setup() {
  delay(2000);
  leds.begin();
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  pinMode(buttonPin6, INPUT_PULLUP);
  
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (!(SD.begin(10))) {
    while (1) {
      leds.setPixel(1, RED);
      leds.setPixel(2, RED); // indicates SD card error
      leds.show();
      delay(500);
    }
  }
}


void loop() {
  int microsec = 1500000 / leds.numPixels();
  
  pushbutton1.update();
  if (pushbutton1.fallingEdge()) {
    playSdWav1.play("925.wav");
    colorWipe(WHITE, microsec);
  }
  pushbutton2.update();
  if (pushbutton2.fallingEdge()) {
    playSdWav1.play("gtw.wav");
    colorWipe(BLUE, microsec);
  }
  pushbutton3.update();
  if (pushbutton3.fallingEdge()) {
    playSdWav1.play("ilikeit.wav");
    colorWipe(RED, microsec);
  }
  pushbutton4.update();
  if (pushbutton4.fallingEdge()) {
    playSdWav1.play("oops.wav");
    colorWipe(YELLOW, microsec);
  }
  pushbutton5.update();
  if (pushbutton5.fallingEdge()) {
    playSdWav1.play("queens.wav");
    colorWipe(PINK, microsec);
  }
  pushbutton6.update();
  if (pushbutton6.fallingEdge()) {
    playSdWav1.play("tiktok.wav");
    colorWipe(GREEN, microsec);
  }
  if ( playSdWav1.isPlaying() == 0 ) {
    delay(200);
    if (playSdWav1.isPlaying() == 0 ) {
      colorWipe(BLACK, 20);
    }
  }
}

void colorWipe(int color, int wait) {
  for (int i=0; i < leds.numPixels(); i++) {
    leds.setPixel(i, color);
  }
  leds.show();
  delay(200);
  delayMicroseconds(wait);
}
 
To follow up, this appears to be a power issue. I was neglecting the draw of bright LEDs and must have been browning-out the teensy. Using dimmer strips or higher current to the LEDs does the trick. Feeling silly, but at least it got me to finally post to the forum!
 
Status
Not open for further replies.
Back
Top