• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Touch Sensor with ESP8266

Q&A Forum › Category: ESP8266 › Touch Sensor with ESP8266
0 Vote Up Vote Down
Rushi Patel asked 6 years ago

Hi Rui,
I want little help from you. If you have time then please solve my problem. I am use 3 touch sensor and i want increment and decrement with touch sensor.
Touch 1 – LED On & Off
Touch 2 – Increment LED Brightness
Touch 3 – Decrement LED Brightness
I am done with that but problem is coming when i am decrement brightness and i turn off LED it should 1st glow and after again i am touch then off.

Question Tags: ESP8266
3 Answers
0 Vote Up Vote Down
Rushi Patel answered 6 years ago

const int TouchPin0 = D5;
const int TouchPin1 = D1;
const int TouchPin = D6;
const int LEDPIN = D8;
bool flag = false;
bool flag1 = false;
bool flag2 = false;
bool incrementflag = false;
bool decrementflag = false;
bool flagonoff = false;
int val0=0;
int val1=0;
int val=0;
volatile int fadeValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// pinMode(LED_BUILTIN, OUTPUT); // declare LED as output
pinMode(LEDPIN, OUTPUT);
pinMode(TouchPin0,INPUT);
pinMode(TouchPin1,INPUT);
pinMode(TouchPin,INPUT);
// digitalWrite(LEDPIN, LOW);
analogWrite(LEDPIN, 0);
//analogWriteFreq(500);
analogWriteRange(255);
}
void loop() {
// put your main code here, to run repeatedly:

val = digitalRead(TouchPin);
if(val==0){
flag = true;
}

if (val==1 && flag==true){
Serial.print(“TouchPin Reading: “);
Serial.println(val);
//analogWrite(LEDPIN,255);
// if(digitalRead(LEDPIN)== HIGH ){
if(digitalRead(LEDPIN)== HIGH || flagonoff == true){
digitalWrite(LEDPIN, LOW);
decrementflag = false;
incrementflag = false;
flagonoff == false;
}
else{
digitalWrite(LEDPIN, HIGH);
decrementflag = true;
incrementflag = true;

}
flag = false;
}

val0 = digitalRead(TouchPin0);
if(val0==0){
flag2 = true;
}

if (val0==1 && flag2==true && incrementflag==true){
Serial.print(“TouchPin0 Reading: “);
Serial.println(val0);
analogWrite(LEDPIN,fadeValue);
// if (digitalRead(LEDPIN) == LOW ){
//digitalWrite(LEDPIN, HIGH);
// if(incrementflag==true){
if (fadeValue<255){
//analogWrite(LEDPIN,fadeValue);
fadeValue+=10;
Serial.print(“fadeValue = “);
Serial.println(fadeValue);
delay(10);
flagonoff = true;
//incrementflag==false;
//}
// }
}

//incrementflag=false;
flag2=false;
}

val1 = digitalRead(TouchPin1);
if(val1==0){
flag1 = true;
}

if (val1==1 && flag1==true && decrementflag==true){
Serial.print(“TouchPin1 Reading: “);
Serial.println(val1);
analogWrite(LEDPIN,fadeValue);
// if(digitalRead(LEDPIN) == HIGH){

//if(decrementflag==true){
if (fadeValue>0){
// analogWrite(LEDPIN,fadeValue);
fadeValue-=10;
Serial.print(“fadeValue = “);
Serial.println(fadeValue);
delay(10);

// }
//}
}

if(fadeValue<100 && fadeValue>0){
flagonoff = true;
}
// decrementflag=false;
flag1=false;

}
//delay(1000);
}

0 Vote Up Vote Down
Rushi Patel answered 6 years ago

This one is my code

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Hello Rushi, unfortunately I can’t debug custom code… I would have to test and build the circuit myself, but due to the amount of requests that I receive per day it’s impossible. I can only help if you have an error following one of my exact code, because I’m already familiar with it.
Thanks for your patience!

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.