I am trying to create a capacitive touch piano.
I know my wiring is correct, but the program is incorrect. I am sending it the output to Hairless MIDI Serial Bridge which is sending it to GarageBand. I know that the bridge/GarageBand is also working. The if statements are the thing going wrong. (I think) The midi notes are sequencing even when i am not touching the wires.
Code:
// Pin for the LED
int LEDPin = 13;
int noteON = 144;
// Pin to connect to your drawing
int capSensePin = 2;
// This is how high the sensor needs to read in order
// to trigger a touch. You'll find this number
// by trial and error, or you could take readings at
// the start of the program to dynamically calculate this.
int touchedCutoff = 36;
int potval;
void MIDImessage(byte command, byte data1, byte data2) {
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}
void setup() {
Serial.begin(9600);
// Set up the LED
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW);
}
void loop() {
potval = analogRead(0); // reads the value of the potentiometer (value between 0 and 1023)
potval = map(potval, 0, 1023, 0, 1000);
if (readCapacitivePin(2) > touchedCutoff) {
MIDImessage(noteON, 60, 100);
delay(300);
MIDImessage(noteON, 60, 0);
}
if (readCapacitivePin(3) > touchedCutoff) {
MIDImessage(noteON, 62, 100);
delay(300);
MIDImessage(noteON, 62, 0);
}
if (readCapacitivePin(4) > touchedCutoff) {
MIDImessage(noteON, 64, 100);
delay(300);
MIDImessage(noteON, 64, 0);
}
if (readCapacitivePin(5) > touchedCutoff) {
MIDImessage(noteON, 65, 100);
delay(300);
MIDImessage(noteON, 65, 0);
}
if (readCapacitivePin(6) > touchedCutoff) {
MIDImessage(noteON, 67, 100);
delay(300);
MIDImessage(noteON, 67, 0);
}
if (readCapacitivePin(7) > touchedCutoff) {
MIDImessage(noteON, 69, 100);
delay(300);
MIDImessage(noteON, 69, 0);
}
if (readCapacitivePin(8) > touchedCutoff) {
MIDImessage(noteON, 71, 100);
delay(300);
MIDImessage(noteON, 71, 0);
}
if (readCapacitivePin(9) > touchedCutoff) {
MIDImessage(noteON, 72, 100);
delay(300);
MIDImessage(noteON, 72, 0);
}
// Every 500 ms, print the value of the capacitive sensor
}
// readCapacitivePin
// 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
// In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
// it up to +5v.
uint8_t readCapacitivePin(int pinToMeasure) {
// This is how you declare a variable which
// will hold the PORT, PIN, and DDR registers
// on an AVR
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
// Here we translate the input pin number from
// Arduino pin number to the AVR PORT, PIN, DDR,
// and which bit of those registers we care about.
byte bitmask;
if ((pinToMeasure >= 0) && (pinToMeasure <= 7)) {
port = &PORTD;
ddr = &DDRD;
bitmask = 1 << pinToMeasure;
pin = &PIND;
}
if ((pinToMeasure > 7) && (pinToMeasure <= 13)) {
port = &PORTB;
ddr = &DDRB;
bitmask = 1 << (pinToMeasure - 8);
pin = &PINB;
}
if ((pinToMeasure > 13) && (pinToMeasure <= 19)) {
port = &PORTC;
ddr = &DDRC;
bitmask = 1 << (pinToMeasure - 13);
pin = &PINC;
}
// Discharge the pin first by setting it low and output
*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);
// Make the pin an input WITHOUT the internal pull-up on
*ddr &= ~(bitmask);
// Now see how long the pin to get pulled up
int cycles = 16000;
for (int i = 0; i < cycles; i++) {
if (*pin & bitmask) {
cycles = i;
break;
}
}
// 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.
*port &= ~(bitmask);
*ddr |= bitmask;
return cycles;
}
Thank you soooo much!!!