Skip to content

Instantly share code, notes, and snippets.

@moonmilk
Created February 20, 2017 02:22
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 moonmilk/027b883de1a708faa6da67804a56fd09 to your computer and use it in GitHub Desktop.
Save moonmilk/027b883de1a708faa6da67804a56fd09 to your computer and use it in GitHub Desktop.
instrument-a-day 2017, day 18: lobster bowl
// Lobster Bowl arduino due touch glitch thing
// readCapacitivePin
// Arduino Due version, from http://playground.arduino.cc/Code/CapacitiveSensor
// Input: Arduino pin number
// Output: A number, from 0 to 17 expressing
// how much capacitance is on the pin
// When you touch the pin, or whatever you have
// attached to it, the number will get higher
uint8_t readCapacitivePin(int pinToMeasure) {
pinMode(pinToMeasure, OUTPUT);
digitalWrite(pinToMeasure, LOW);
delay(1);
// Prevent the timer IRQ from disturbing our measurement
noInterrupts();
// Make the pin an input with the internal pull-up on
pinMode(pinToMeasure, INPUT_PULLUP);
// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
uint8_t cycles = 17;
if (digitalRead(pinToMeasure)) { cycles = 0;}
else if (digitalRead(pinToMeasure)) { cycles = 1;}
else if (digitalRead(pinToMeasure)) { cycles = 2;}
else if (digitalRead(pinToMeasure)) { cycles = 3;}
else if (digitalRead(pinToMeasure)) { cycles = 4;}
else if (digitalRead(pinToMeasure)) { cycles = 5;}
else if (digitalRead(pinToMeasure)) { cycles = 6;}
else if (digitalRead(pinToMeasure)) { cycles = 7;}
else if (digitalRead(pinToMeasure)) { cycles = 8;}
else if (digitalRead(pinToMeasure)) { cycles = 9;}
else if (digitalRead(pinToMeasure)) { cycles = 10;}
else if (digitalRead(pinToMeasure)) { cycles = 11;}
else if (digitalRead(pinToMeasure)) { cycles = 12;}
else if (digitalRead(pinToMeasure)) { cycles = 13;}
else if (digitalRead(pinToMeasure)) { cycles = 14;}
else if (digitalRead(pinToMeasure)) { cycles = 15;}
else if (digitalRead(pinToMeasure)) { cycles = 16;}
// End of timing-critical section
interrupts();
// Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between
// sensors.
digitalWrite(pinToMeasure, LOW);
pinMode(pinToMeasure, OUTPUT);
// set back to input mode
pinMode(pinToMeasure, INPUT);
return cycles;
}
void setup() {
// speaker on pin 13
pinMode(13, OUTPUT);
}
bool o;
void loop() {
for (int i=22; i<=53; i+=1) {
int t = readCapacitivePin(i);
if (t>1) {
delayMicroseconds(t*(53-i)*10);
o = !o;
digitalWrite(13, o);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment