Conflict between tone() and CapSense library

After tearing my hairs off working with CapSense, I finally came to the conclusion that the errors I was experiencing were coming from the software side, more precisely from the tone() function. :slight_smile:

I noticed that when the tone() fonction is called, wether the piezo buzzer is connected or not, CapSense reads a lot of false data. So for the sake of example I stripped down everything;

#include <CapacitiveSensor.h>
CapacitiveSensor   cs_7_8 = CapacitiveSensor(7,8);
unsigned long csSum = 0;
unsigned long previousPiezoAlarmInterval = 0;
const int PiezoAlarmInterval = 5*1000;
int speakerPin = 4;

void setup()
{
	Serial.begin(9600);
}

void loop()
{
Tone();

	long cs =  cs_7_8.capacitiveSensor(80);
	if (cs > 100) {
		csSum += cs;
		Serial.println(cs); 
		
		if (csSum >= 3500) { //Re-Calibrate to avoid tripping off
			csSum = 0; 
			cs_7_8.reset_CS_AutoCal();
		}
		
	} else {
		csSum = 0; //timeout
		Serial.println(cs); 
	}

}

void Tone() {
	if (millis() - previousPiezoAlarmInterval >= PiezoAlarmInterval) {
		previousPiezoAlarmInterval += PiezoAlarmInterval;
		tone(speakerPin, 30, 3000);
	}
}

So pin 7 is connected to a 10M resistor, which is connected to pin 8. And pin 8 is connected to an antenna.

In the end I worked the bug around using a substitute to the tone() function, but as I could not manage to get the exact tone I wanted I would rather prefer to know how to fix the bug.