Skip to content

Instantly share code, notes, and snippets.

@AKAMEDIASYSTEM
Created March 12, 2017 20:02
Show Gist options
  • Save AKAMEDIASYSTEM/ebd470c2e6054bd51f467e72606a702a to your computer and use it in GitHub Desktop.
Save AKAMEDIASYSTEM/ebd470c2e6054bd51f467e72606a702a to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#define NUM_PIXELS 60 // number of pixels on one strip
#define PIX_PIN 2 // neopixels
#define sigPin 21 // input from touch traces
#define THRESHOLD 600 // analogRead threshold
int WHITE_LEVEL = 50; // 0-255 white brightness
#define RING_1 10 // number of LEDs in this ring
#define RING_2 20 // number of LEDs in this ring
#define RING_3 30 // number of LEDs in this ring
#define RING_4 40 // number of LEDs in this ring
int FADETIME = 500; // ms to take to fade the pixels
int DEBOUNCE_TIME = 200; //debounce time
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIX_PIN, NEO_GRBW + NEO_KHZ800);
elapsedMillis sinceSig;
// GUItool: begin automatically generated code
AudioPlaySdWav playWav1; //xy=317,272
AudioOutputI2S audioOutput; //xy=624,286
AudioConnection patchCord1(playWav1, 0, audioOutput, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=560,221
// GUItool: end automatically generated code
// Use these with the audio adaptor board
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
int state = 0; // this tracks state transisions 0-6
/*
* 0: nothing happening
* 1: 1st ring lit, wait for touch
* 2: 1st + 2nd ring lit, wait for touch
* 3: 1st + 2nd + 3rd ring lit, wait for touch
* 4: 1st + 2nd + 3rd + 4th ring lit, wait for touch
* 5: 1st + 2nd + 3rd + 4th ring lit PLUS playsound and immediately move to state 6
* 6: fade out all rings while sound decays, then move ot state 0
*/
void setup() {
Serial.begin(9600);
pinMode(PIX_PIN, OUTPUT);
pinMode(sigPin, INPUT_PULLDOWN);
pixels.begin();
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(8);
// Comment these out if not using the audio adaptor board.
// This may wait forever if the SDA & SCL pins lack
// pullup resistors
sgtl5000_1.enable();
sgtl5000_1.volume(0.5); // lowered this a lot to attempt to fix distortion, has little effect
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
delay(500);
Serial.println("ring 1");
lightRing(1);
delay(1000);
Serial.println("ring 2");
lightRing(2);
delay(1000);
Serial.println("ring 3");
lightRing(3);
delay(1000);
Serial.println("ring 4");
lightRing(4);
delay(1000);
dingBell();
delay(2000);
}
void loop() {
int senseRead = analogRead(sigPin);
if( (senseRead > THRESHOLD) && (sinceSig > DEBOUNCE_TIME)){ // will have to determine THRESHOLD val in final install
sinceSig = 0;
increment_state();
}
switch(state){
case 0:
// normal waiting state
// Serial.println("case 0");
break;
case 1:
// light first ring, wait for timeout
// Serial.println("case 1");
lightRing(1);
break;
case 2:
// light next ring, wait for timeout
// Serial.println("case 2");
lightRing(2);
break;
case 3:
// light next ring, wait for timeout
// Serial.println("case 3");
lightRing(3);
break;
case 4:
// light next ring, wait for timeout
// Serial.println("case 4");
lightRing(4);
break;
case 5:
//ding bell and fade all 4 rings
// Serial.println("case 5");
dingBell();
break;
}
}
void increment_state(){
state = ((state+1) % 6);
Serial.print("incremented state, it is now ");
Serial.println(state);
sinceSig = 0;
}
void playFile(const char *filename) {
Serial.print("Playing file: ");
Serial.println(filename);
// Start playing the file. This sketch continues to
// run while the file plays.
AudioNoInterrupts();
playWav1.play(filename);
// A brief delay for the library read WAV info
delay(5);
AudioInterrupts();
}
void dingBell() {
// delay(100);
if (random(10) >= 5) {
// playFile("SDBELL1.WAV");
playFile("SDTEST1.WAV");
} else {
// playFile("SDBELL2.WAV");
playFile("SDTEST2.WAV");
}
delay(2500); // only after adding this did the playback become less distorted (still not perfect)
fadeRings(4);
}
void lightRing(int num) {
// light #num rings warm white
// don't forget strip.display() at the end
switch (num) {
case 1:
for (int i = 0; i < RING_1; i++) {
pixels.setPixelColor(i, 0, 0, 0, WHITE_LEVEL);
}
break;
case 2:
for (int i = 0; i < RING_2; i++) {
pixels.setPixelColor(i, 0, 0, 0, WHITE_LEVEL);
}
break;
case 3:
for (int i = 0; i < RING_3; i++) {
pixels.setPixelColor(i, 0, 0, 0, WHITE_LEVEL);
}
break;
case 4:
for (int i = 0; i < RING_4; i++) {
pixels.setPixelColor(i, 0, 0, 0, WHITE_LEVEL);
}
break;
}
pixels.show();
// state = 0;
}
void fadeRings(int num) {
// fade all rings for a set time, the same time it takes the sound to decay
float tempLevel;
for (int j = FADETIME; j > 0; j--) {
tempLevel = ((float)j / (float)FADETIME);
tempLevel = tempLevel * WHITE_LEVEL;
switch (num) {
case 1:
for (int i = 0; i < RING_1; i++) {
pixels.setPixelColor(i, 0, 0, 0, int(tempLevel));
}
break;
case 2:
for (int i = 0; i < RING_2; i++) {
pixels.setPixelColor(i, 0, 0, 0, int(tempLevel));
}
break;
case 3:
for (int i = 0; i < RING_3; i++) {
pixels.setPixelColor(i, 0, 0, 0, int(tempLevel));
}
break;
case 4:
for (int i = 0; i < RING_4; i++) {
pixels.setPixelColor(i, 0, 0, 0, int(tempLevel));
}
break;
}
pixels.show();
delay(1); // this is to make each loop a lil more than 1ms
}
state = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment