YourESP32 Sketch TouchSensitiveLED

From ArduinoInfo
Jump to navigation Jump to search

/* YourESP32 Example: Sketch Touch Sensitive LED
    SEE: https://ESP32Info.Info
  - WHAT IT DOES
  - SEE the comments after "//" on each line below
  - CONNECTIONS:
   -
   -
  - V1.00 10/02/2018
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
#define touchPin  4
#define ledPin    21

// change your threshold value
const int threshold = 20;

/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int touchValue;


void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(115200);
  delay(1000); // time to bring up serial monitor
  pinMode (ledPin, OUTPUT); // initialize the LED pin as an output:

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // read the state of the pushbutton value:
  touchValue = touchRead(touchPin);
  Serial.print(touchValue);

  // check if the touchValue is below the threshold
  // if it is, set ledPin to HIGH
  if (touchValue < threshold) {
    // turn LED on
    digitalWrite(ledPin, HIGH);
    Serial.println(" - LED on");
  }
  else {
    // turn LED off
    digitalWrite(ledPin, LOW);
    Serial.println(" - LED off");
  }
  delay(100);

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//NONE yet

//*********( THE END )***********