Skip to content

Instantly share code, notes, and snippets.

@ladyada
Created February 22, 2015 01:25
Show Gist options
  • Save ladyada/42e738bbc2d6014d127c to your computer and use it in GitHub Desktop.
Save ladyada/42e738bbc2d6014d127c to your computer and use it in GitHub Desktop.
daftpunkonsole.ino
#include <Wire.h>
#include "Adafruit_Trellis.h"
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
/************ sound board setup ***********/
// Choose any two pins that can be used with SoftwareSerial to RX & TX
#define SFX_TX 5
#define SFX_RX 6
// Connect to the RST pin on the Sound Board
#define SFX_RST 4
#define SFX_ACT 7 // the 'ACT'ivity LED, to tell us if we're still playing
// You can also monitor the ACT pin for when audio is playing!
// we'll be using software serial
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
// pass the software serial to Adafruit_soundboard, the second
// argument is the debug port (not used really) and the third
// arg is the reset pin
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
/************ Trellis setup ***********/
Adafruit_Trellis matrix0 = Adafruit_Trellis();
Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0);
// set to however many you're working with here, up to 8
#define NUMTRELLIS 1
#define numKeys (NUMTRELLIS * 16)
// Connect Trellis Vin to 5V and Ground to ground.
// Connect the INT wire to pin #A2 (can change later!)
#define INTPIN 2
// Connect I2C SDA pin to your Arduino SDA line
// Connect I2C SCL pin to your Arduino SCL line
char PadToTrack[numKeys][12] = {"DOIT WAV",
"MAKEIT WAV",
"MAKESENSWAV",
"HARDER WAV",
"BETTER WAV",
"FASTER WAV",
"STRONGERWAV",
"WORKIT WAV",
"EVER WAV",
"AFTER WAV",
};
/************ main setup ***********/
void setup() {
Serial.begin(115200);
Serial.println("Trellis Demo");
// INT pin requires a pullup
pinMode(INTPIN, INPUT);
digitalWrite(INTPIN, HIGH);
// ACT LED
pinMode(SFX_ACT, INPUT);
digitalWrite(SFX_ACT, HIGH); //pullup
// begin() with the addresses of each panel in order
trellis.begin(0x70); // only one
// softwareserial at 9600 baud
ss.begin(9600);
if (!sfx.reset()) {
Serial.println("SFX not found");
while (1);
}
Serial.println("SFX board found");
uint8_t files = sfx.listFiles();
Serial.println("File Listing");
Serial.println("========================");
Serial.println();
Serial.print("Found "); Serial.print(files); Serial.println(" Files");
for (uint8_t f=0; f<files; f++) {
Serial.print(f);
Serial.print("\tname: "); Serial.print(sfx.fileName(f));
Serial.print("\tsize: "); Serial.println(sfx.fileSize(f));
}
Serial.println("========================");
trellis.clear();
trellis.writeDisplay();
}
int currentPlaying = -1;
void loop() {
//delay(30); // 30ms delay is required, dont remove me!
if (digitalRead(SFX_ACT) && (currentPlaying != -1)) {
// *not* playing anything according to ACT lite
trellis.clear();
trellis.writeDisplay();
currentPlaying = -1;
}
// If a button was just pressed or released...
if (! digitalRead(INTPIN)) {
trellis.readSwitches();
// go through every button
for (uint8_t i=0; i<numKeys; i++) {
// if it was pressed, turn it on
if (trellis.isKeyPressed(i) && (i != currentPlaying)) {
Serial.print("v"); Serial.println(i);
trellis.clear();
if (! digitalRead(SFX_ACT)) {
Serial.println("stop..."); // check ACT lite first?
if (! sfx.stop() ) {
Serial.println("Failed to stop");
}
}
// play!
char * filename = PadToTrack[i];
int ret = sfx.playTrack(filename);
Serial.print("Playing "); Serial.println(filename);
if (! ret) {
Serial.println("Failed to play track?");
} else {
trellis.setLED(i);
}
trellis.writeDisplay();
/* for my debugging
int ms = 0;
while (digitalRead(SFX_ACT)) {
delay(1);
ms++;
}
Serial.println(ms);
*/
delay(25); // give it a chance to start playing
currentPlaying = i;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment