Update... everything?

This commit is contained in:
Xun Zhang 2023-12-21 17:54:39 -08:00
parent 9209d75058
commit b45a052cdc
13 changed files with 111 additions and 6748 deletions

View file

@ -1,107 +1,80 @@
/***************************************************************
* *
* Taiko Sanro - Arduino *
* Support Arduino models with ATmega32u4 microprocessors *
* *
* Shiky Chang Chris *
* zhangxunpx@gmail.com wisaly@gmail.com *
* *
***************************************************************/
#define CHANNELS 4
#define SAMPLE_CACHE_LENGTH 16 // Must be power of 2 (8, 16, etc.); See cache.h for implementation
#define HIT_THRES 750 // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you
#define RESET_THRES 300 // changed SAMPLE_CACHE_LENGTH, you must adjust thresholds here
// New implementation using fast, stable and sensitive piezoelectric
// ceramic sensors (the same sensors used in electirc drum kit).
// No longer need microphones.
#define DEBUG 0
#define CHANNELS 2
#define SAMPLE_CACHE_LENGTH 10
#define DON_SILENCE_THRES 1e4
#define DON_LIGHT_THRES 3e4
#define DON_HEAVY_THRES 1e5
#define KAT_SILENCE_THRES 4e3
#define KAT_LIGHT_THRES 5e3
#define KAT_HEAVY_THRES 1e4
#define FORCED_FREQ 1000
#include <Keyboard.h>
#include <limits.h>
#include "cache.h"
unsigned long lastTime;
unsigned long int lastTime;
float channelSample[CHANNELS];
Cache <float, SAMPLE_CACHE_LENGTH> sampleCache[CHANNELS];
float power[CHANNELS];
Cache<int, SAMPLE_CACHE_LENGTH> inputWindow[CHANNELS];
unsigned long power[CHANNELS];
unsigned long lastPower[CHANNELS];
bool triggered;
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};
unsigned long triggeredTime[CHANNELS];
int input_pins[] = {A0, A1}; // Don, Kat
int output_pins[] = {A2, A3, A4, A5}; // Left Don, Left Kat, Right Don, Right Kat
const byte inPins[] = {A0, A1, A2, A3}; // L don, L kat, R don, R kat
const byte outPins[] = {5, 6, 7, 8}; // LED visualization (optional)
const char outKeys[] = {'f', 'd', 'j', 'k'}; // L don, L kat, R don, R kat
String debug_output[] = {"D", "K"};
float sensitivity[] = {1.0, 1.0, 1.0, 1.0};
short maxIndex;
float maxPower;
void setup() {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
Serial.begin (115200);
for (short int i = 0; i < CHANNELS; i++) {
power [i] = 0;
}
triggered = false;
lastTime = 0;
Serial.begin(115200);
Keyboard.begin();
analogReference(DEFAULT);
for (byte i = 0; i < CHANNELS; i++) {
power[i] = 0;
lastPower[i] = 0;
triggered = false;
}
lastTime = 0;
maxIndex = -1;
maxPower = 0;
}
void loop() {
lastTime = micros ();
for (short int i = 0; i < CHANNELS; i++) {
channelSample[i] = analogRead(input_pins[i]);
sampleCache[i].put(channelSample[i]);
long int tempInt = sampleCache[i].get(1);
power[i] -= tempInt * tempInt;
tempInt = sampleCache[i].get();
power[i] += tempInt * tempInt;
if (power[i] > light_thres[i]) {
if (!triggered) {
digitalWrite(output_pins[i], LOW);
if (power[i] >= heavy_thres[i]) {
digitalWrite(output_pins[i] + 2, LOW);
if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) {
triggered = false;
digitalWrite(outPins[maxIndex], LOW);
maxIndex = -1;
maxPower = 0;
}
for (byte i = 0; i < CHANNELS; i++) {
inputWindow[i].put(analogRead(inPins[i]));
power[i] = sensitivity[i] * (power[i] - inputWindow[i].get(1) + inputWindow[i].get());
if (lastPower[i] > maxPower && power[i] < lastPower[i]) {
maxPower = lastPower[i];
maxIndex = i;
}
digitalWrite(LED_BUILTIN, HIGH);
Serial.println(debug_output[i]);
}
triggered = true;
lastPower[i] = power[i];
#if DEBUG
Serial.print(power[i]);
Serial.print(" ");
#endif
}
}
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);
}
}
void longMicroDelay (float microTime) {
delay (microTime / 1000);
delayMicroseconds (microTime - floor(microTime / 1000) * 1000);
if (!triggered && maxPower >= HIT_THRES) {
triggered = true;
digitalWrite(outPins[maxIndex], HIGH);
#if !DEBUG
Keyboard.print(outKeys[maxIndex]);
#endif
}
#if DEBUG
Serial.print("\n");
#endif
}