proton/ProtonPack.ino

104 lines
1.9 KiB
Arduino
Raw Normal View History

// Proton Pack with NeoPixels
// Boy howdy do these make everything easy
2016-09-25 16:57:13 -06:00
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>
#include "MusicPlayer.h"
#include "Synchrotron.h"
// Music Player
#define MUSIC_CS 7
#define MUSIC_DATA 6
#define MUSIC_CARDCS 4
#define MUSIC_REQ 3
MusicPlayer *music;
// Synchrotron
#define SYNC1_NPIXELS 24
#define SYNC1_DATA 5
Synchrotron *sync1;
// Debug LED
#define DEBUG 13
// Inputs
2016-09-25 22:43:31 -06:00
#define TRIGGER 8
2016-09-25 16:57:13 -06:00
// global time counter
2016-09-25 16:57:13 -06:00
unsigned long jiffies = 0;
void setup() {
randomSeed(analogRead(12));
// inputs
2016-09-25 16:57:13 -06:00
pinMode(TRIGGER, INPUT_PULLUP);
// outputs
pinMode(DEBUG, OUTPUT);
// music player, this sets up SPI for us
music = new MusicPlayer(MUSIC_CS, MUSIC_DATA, MUSIC_REQ, MUSIC_CARDCS);
2016-09-25 16:57:13 -06:00
// synchrotron
sync1 = new Synchrotron(SYNC1_NPIXELS, SYNC1_DATA);
2016-09-25 16:57:13 -06:00
}
void flashDebug() {
uint8_t val;
val = (jiffies % 100) < 50;
digitalWrite(DEBUG, val);
2016-09-25 16:57:13 -06:00
}
void loop() {
static int state = 0;
// 6 seems to be about what my overly-critical brain needs to buffer out
// any music player delays so that they're unnoticeable
unsigned long new_jiffies = millis() / 6;
boolean trigger = ! digitalRead(TRIGGER);
2016-09-25 16:57:13 -06:00
music->poll(jiffies);
2016-09-25 22:43:31 -06:00
switch (state) {
case 0:
if (trigger && music->startPlayingFile("track001.mp3")) {
state = 1;
sync1->charge();
}
2016-09-25 22:43:31 -06:00
break;
case 1:
if (! music->isPlaying()) {
state = 3;
}
break;
case 3:
if (trigger && music->startPlayingFile("track003.mp3")) {
state = 4;
sync1->fire();
2016-09-25 22:43:31 -06:00
}
break;
case 4:
if (! trigger && music->startPlayingFile("track002.mp3")) {
state = 5;
sync1->discharge();
}
break;
case 5:
2016-09-25 22:43:31 -06:00
if (! music->isPlaying()) {
state = 0;
}
break;
}
if (new_jiffies > jiffies) {
jiffies = new_jiffies;
sync1->tick(jiffies);
flashDebug();
}
2016-09-25 16:57:13 -06:00
}