This will be my final post for the Holiday Special 20 project.  I was able to accomplish much of what I had planned, but failed to get the Christmas music to play from the external flash memory on the QT Py.  I'll use this post to describe the problems that I had and to summarize the project.  And I'll do a demo video.

 

Here are a couple of pictures of the completed construction (front and top view) which show the addition of the audio amplifier and speaker:

 

SamdAudio library problems

I had chosen to use the SamdAudio library because it supports playing WAV audio files from both SD card and external flash memory using a SAMD21 MCU.  I demonstrated in a previous post playing WAV audio from SD card on a Xiao Expansion board and in the following post I demonstrated loading and accessing a WAV file in the external flash memory on the QT Py.  So, all that remained was to add the library and play the audio from the flash memory.

 

Unfortunately, the devil is always in the details and I could not get the playback to work.  The first problem that I encountered was that the SamdAudio library was last updated about 2 years ago and it is no longer compatible with the current Adafruit SPIFlash library and it was trying to include files that no longer exist, so the program would not compile.  I thought about trying to find the earlier version of the Adafruit library that it used, but I found that others had encountered this problem and there was a forked version of the SamdAudio library that was updated to work with the new SPIFlash library (the SD part of the old library still works).   The new library is the Audio_FeatherM0 which was used on the Adafruit FeatherM0 (also uses a SAMD21 variant).

    

With the Audio_FeatherM0 library the program compiled and it could find the WAV file in the flash memory, but the playback did not work.  The audio library is non-blocking, which it achieves by using timers and interrupts for reading the flash memory and writing to the DAC.  The good news and bad news is that the SAMD MCUs have highly configurable timer/counters.  The bad news part is that there are a lot of registers that you need to get right to make a timer/counter work correctly (clock source, pre-scaler, output multiplexer, etc.).  Since this library has worked with other SAMD21s, the most likely issue is that there is a resource conflict with the clock that is being used for the timer/counter.  I tried a few different clocks, but I couldn't get it to work.  I'm reasonably confident that the flash memory read is working, but I'll need to defer the debug until I understand the SAMD21 better.

 

Sort of working demo

I decided that I would put together a demo of how it should have worked.  I'm going to use the Xiao Expansion board to play the music, but I'll trigger an interrupt to that board from the second cap sense button on the QT Py.

Everything almost worked ....

 

 

QT_Py_Christmas.ino

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#include "Adafruit_FreeTouch.h"


//#ifdef __AVR__
//  #include <avr/power.h>
//#endif


#define PIN 8
#define EXT_TRIG 6
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(20, PIN, NEO_GRB + NEO_KHZ800);


// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.


// Add the Fretouch pins
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(A2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
//Adafruit_FreeTouch qt_3 = Adafruit_FreeTouch(A3, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);


// Capacitive touch variables
int touch = 750;    // Threshold value for on and off
long oldState = 0;
long offState = 0;
int colorPattern =0;


// Display variables
int BRIGHTNESS = 20;


// Delay variables
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousOnBoardLedMillis = 0;   // will store last time the LED was updated


void setup() {
  pinMode(EXT_TRIG, OUTPUT);    // trigger output for Xiao to start playing music
  Serial.begin(115200);


  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


  // Clear the buffer
  display.clearDisplay();

  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(32,0);
  display.println("QT Py");
  display.setCursor(6,22);
  display.println("Merry Xmas");
  display.setCursor(12,44);
  display.println("Element14");
  display.display(); // Update screen with each newly-drawn rectangle 


  // Initialize NeoPixel ring
  strip.begin();
  strip.setBrightness(20);
  strip.show(); // Update all pixels


  // Initialize Freetouch pins
  if (! qt_1.begin())  
    Serial.println("Failed to begin qt on pin A1");
  if (! qt_2.begin())  
    Serial.println("Failed to begin qt on pin A2");

}


void loop() {

  currentMillis = millis();   // capture the latest value of millis()
                              //   this is equivalent to noting the time from a clock
                              //   use the same time for functions to keep them synchronized

  checkpress();   //check to see if the button's been pressed

}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}


void rainbow(uint8_t wait) {
  uint16_t i, j;


  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}


// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;


  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}


//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();


      delay(wait);


      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}


//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}


// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


// Fill the dots one after the other with a color
void tricolorWipe(uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i+=3) {
    strip.setPixelColor(i, strip.Color(255, 0, 0));  // red
    strip.setPixelColor(i+1, strip.Color(0, 255, 0));  // green
    strip.setPixelColor(i+2, strip.Color(0, 0, 255));  // blue
    strip.show();
    delay(wait);
  }
}

  // Check state of Freetouch pins
  void checkpress() {

// Get current button state.

    long newState =  qt_1.measure(); 
    long switchState = qt_2.measure(); 

  if (newState > touch) {  

  //select pattern to run
    switch(colorPattern){
      case 0:
              colorWipe(strip.Color(255, 0, 0), 30); // Red
              break;
      case 1:
              colorWipe(strip.Color(0, 255, 0), 50); // Green
              break;
      case 2:
              colorWipe(strip.Color(0, 0, 255), 50); // Blue
              break;
      case 3:
              tricolorWipe(50);
              break;              
      case 4:
              strip.setBrightness(0);
              strip.show(); // Update all pixels
              delay(300);
    }
  
    colorPattern++;
    if (colorPattern == 5){
      strip.setBrightness(20);
      colorPattern=0;
    }
  }


  if (switchState > touch) {
    // Clear the buffer
    display.clearDisplay();
  
    //display.drawRect(0, 0, 128, 15, SSD1306_WHITE);
    // display.drawRect(0, 17, 128, 47, SSD1306_WHITE);
    display.setTextColor(WHITE);
    display.setTextSize(2);
    //display.setCursor(48,4);
    display.setCursor(25,0);
    display.println("Playing");
    //display.setTextSize(2);
    display.setCursor(10,22);
    display.println("Christmas");
    display.setCursor(30,44);
    display.println("Music");
    display.display(); // Update screen with each newly-drawn rectangle 


    digitalWrite(EXT_TRIG, LOW);    // trigger Xiao interrupt to start playing music
  }
  
  // Set the last button state to the old state.
  oldState = newState;
  offState = switchState;


} 

 

Links to previous blogs

A QT Py Christmas

A QT Py Christmas - add Cap Sense Buttons

A QT Py Christmas - add Sound

A QT Py Christmas - Accessing external Flash memory