Skip to content

Instantly share code, notes, and snippets.

@mokjpn
Created December 26, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokjpn/cd95fee54b980f3191be to your computer and use it in GitHub Desktop.
Save mokjpn/cd95fee54b980f3191be to your computer and use it in GitHub Desktop.
/* SpeedButton Sketch */
/* NeoPixel */
#include <Adafruit_NeoPixel.h>
// Digital pin to connect NeoPixel
#define PIN 0
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
/* Touch Sensor (Touch Board) */
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4
// Touch Board mp3 player
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;
// mp3 behaviour defines
// By default, touching an electrode repeatedly will
// play the track again from the start each time.
//
// If you set this to FALSE, repeatedly touching an
// electrode will stop the track if it is already
// playing, or play it from the start if it is not.
// touch behaviour definitions
#define REPLAY_MODE TRUE
// Touch Board SD Card Slot
SdFat sd;
// Variables for the Speed Button
int buttonState = 0; // variable for reading the pushbutton status
bool answered = FALSE;
void setup() {
// Initialize Serial.
Serial.begin(115200);
Serial.println("Start");
// Initialize the NeoPixel.
pixels.begin();
colorLED(0, 0, 0);
// initialize the pushbutton pin as an input:
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
// initialize SD card slot
if (!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
// initialize Touch Sensor
if (!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
MPR121.setInterruptPin(MPR121_INT);
MPR121.setTouchThreshold(40);
MPR121.setReleaseThreshold(20);
// initialize MP3 Player
result = MP3player.begin();
MP3player.setVolume(10, 10);
if (result != 0) {
Serial.print("Error code: ");
Serial.print(result);
Serial.println(" when trying to start MP3 player");
}
}
// Change the Neopixel Ring's color
void colorLED(int r, int g, int b) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(r, g, b)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
}
void loop() {
// read the state of the Game Button value.
// if the button is pushed, change the NeoPixel's color and play music #1.
buttonState = digitalRead(10);
if (buttonState == LOW && !answered) {
Serial.println("PIN10 on");
colorLED(150, 150, 0);
answered = TRUE;
MP3player.stopTrack();
MP3player.playTrack(1);
}
buttonState = digitalRead(11);
if (buttonState == LOW && !answered) {
Serial.println("PIN11 on");
colorLED(0, 150, 0);
answered = TRUE;
MP3player.stopTrack();
MP3player.playTrack(1);
}
buttonState = digitalRead(12);
if (buttonState == LOW && !answered) {
Serial.println("PIN12 on");
colorLED(150, 0, 0);
answered = TRUE;
MP3player.stopTrack();
MP3player.playTrack(1);
}
buttonState = digitalRead(13);
if (buttonState == LOW && !answered) {
int b = digitalRead(10);
Serial.println("PIN13 on");
Serial.println(b);
colorLED(0, 150, 150);
answered = TRUE;
MP3player.stopTrack();
MP3player.playTrack(1);
}
// Read Touch Sensor. Take action according to which sensor is touched.
if (MPR121.touchStatusChanged()) {
MPR121.updateTouchData();
// only make an action if we have one or fewer pins touched
// ignore multiple touches
if (MPR121.getNumTouches() <= 1) {
for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
if (MPR121.isNewTouch(i)) {
//pin i was just touched
Serial.print("pin ");
Serial.print(i);
Serial.println(" was just touched");
switch (i) {
case 0:
// reset behaviour. Turn Off LEDs, and make the Game Buttons active
// (answered=FALSE).
colorLED(0, 0, 0);
answered = FALSE;
MP3player.stopTrack();
MP3player.playTrack(0);
break;
case 1:
// just play music #4
MP3player.stopTrack();
MP3player.playTrack(4);
break;
case 2:
// just play music #5
MP3player.stopTrack();
MP3player.playTrack(5);
break;
case 10:
// Play Music #2, and reset the status.
// (Answer is checked)
MP3player.stopTrack();
MP3player.playTrack(2);
colorLED(0, 0, 0);
answered = FALSE;
break;
case 11:
// Play Music #3, and reset the status.
// (Answer is checked)
MP3player.stopTrack();
MP3player.playTrack(3);
colorLED(0, 0, 0);
answered = FALSE;
break;
}
} else {
if (MPR121.isNewRelease(i)) {
Serial.print("pin ");
Serial.print(i);
Serial.println(" is no longer being touched");
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment