Capsense instrument problem!

Hey guys,

For an assignment on my university i have to make a creative instrument. I want to make a capsense instrument with 3 wires but i have a problem with it. Everytime i touch one of the wires, a tone should be played. But when i touch a wire the tone keeps playing, even when i am not touching it anymore. Can someone help me??

here is my code:

#include <CapSense.h>

CapSense cs_4_2 = CapSense(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapSense cs_4_6 = CapSense(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapSense cs_4_8 = CapSense(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
int tone1 = 120;
int tone2 = 140;
int tone3 = 160;

void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);

pinMode(11, OUTPUT);

}

void loop()
{

long start = millis();
long total1 = cs_4_2.capSense(30);
long total2 = cs_4_6.capSense(30);
long total3 = cs_4_8.capSense(30);

Serial.print(millis() - start); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug windown 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

delay(10); // arbitrary delay to limit data to serial port

if(total1>300) analogWrite(11,tone1); else digitalWrite(tone1, LOW);

if(total2>300) analogWrite(11,tone2); else digitalWrite(tone2, LOW);

if(total3>300) analogWrite(11,tone3); else digitalWrite(tone3, LOW);

}

capsense_attempt_2.ino (1.86 KB)

  if(total1>300)    analogWrite(11,tone1); else digitalWrite(tone1, LOW);
  if(total2>300)    analogWrite(11,tone2); else digitalWrite(tone2, LOW);
  if(total3>300)    analogWrite(11,tone3); else digitalWrite(tone3, LOW);

tone1, tone2, and tone3 are not pin numbers.

What can i use instead, then?
sorry, i am just a beginner :~

What can i use instead, then?

You used a pin number in the analogWrite() call. Why not use that same pin number in the digitalWrite() call?

I tried that too by writing this piece of code:

if(total1>300) analogWrite(11,tone1); else analogWrite(11, LOW);

if(total2>300) analogWrite(11,tone2); else analogWrite(1, LOW);

if(total3>300) analogWrite(11,tone3); else analogWrite(11, LOW);

but then it only plays the last if-statement :s

Actually, I don't understand what you are doing. analogWrite() takes a pin number and a value between 0 and 255. You don't even call analogWrite() unless the value is greater than 300.

What is connected to pin 11?

you are right, i changed the analogWrites to digitalwrites, but the problem is still there. Connected to pin 11 is a Piezo buzzer which should play the 3 tones comming from the 3 wires. But it only plays the tone from the last if statement. Do you have any idea how to solve this?

how does the value between 0 to 255 make tone?
are you using the tone function from arduino if so you cant make the arduino simultaniously turn on all three tone...
its have a limit of only one tone at a time and must be followed by a notone b2 the next one can be activated.

I am not using the tone from the library, i just assigned a 3 different values to them. But i think the problem might be in the if statements since i say: digitalWrite(11, LOW); maybe this turns the speaker off for the first two statements. I dont what i can use instead

the do the long way around use a truth table 3 input 1 output

total 1 total 2 total 3 output tone
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

fill in the output tone that you want it to sound
consider that 1 is when totalX is higher then 300

that is if you want it to give only one sound
if you want it to sound in a sequential way use the blink without delay to change between tone 1 to tone 2 to tone 3