Taiko-Nijiiro-Analog-IOBoard/sanro/sanro.ino

108 lines
3.0 KiB
Arduino
Raw Normal View History

2017-11-21 23:34:09 +01:00
/***************************************************************
* *
* Taiko Sanro - Arduino *
* Support Arduino models with ATmega32u4 microprocessors *
* *
* Shiky Chang Chris *
* zhangxunpx@gmail.com wisaly@gmail.com *
* *
***************************************************************/
// New implementation using fast, stable and sensitive piezoelectric
// ceramic sensors (the same sensors used in electirc drum kit).
// No longer need microphones.
#define CHANNELS 2
2017-11-25 04:13:36 +01:00
#define SAMPLE_CACHE_LENGTH 10
2017-11-21 23:34:09 +01:00
2017-11-25 04:13:36 +01:00
#define DON_SILENCE_THRES 1e4
#define DON_LIGHT_THRES 3e4
2017-11-24 21:17:11 +01:00
#define DON_HEAVY_THRES 1e5
2017-11-21 23:34:09 +01:00
2017-11-25 04:13:36 +01:00
#define KAT_SILENCE_THRES 4e3
2017-11-24 21:17:11 +01:00
#define KAT_LIGHT_THRES 5e3
#define KAT_HEAVY_THRES 1e4
2017-11-21 23:34:09 +01:00
2017-11-25 04:13:36 +01:00
#define FORCED_FREQ 1000
2017-11-21 23:34:09 +01:00
2017-11-24 21:17:11 +01:00
#include "cache.h"
2017-11-21 23:34:09 +01:00
2017-11-24 21:17:11 +01:00
unsigned long lastTime;
2017-11-21 23:34:09 +01:00
2017-11-24 21:17:11 +01:00
float channelSample[CHANNELS];
Cache <float, SAMPLE_CACHE_LENGTH> sampleCache[CHANNELS];
float power[CHANNELS];
2017-11-21 23:34:09 +01:00
2017-11-25 04:13:36 +01:00
bool triggered;
2017-11-24 21:17:11 +01:00
float light_thres[] = {DON_LIGHT_THRES, KAT_LIGHT_THRES};
float heavy_thres[] = {DON_HEAVY_THRES, KAT_HEAVY_THRES};
float silence_thres[] = {DON_SILENCE_THRES, KAT_SILENCE_THRES};
2017-11-21 23:34:09 +01:00
2017-11-22 05:25:19 +01:00
int input_pins[] = {A0, A1}; // Don, Kat
int output_pins[] = {A2, A3, A4, A5}; // Left Don, Left Kat, Right Don, Right Kat
2017-11-21 23:34:09 +01:00
2017-11-25 04:13:36 +01:00
String debug_output[] = {"D", "K"};
2017-11-24 21:17:11 +01:00
2017-11-21 23:34:09 +01:00
void setup() {
2017-11-22 05:25:19 +01:00
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
2017-11-24 21:17:11 +01:00
Serial.begin (115200);
2017-11-21 23:34:09 +01:00
for (short int i = 0; i < CHANNELS; i++) {
2017-11-25 04:13:36 +01:00
power [i] = 0;
2017-11-21 23:34:09 +01:00
}
2017-11-25 04:13:36 +01:00
triggered = false;
2017-11-21 23:34:09 +01:00
lastTime = 0;
}
void loop() {
2017-11-24 21:17:11 +01:00
lastTime = micros ();
2017-11-21 23:34:09 +01:00
for (short int i = 0; i < CHANNELS; i++) {
2017-11-22 05:25:19 +01:00
channelSample[i] = analogRead(input_pins[i]);
2017-11-25 04:13:36 +01:00
sampleCache[i].put(channelSample[i]);
2017-11-21 23:34:09 +01:00
2017-11-25 04:13:36 +01:00
long int tempInt = sampleCache[i].get(1);
2017-11-22 05:25:19 +01:00
power[i] -= tempInt * tempInt;
tempInt = sampleCache[i].get();
power[i] += tempInt * tempInt;
2017-11-25 04:13:36 +01:00
if (power[i] > light_thres[i]) {
if (!triggered) {
2017-11-24 21:17:11 +01:00
digitalWrite(output_pins[i], LOW);
if (power[i] >= heavy_thres[i]) {
2017-11-22 05:25:19 +01:00
digitalWrite(output_pins[i] + 2, LOW);
2017-11-21 23:34:09 +01:00
}
2017-11-24 21:17:11 +01:00
digitalWrite(LED_BUILTIN, HIGH);
2017-11-25 04:13:36 +01:00
Serial.println(debug_output[i]);
2017-11-21 23:34:09 +01:00
}
2017-11-25 04:13:36 +01:00
triggered = true;
2017-11-22 05:25:19 +01:00
}
2017-11-21 23:34:09 +01:00
}
2017-11-25 04:13:36 +01:00
for (short int i = 0; i < CHANNELS; i++) {
triggered = false;
digitalWrite(output_pins[i], HIGH);
digitalWrite(output_pins[i] + 2, HIGH);
if (power[i] > silence_thres[i]) {
digitalWrite(LED_BUILTIN, LOW);
triggered = true;
break;
}
}
// Serial.println(power[0]);
float delayTime = 1e6 / FORCED_FREQ - (micros() - lastTime);
if (delayTime > 0) {
longMicroDelay(delayTime);
} else {
// digitalWrite(LED_BUILTIN, HIGH);
}
2017-11-24 21:17:11 +01:00
}
void longMicroDelay (float microTime) {
delay (microTime / 1000);
delayMicroseconds (microTime - floor(microTime / 1000) * 1000);
2017-11-21 23:34:09 +01:00
}