Skip to content

Instantly share code, notes, and snippets.

@antimodular
Created December 13, 2018 18:02
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 antimodular/e7591df411157bfcf2c08e59274c3dcd to your computer and use it in GitHub Desktop.
Save antimodular/e7591df411157bfcf2c08e59274c3dcd to your computer and use it in GitHub Desktop.
//-------teensy hardware restart
//https://forum.pjrc.com/threads/44857-How-to-Reset-Restart-Teensy-3-5-using-sotware?p=145990&viewfull=1#post145990
//#define RESTART_ADDR 0xE000ED0C
//#define READ_RESTART() (*(volatile uint32_t *)RESTART_ADDR)
//#define WRITE_RESTART(val) ((*(volatile uint32_t *)RESTART_ADDR) = (val))
#ifdef USE_FINGER_TOUCH
unsigned long touchPrint_timer;
float touch_alpha = 0.9;
int touchValue;
int raw_touchValue;
int old_touchValue;
int touchAverage;
//unsigned long touch_onTimer; //touch_onHysteresis;
unsigned long touch_offTimer;
unsigned long onHysteresis = 20;
unsigned long offHysteresis = 20;
bool touchReset = false;
unsigned long overMaxTimer;
int TOUCH_MAX = 65100; //65535;
void setup_touchSensor() {
}
void check_touchSensor() {
#ifdef USE_ADA_TOUCH
boolean ada_read = digitalRead(ADA_TOUCH_PIN);
// Serial.print("ada_read ");
// Serial.print(ada_read);
// Serial.println();
if (ada_read == true) {
//not touching
if (millis() - touch_offTimer > offHysteresis) isTouched = false;
touch_onTimer = millis();
} else {
//touching
if (millis() - touch_onTimer > onHysteresis) isTouched = true;
touch_offTimer = millis();
}
// if (touchValue < touchThreshold) isTouched = false;
// else isTouched = true;
// Serial.print("touch read ");
// Serial.print(r1);
// Serial.println();
if (isTouched != old_isTouched) {
old_isTouched = isTouched;
insideSend(isTouched); //finger is inside. send that state to computer
// Serial.print("isTouched ");
// Serial.println(isTouched);
if (isTouched == false) {
set_actuatorBPM(-1, 3);
bpmSend(-1);
DEFAULT_BPM = int(random(DEFAULT_BPM_min, DEFAULT_BPM_max));
}
}
#else
old_touchValue = touchValue;
raw_touchValue = touchRead(TOUCH_PIN); //max retruned value is 65535
int raw_touchValue2 = touchRead(TOUCH_PIN2);
if (raw_touchValue < 0) raw_touchValue = 1;
raw_touchValue = constrain(raw_touchValue, 0, TOUCH_MAX);
// Serial.print("raw_touchValue ");
// Serial.print(raw_touchValue);
// Serial.println();
//this is a test to work around the problem of the touchSensor causing constant pulsing of the actuator
/*
if (raw_touchValue > 60000) {
// Serial.print("raw_touchValue ");
// Serial.print(raw_touchValue);
// Serial.println();
if (millis() - overMaxTimer > 30000) { // && touchReset == false) {
overMaxTimer = millis();
// touchReset = true;
// pinMode(TOUCH_PIN, OUTPUT);
// digitalWrite(TOUCH_PIN, LOW);
Serial.println("reset teensy");
allOff(0);
delay(1000);
WRITE_RESTART(0x5FA0004);
delay(2000);
}
} else {
overMaxTimer = millis();
// touchReset = false;
}
//end test
*/
//create running average of touch value
//the larger touch_alpha the greater the smoothing
//the smaller touch_alpha the closer we get to the raw value
touchValue = int(touch_alpha * old_touchValue) + int((1 - touch_alpha) * raw_touchValue);
touchValue = constrain(touchValue, 0, TOUCH_MAX); //constrain the running average. not quite to max INT so that later i can send out info about the actual value over osc
if (touchValue < touchThreshold) {
//not touching
if (millis() - touch_offTimer > offHysteresis) isTouched = false;
touch_onTimer = millis();
} else {
//touching
if (millis() - touch_onTimer > onHysteresis) isTouched = true;
touch_offTimer = millis();
}
// if (touchValue < touchThreshold) isTouched = false;
// else isTouched = true;
// Serial.print("touch read ");
// Serial.print(r1);
// Serial.println();
if (isTouched != old_isTouched) {
old_isTouched = isTouched;
insideSend(isTouched); //finger is inside. send that state to computer
// Serial.print("isTouched ");
// Serial.println(isTouched);
if (isTouched == false) {
set_actuatorBPM(-1, 3);
bpmSend(-1);
DEFAULT_BPM = int(random(DEFAULT_BPM_min, DEFAULT_BPM_max));
}
}
// float touchAvg_alpha = 0.99;
// if (raw_touchValue < 6000) {
// touchAverage = touchAvg_alpha * touchAverage + (1 - touchAvg_alpha) * raw_touchValue;
//// touchThreshold = touchAverage;
// }
#endif //end else USE_ADA_TOUCH
// Serial.print("isTouched ");
// Serial.print(isTouched);
// Serial.println();
#ifdef USE_FINGER
if (bDebug) {
if (millis() - touchPrint_timer > 1000) {
//to help troubleshoot sound out 1234 if actual touchValue was bad
if (touchValue >= 0 && touchValue < TOUCH_MAX - 100) touchReadSend(touchValue, 0); //touchAverage);
else touchReadSend(12345, 0);
touchPrint_timer = millis();
Serial.print("touchVal ");
Serial.print(touchValue);
Serial.print(" / ");
Serial.print(touchThreshold);
Serial.print(" / ");
Serial.print(raw_touchValue);
Serial.print(" / ");
Serial.print(raw_touchValue2);
Serial.println();
}
}
#endif
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment