Help regarding touch sensor

I am working on smart home project, where I am using touch sensor as a push button, actually I make my touch sensor as a push button just because I can't turn off my touch button from coding, for better understanding I will give you some example:

I am checking button state value from both sides. I.e. server side and by physical pressing, so when I press button physically it works fine but when i changes button status from server side then due to my button state is high or low, it instantly changing my value to previous one, so i am unable to change button state from server side.

So that's why i used my touch sensor as a push button so when user click on it, it change some variable value and on the bases of that value I can handle state of button from both side, but problem is i am checking lot of data continuosly so my loop is slowing

I am checking button press by following method
If(digitalRead(button = HIGH)) {
StateOfButton = !StateOfButton

So in my side, i need to instantly call this method whenever user clicks touch sensor, but in my code it's taking 500ms-800ms, I tried to print when loop recall, and loop print by delay of 1 sec 4 times and then recalling loop very fast more than 20+ in second, so any one have any suggestions what can i do

This is wrong

if(digitalRead(button == HIGH))

or just

if(digitalRead(button))

Also, post your code if you need help with your code!

This is my code I created to check working of button:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Firebase_ESP_Client.h>

#define API_KEY "API_KEY"
#define DATABASE_URL "URL"
#define USER_EMAIL "Email"
#define USER_PASSWORD "Password"

FirebaseData fbdo;

FirebaseAuth auth;

FirebaseConfig config;

#define buttonPin 4
#define LED 2

const char *ssid = "SSID"; //SSID
const char *pass = "Password"; // password

bool buttonState, buttonStatus, isFromBoard;

void setup() {
pinMode(buttonPin, INPUT);
pinMode(LED, OUTPUT);

buttonState = false;

isFromBoard = false;

buttonStatus = false;

Serial.begin(115200); //serial start
delay(1000);
Serial.println("Connecting to wifi");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
};

config.database_url = DATABASE_URL;
config.api_key = API_KEY;

/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;

Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
fbdo.setBSSLBufferSize(512, 2048);
}

void loop() {
digitalWrite(LED, buttonState);
if(digitalRead(buttonPin) == HIGH) {
buttonState = !buttonState;
isFromBoard = true;
}

if(isFromBoard) { // update firebase only when we press physical buttons
Firebase.RTDB.setBool(&fbdo, "button1", buttonState);
isFromBoard = false;
}
else{
buttonStatus = Firebase.RTDB.getInt(&fbdo, "button1");
if(buttonStatus != buttonState) {
buttonState = buttonStatus;
}
}
}

The following might cause buttonState to toggle several times while you have the physical button pressed because there might be multiple calls to loop() while the button is pressed.

  if (digitalRead(buttonPin) == HIGH) {
    buttonState = !buttonState;
    isFromBoard = true;
  }

You should actually be checking for the button changing from LOW to HIGH rather than just HIGH. Also, since you didn't use INPUT_PULLUP on the button mode I assume you have a pull-down resistor. Otherwise I would suggest using the internal pullup and checking for a HIGH to LOW transition.

As i mentioned, I am using my touch sensor as a push button so after clicking on it, it's again go to its LOW state, if I used touch sensor as a button, then I am facing some problems with firebase like I am not able to change the value from server side, and in my code I used INPUT_PULLUP , but my issue is different, As refer to this code, I need to instantly perform this step:

if(digitalRead(buttonPin) == HIGH) {
buttonState = !buttonState;
isFromBoard = true;
}

so it can update in Firebase, but when i click in milliseconds Its not detecting my press, so How can I solve this,

If i write Fresh code like this:

if(digitalRead(buttonPin)) {
Serial.prinln("High")
} else {
Serial.prinln("LOW")
}

In this code it detecting it, but in my above code its not detecting fast touch

The following code changes buttonState ONLY if the button goes from LOW to HIGH:

void loop()
{
  static int lastButtonRead = digitalRead(buttonPin);
  int buttonRead = digitalRead(buttonPin);
  
  if (buttonRead != lastButtonRead)
  {
    if (buttonRead == HIGH) 
    {
      buttonState = !buttonState;
      isFromBoard = true;
    }
    lastButtonRead = buttonRead;
  }

Again, are you sure your button is wired properly? Do you have a pull-down resistor?

You are not understanding, We can use touch sensor as two types, like a simple button i.e. when we click on it it state goesto HIGH and again when we press on it, it's go to LOW, and second one that I am using it as a push button. i.e. as long as we are pressing the button, its state will HIGH and when we finger out from sensor it again goes to LOW state, just like doorbell button, so I just want to check only when It goes to HIGH, because I know after releasing finger its automatically going to LOW,

if (digitalRead(buttonPin) == HIGH) {
    buttonState = !buttonState;
    isFromBoard = true;
  }

but this part is not get called even I click on button, don't know why, but in this code its immediately sensing touch

void setup() {
    pinMode(buttonPin, INPUT);
    Serial.begin(115200);
}

void loop() {
    if(digitalRead(buttonPin) == HIGH) {
        Serial.println("Touched");
    }
}

So I am not getting, in my code why its not detecting fast touch

So you are saying the following code works?

void setup() {
    pinMode(buttonPin, INPUT);
    Serial.begin(115200);
}

void loop() {
    if(digitalRead(buttonPin) == HIGH) {
        Serial.println("Touched");
    }
}

If you are trying to do a really fast touch it probably misses it because the database accesses are taking a significant amount of time. I would record the millis() before and after database access and I bet you will find it significant.

Yes, this small code detect touch very fast, but same code I added in my code, but it doesn't detect it sometimes, and I am printing some text after starting loop and ending loop, and loop is also working fine then why that part not executing sometimes.

I believe it is the database accesses. Sorry I can't help with that.

I found the solution. I am using ESP8266 Interrupts to detect touches and now its working fine. :smiley:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.