Advice on modification of coding to send data from ESP32 to google spreadsheet

Hello , I would like to ask any advice or modifications needed for my current esp32 project.
Basically what I am trying to achieve for my program is sending the data of the frequency of a push button pressed to google spreadsheet in a certain period of time (in my case every 15 minutes).

The hardware I'm using is :
ESP32-WROOM-32

This is my current view from spreadsheets. The main problem I am facing is the counter does not increase higher than 1 even though the push button is pressed many times.
image

Below is my coding for the arduino:

#include <WiFi.h>         // Wifi Library
#include <HTTPClient.h>
#define button_pin 14
bool button_state;
bool pastbutton_state;
bool reset_state;
bool pastreset_state;
int counter = 0;
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 60000;


//---------------------------------------------------------------------
const char * ssid = "TT-Visitor";  // type your Wifi name
const char * password = "";  // Type your wifi password
String GOOGLE_SCRIPT_ID = "";  // Type your App Script id (setiap program lain2 ID)
const int sendInterval = 50;

WiFiClientSecure client;


void setup() {
  
  pinMode (button_pin , INPUT);
  pinMode(2,OUTPUT);
  Serial.begin(9600);
  delay(10);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid , password);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(2,LOW); // lampu biru esp menyala dan tutup sementara tunggu wifi connect
    delay(500);
    Serial.print(".");
    digitalWrite(2,HIGH);
    delay(500);
  }
  Serial.println("OK"); // dah connect akan keluar " OK "
  }

  
void loop() 
{

for (int j=0; j<10 ; j++)
{
  for (int i=0;  i<1 ; i++)
      {
   pastbutton_state = button_state ;
   button_state = digitalRead(button_pin);
   if (button_state != pastbutton_state && button_state == LOW)
         {
    counter += (i+1);
    Serial.print ("Counter increases to ");
    Serial.println(counter);
         }
   delay(40);
      }
  
}
 write_google_sheet( "value1="+String(counter));

delay(5000); // The data will be sent to google spreadsheet every 180s = 3minutes)
//counter = 0;
}

///////////////////////////////////////////////
void write_google_sheet(String params) {
   HTTPClient http;
   String url="https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+params;
   Serial.println(url);
    Serial.println("Updating the frequency of button pressed");
    http.begin(url.c_str());
    http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
    int httpCode = http.GET();  
    //Serial.print("HTTP Status Code: ");
   // Serial.println(httpCode);
    
    String payload;
    if (httpCode > 0) {
        payload = http.getString();
      //  Serial.println("Payload: "+payload);     
    }
    http.end();
}

Thank you very much

What is

for ( int i = 0; i < 1; i++ )

supposed to do?

Do You reset the controller between button pressings? Somehow counter is set back to 0.

How is buttonPin wired?

You have a lot of print() statements in your code, what does the Serial Monitor display?

Also, how is your button wired up? You have declared it as INTPUT which means it needs a pull-up or pull-down resistor. Is one in your circuit?

for this part the i<1 increases the counter by 1, if it is change to i<2 the counter increases by 2

Since your for loop runs from 0 to 0, i will always be 0 so this is the same as

counter += 1;

which is the same (and more usual way) as

counter++;

It directly connected to the input pin and GND pin. I am also using a input pullup resistor

It is always better to copy and paste text, like the output of the Serial Monitor rather than posting a picture of text.

From the little bit I can see, you only have the message "Counter increases"... 1 time which means your code has only detected a button press 1 time.

I'm truly sorry for that, forgive me because its my first time using this forum. This is what the reading from the serial monitor shows. Yes the counter says it increases by 1 even though I probably pressed the button a dozen times.

10:57:00.281 -> Connecting to Wi-Fi.OK
10:57:01.317 -> https://script.google.com/macros/s/AKfycbyQ6kLlQqT8z8WhkSnd4EGvwtJV5OWji92uHJXiuqTrfRXuWwjypS_JHGJP8lhsBdzR/exec?value1=0
10:57:01.460 -> Updating the frequency of button pressed
10:57:05.624 -> HTTP Status Code: 200
10:57:05.624 -> Payload: Written on column A
10:57:10.639 -> https://script.google.com/macros/s/AKfycbyQ6kLlQqT8z8WhkSnd4EGvwtJV5OWji92uHJXiuqTrfRXuWwjypS_JHGJP8lhsBdzR/exec?value1=0
10:57:10.778 -> Updating the frequency of button pressed
10:57:15.150 -> HTTP Status Code: 200
10:57:15.150 -> Payload: Written on column A
10:57:20.120 -> Counter increases to 1
10:57:20.167 -> https://script.google.com/macros/s/AKfycbyQ6kLlQqT8z8WhkSnd4EGvwtJV5OWji92uHJXiuqTrfRXuWwjypS_JHGJP8lhsBdzR/exec?value1=1
10:57:20.307 -> Updating the frequency of button pressed
10:57:24.338 -> HTTP Status Code: 200
10:57:24.385 -> Payload: Written on column A
10:57:29.398 -> https://script.google.com/macros/s/AKfycbyQ6kLlQqT8z8WhkSnd4EGvwtJV5OWji92uHJXiuqTrfRXuWwjypS_JHGJP8lhsBdzR/exec?value1=1
10:57:29.539 -> Updating the frequency of button pressed
10:57:33.575 -> HTTP Status Code: 200
10:57:33.575 -> Payload: Written on column A
10:57:38.630 -> https://script.google.com/macros/s/AKfycbyQ6kLlQqT8z8WhkSnd4EGvwtJV5OWji92uHJXiuqTrfRXuWwjypS_JHGJP8lhsBdzR/exec?value1=1
10:57:38.725 -> Updating the frequency of button pressed

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