1 (edited by Vanish565 2023-08-21 10:08:52)

Topic: Button Counter

Hi everyone, I am very new to playing with microcontrollers. I am using an ESP32 and I want to implement a project where I can count the number of times the button is pressed. If I press the button, the LED should flash once and the counter should increase by 1. And then the LED must flash off.

Here is the code I have implement:

/*
   -- New project --
   
   This source code of graphical user interface 
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 3.1.8 or later version 
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.11.1 or later version;
     - for iOS 1.9.1 or later version;
    
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.    
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP32CORE_BLE
#include <BLEDevice.h>

#include <RemoteXY.h>


// RemoteXY connection settings 
#define REMOTEXY_BLUETOOTH_NAME "RemoteXY"


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =   // 19 bytes
  { 255,1,0,0,0,12,0,16,31,1,1,0,25,56,12,12,2,31,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t button_1; // =1 if button pressed, else =0 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define PIN_BUTTON_1 23
// Variables 
int counter = 0;

void setup() 
{
  RemoteXY_Init (); 
  
  pinMode (PIN_BUTTON_1, OUTPUT);
  
  // TODO you setup code
  // Setting up the baudrate = 115200
  Serial.begin(115200);
  
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  if (RemoteXY.button_1 == 1)
  {
    digitalWrite(PIN_BUTTON_1, RemoteXY.button_1==1);
    counter += 1;
  }
  else {
    digitalWrite(PIN_BUTTON_1, LOW);
  }
  Serial.println(counter);
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay() 
  delay(1000);

}

I know I should not use a delay but it works. The input is very slow and I do not know how to fix it. I have to hold the button on my phone for it to register. It is not instant feedback.