/*************************************************************** * * * Taiko Sanro - Arduino * * Support Arduino models with ATmega32u4 microprocessors * * * * Shiky Chang Chris * * zhangxunpx@gmail.com wisaly@gmail.com * * * ***************************************************************/ #define MODE_JIRO 1 #define MODE_DEBUG 1 #define MODE_DEBUG_SHOW_TIME 0 #define CHANNELS 4 #define SAMPLE_CACHE_LENGTH 15 #define POWER_CACHE_LENGTH 3 #define LIGHT_THRES 500 #define HEAVY_THRES 1000 #include #include #include "cache.h" #if MODE_JIRO #define HEAVY_THRES LONG_MAX #endif int channelSample [CHANNELS]; int lastChannelSample [CHANNELS]; Cache sampleCache [CHANNELS]; long int power [CHANNELS]; Cache powerCache [CHANNELS]; bool triggered [CHANNELS]; int pins[] = {A0, A1, A2, A3}; // L don, R don, L kat, R kat char lightKeys[] = {'g', 'h', 'f', 'j'}; char heavyKeys[] = {'t', 'y', 'r', 'u'}; void setup() { Serial.begin (9600); Keyboard.begin (); analogReference (DEFAULT); for (short int i = 0; i < CHANNELS; i++) { power [i] = 0; lastChannelSample [i] = 0; triggered [i] = false; } } void loop() { #if MODE_DEBUG long int sampleLastTime = 0; long int calcLastTime = 0; #endif for (short int i = 0; i < CHANNELS; i++) { #if MODE_DEBUG long int lastTime = micros(); #endif channelSample[i] = analogRead (pins[i]); #if MODE_DEBUG sampleLastTime += micros () - lastTime; lastTime = micros(); #endif sampleCache [i].put (channelSample [i] - lastChannelSample [i]); long int tempInt; tempInt = sampleCache [i].get (1); power [i] -= tempInt * tempInt; tempInt = sampleCache [i].get (); power [i] += tempInt * tempInt; if (power [i] < LIGHT_THRES) { power [i] = 0; } powerCache [i].put (power [i]); lastChannelSample [i] = channelSample [i]; if (powerCache [i].get (1) == 0) { triggered [i] = false; } bool isDescending = true; for (short int j = 0; j < POWER_CACHE_LENGTH - 1; j++){ if (!triggered [i] && (powerCache [i].get (j - 1) > powerCache [i].get (j))) { isDescending = false; break; } } if (isDescending) { if (power [i] >= HEAVY_THRES) { triggered [i] = true; #if !MODE_DEBUG Keyboard.print (heavyKeys [i]); #endif } else if (power [i] >= LIGHT_THRES) { triggered [i] = true; #if !MODE_DEBUG Keyboard.print (lightKeys [i]); #endif } #if MODE_DEBUG calcLastTime += micros () - lastTime; #endif } #if MODE_DEBUG Serial.print (power [i]); Serial.print ("\t"); #endif // End of each channel } #if MODE_DEBUG // Damn Arduino plotter, the Y axis scale changes much // too frequently! Use these 5000 and 0 values to "lock" // the plotter. Serial.print (5000); Serial.print ("\t"); Serial.print (0); Serial.print ("\t"); #if MODE_DEBUG_SHOW_TIME Serial.print (sampleLastTime); Serial.print ('\t'); Serial.print (calcLastTime); #endif Serial.println (""); #endif }