I'm making a simple capacitive sensor piano with fruits and vegetables for input, pretty much this: Arduino + Capsense + Fruit - YouTube
#include <CapacitiveSensor.h>
#define SND_PIN 12
int range = 300;
byte button;
int freq;
/*
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Uses a high value resistor e.g. 10 megohm between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
* Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
*/
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 11 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
CapacitiveSensor cs_4_5 = CapacitiveSensor(4,5); // 11 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil
CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 11 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add wire, foil
void setup()
{
Serial.begin(9600);
}
void loop()
{
long total1 = cs_4_2.capacitiveSensor(30);
long total2 = cs_4_5.capacitiveSensor(30);
long total3 = cs_4_8.capacitiveSensor(30);
button = 0;
if (total1 > range) button |= 1;
if (total2 > range) button |= 2;
if (total3 > range) button |= 4;
switch (button) {
case 1: freq = 500; break;
case 2: freq = 300; break;
case 3: freq = 400; break;
case 4: freq = 500; break;
case 5: freq = 600; break;
case 6: freq = 700; break;
case 7: freq = 800; break;
default: freq = 0;
}
Serial.print("\t"); // tab character for debug window spacing
Serial.print(total1); // print sensor output 1
Serial.print("\t");
Serial.print(total2); // print sensor output 2
Serial.print("\t");
Serial.println(total3); // print sensor output 3
freq? tone(SND_PIN, freq) : noTone(SND_PIN);
delay(200);
And it seems to be working,well, halfway.
The speaker gives a tone with a 2-3 second delay, and keeps the tone going for about just as long.
I need immediate response and tone that last a limited time, like 500 ms. A quick beep-boop. The problems seems to be with the code - the signal from the board seems to be received with fixed interval of about 2 seconds, or every time it checks COM port i.e it only beeps when serial printer says the value is above threshold, but it only checks for the value every two seconds or so. Can I can change sensor-to-board data exchange rate? I'm an absolute beginner, pardon my greenness.
Any help or advice would be greatly appreciated. Diagram is in the attachment.
EDIT:
I'm thinking it's the loop speed, I get a capacitance reading only every 2 seconds or so. I've removed all delays, have no millis () or anything like that. so all code repeat infinitely and "very fast", but I get a reading every 1.5 seconds or so.
Could it be hardware lag?