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.
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);
}
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!