// constants won't change. They're used here to set pin numbers and threshold:
const int ledPin = 12;
const int touchPin = 13; // D4 = TOUCH0
const int buzzerPin = 14;
const int touchThreshold = 20;
int freq = 300;
int channel = 0;
int resolution = 8;
// variables will change:
int touchVal = 0; // variable for reading the touch sensor status
void setup() {
Serial.begin(115200);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// pinMode(buzzerPin, OUTPUT);
ledcSetup(channel, freq, resolution);
ledcAttachPin(buzzerPin, channel);
}
void loop() {
// read the value of the touch sensor:
touchVal = touchRead(touchPin);
// Serial.println(touchVal);
ledcWriteTone(channel, freq);
// check if the sensor is in touch:
if (touchVal < touchThreshold) {
// turn LED on:
digitalWrite(ledPin, HIGH);
ledcWrite(channel, 255);
// digitalWrite(buzzerPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
ledcWrite(channel, 0);
// digitalWrite(buzzerPin, LOW);
}
delay(200);
}