// WAV files converted to code by wav2sketch
#include "AudioSampleSnare.h" // http://www.freesound.org/people/KEVOY/sounds/82583/
#include "AudioSampleTomtom.h" // http://www.freesound.org/people/zgump/sounds/86334/
#include "AudioSampleHihat.h" // http://www.freesound.org/people/mhc/sounds/102790/
#include "AudioSampleKick.h" // http://www.freesound.org/people/DWSD/sounds/171104/
#include "AudioSampleGong.h" // http://www.freesound.org/people/juskiddink/sounds/86773/
#include "AudioSampleCashregister.h" // http://www.freesound.org/people/kiddpark/sounds/201159/
/////LED//////
#include <FastLED.h>
#define LED_PIN 12
#define NUM_LEDS 4
CRGB leds[NUM_LEDS];
///////////////////
#include <Bounce.h>
#include "pitches.h"
const int notes[] = {
AudioSampleSnare, AudioSampleTomtom, AudioSampleHihat, AudioSampleKick, AudioSampleGong, AudioSampleCashregister, AudioSampleTomtom, AudioSampleTomtom
};
int melody1[64];
int melody2[64];
//int envelopeArr1[32];
//int envelopeArr2[32];
bool arrToggle = false;
///////CAP TOUCH///////////////////
#include <Wire.h>
#include "Adafruit_MPR121.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlayMemory playMem1; //xy=197,57
AudioPlayMemory playMem2; //xy=207,115
AudioPlayMemory playMem3; //xy=212,169
AudioPlayMemory playMem4; //xy=237,246
AudioMixer4 mixer1; //xy=460,182
AudioOutputI2S i2s1; //xy=761,184
AudioConnection patchCord1(playMem1, 0, mixer1, 0);
AudioConnection patchCord2(playMem2, 0, mixer1, 1);
AudioConnection patchCord3(playMem3, 0, mixer1, 2);
AudioConnection patchCord4(playMem4, 0, mixer1, 3);
AudioConnection patchCord5(mixer1, 0, i2s1, 0);
AudioConnection patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=545,444
// GUItool: end automatically generated code
//CAP TOUCH Vars//
Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
// Bounce objects to read pushbuttons
Bounce recButton = Bounce(0, 15);
Bounce playButton = Bounce(1, 15);
Bounce stopButton = Bounce(2, 15);
//Bounce button4 = Bounce(3, 15);
//Bounce button5 = Bounce(4, 15);
//Bounce button6 = Bounce(5, 15);
//Bounce button7 = Bounce(6, 15);
//Bounce button8 = Bounce(8, 15);
//TEMPO Vars//
elapsedMillis blinkTime;
elapsedMillis loopTime;
int tempoPin = A0;
int millisBPM = 0;
int bars;
int bpm;
int tempoCounter = 0;
int barsCounter = 0;
bool didPlay = false;
bool didBarPlay = false;
///////////////////
void setup() {
Serial.begin(9600);
//int timing[];
////////LED Vars//////
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
//////////////////
while (!Serial) { // needed to keep leonardo/micro from starting too fast!
delay(10);
}
// Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
// Serial.println("MPR121 not found, check wiring?");
while (1);
}
// Serial.println("MPR121 found!");
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
// pinMode(3, INPUT_PULLUP);
// pinMode(4, INPUT_PULLUP);
// pinMode(5, INPUT_PULLUP);
// pinMode(6, INPUT_PULLUP);
// pinMode(8, INPUT_PULLUP);
AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
mixer1.gain(0, 0.4);
mixer1.gain(1, 0.4);
mixer1.gain(2, 0.4);
mixer1.gain(3, 0.4);
//Envelope///
// envelope1.attack(0.3);
// envelope1.hold(0);
// envelope1.decay(0.1);
// envelope1.sustain(0.3);
// envelope1.release(1000);
}
bool preCount = false;
bool isRec = false;
bool isPlay = false;
void loop() {
////////////MAKE LEDS BLACK////////////
if (!isPlay && !isRec){
for (int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Black;
FastLED.show();
}
}
/////////////////BPM///////////////////////
bpm = map(analogRead(tempoPin),0,1023,60,140);
millisBPM = 60000/bpm;
bars = millisBPM/2;
///////////BUTTON CHECK////////////
recButton.update();
if (recButton.fallingEdge()){
preCount = true;
barsCounter = 0;
memset(melody1, 0, sizeof(melody1));
memset(melody2, 0, sizeof(melody2));
Serial.println("record");
arrToggle = false;
}
playButton.update();
if (playButton.fallingEdge()){
barsCounter = 0;
isPlay = true;
Serial.println("play");
}
stopButton.update();
if (stopButton.fallingEdge()){
isPlay = false;
isRec = false;
tempoCounter = 0;
barsCounter = 0;
// envelope1.noteOff();
Serial.println("stop");
}
////////////PRECOUNT/////////////////////
if (preCount){
playMem1.play(AudioSampleTomtom);
leds[0] = CRGB::Red;
FastLED.show();
delay(millisBPM);
playMem1.play(AudioSampleSnare);
leds[0] = CRGB::Green;
FastLED.show();
delay(millisBPM);
playMem1.play(AudioSampleSnare);
leds[0] = CRGB::Green;
FastLED.show();
delay(millisBPM);
playMem1.play(AudioSampleSnare);
leds[0] = CRGB::Green;
FastLED.show();
delay(millisBPM);
playMem1.play(AudioSampleTomtom);
leds[0] = CRGB::Red;
FastLED.show();
delay(millisBPM);
playMem1.play(AudioSampleSnare);
leds[0] = CRGB::Green;
FastLED.show();
delay(millisBPM);
playMem1.play(AudioSampleSnare);
leds[0] = CRGB::Green;
FastLED.show();
delay(millisBPM);
playMem1.play(AudioSampleSnare);
leds[0] = CRGB::Green;
FastLED.show();
delay(millisBPM);
isRec = true;
}
preCount = false;
///////////////////SEQUENCE////////////////////
if(isRec || isPlay){
//////////////////Couner of bars///////////
if(blinkTime > millisBPM/4 ){
barsCounter ++;
didPlay = false;
didBarPlay = false;
blinkTime = 0;
// envelope1.noteOff();
Serial.println(barsCounter);
}
///////////////////CLICK////////////////////
if (barsCounter == 4 && !didPlay){
leds[3] = CRGB::Black;
// Serial.println('!');
// Serial.println(bpm);
playMem1.play(AudioSampleTomtom);
didPlay = true;
leds[0] = CRGB::Red;
FastLED.show();
} else if (barsCounter == 8 && !didPlay){
leds[0] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[1] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 12 && !didPlay){
leds[1] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[2] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 16 && !didPlay){
leds[2] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[3] = CRGB::Green;
FastLED.show();
} else if(barsCounter == 20 && !didPlay){
leds[3] = CRGB::Black;
// Serial.println('!');
// Serial.println(bpm);
playMem1.play(AudioSampleTomtom);
didPlay = true;
leds[0] = CRGB::Red;
FastLED.show();
} else if (barsCounter == 24 && !didPlay){
leds[0] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[1] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 28 && !didPlay){
leds[1] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[2] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 32 && !didPlay){
leds[2] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[3] = CRGB::Green;
FastLED.show();
}else if(barsCounter == 36 && !didPlay){
leds[3] = CRGB::Black;
// Serial.println('!');
// Serial.println(bpm);
playMem1.play(AudioSampleTomtom);
didPlay = true;
leds[0] = CRGB::Red;
FastLED.show();
} else if (barsCounter == 40 && !didPlay){
leds[0] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[1] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 44 && !didPlay){
leds[1] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[2] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 48 && !didPlay){
leds[2] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[3] = CRGB::Green;
FastLED.show();
}else if(barsCounter == 52 && !didPlay){
leds[3] = CRGB::Black;
// Serial.println('!');
// Serial.println(bpm);
playMem1.play(AudioSampleTomtom);
didPlay = true;
leds[0] = CRGB::Red;
FastLED.show();
} else if (barsCounter == 56 && !didPlay){
leds[0] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[1] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 60 && !didPlay){
leds[1] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[2] = CRGB::Green;
FastLED.show();
} else if (barsCounter == 64 && !didPlay){
leds[2] = CRGB::Black;
// Serial.println('/');
playMem1.play(AudioSampleSnare);
didPlay = true;
leds[3] = CRGB::Green;
FastLED.show();
}
}
///////////////////PLAY THE LOOP//////////////////
if (isPlay){
if(didBarPlay == false){
if(arrToggle == true){
if(melody1[barsCounter]!= NULL){
if(melody1[barsCounter] != melody1[barsCounter-1]){
// waveform1.begin(0.75,melody1[barsCounter],WAVEFORM_SAWTOOTH);
// waveform1.pulseWidth(0.15);
//// envelope1.noteOn();
}
} else{
// envelope1.noteOff();
}
}else if(arrToggle == false){
if(melody2[barsCounter]!= NULL){
if(melody2[barsCounter] != melody2[barsCounter-1]){
// waveform1.begin(0.75,melody2[barsCounter],WAVEFORM_SAWTOOTH);
// waveform1.pulseWidth(0.15);
//// envelope1.noteOn();
}
}else{
// envelope1.noteOff();
}
}
}
didBarPlay = true;
}
////////////////Counter reset/////////////////
if (barsCounter >= 64){
barsCounter = 0;
if (isPlay){
if (arrToggle){
Serial.println("playing array 1");
// for(int i = 0; i < sizeof(melody1); i++){
// Serial.println(melody1[i]);
//
// }
} else {
Serial.println("playing array 2");
}
}
if (isRec){
arrToggle = !arrToggle;
if (arrToggle){
memset(melody2, 0, sizeof(melody2));
}else {
memset(melody1, 0, sizeof(melody1));
}
}
}
//////////////KEYBOARD////////////////////
currtouched = cap.touched();
for (uint8_t i=0; i<12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
playMem2.play(AudioSampleHihat);
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
// envelope1.noteOff();
}
if(isRec){ ////////////////REC/////////////////
// if((currtouched & _BV(i))){
// if (!arrToggle){
// melody1[barsCounter] = notes[i];
// }else{
// melody2[barsCounter] = notes[i];
// }
// }
// }
}
}
// reset our state
lasttouched = currtouched;
}