0

I have connected three push buttons to node mcu which are working properly, but display of output on COM port is very slow. It works properly when two buttons are connected, but not when three are connected.

Schematic

#include <ESP8266WiFi.h>
#include<Button.h>
#include<Arduino.h>
// Include the Wi-Fi library
#define LED_BUILTIN 2
int val1 , val2, val3;
const char* ssid     = "****";        // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "************"; 
// constants won't change. They're used here to
// set pin numbers:

void buttononepressed(int);
int buttontwopressed(int);
int buttonthreepressed(int);

const int buttonPin1 = 5;     // the number of the pushbutton pin 2
const int buttonPin2 = 4;     // the number of the pushbutton pin 3
const int buttonPin3 = 0; 

int buttonPresses1 = 0;
int buttonPresses2 = 0;   
int buttonPresses3 = 0;  


// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0; 


void setup() {
  Serial.begin(115200);               // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');

  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED)
  { 
    // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i);
    Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
  // initialize the LED pin as an output:
  pinMode(LED_BUILTIN, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);

  //buttonState1 = digitalRead(switchPin);   // read the initial state
}

void loop()
{
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);

  buttonState3 = digitalRead(buttonPin3);


  // check whether a  pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (buttonState1 == HIGH) 
  {    
    // turn LED on:   // Tweet that the washer is done 
    digitalWrite( LED_BUILTIN, HIGH); 
    //Serial.println("button1"); 
    buttononepressed(buttonState1);
  }
  if (buttonState2 == HIGH) 
  {    
    // turn LED on:    //Tweet that the dryer is done
    digitalWrite( LED_BUILTIN, LOW);  
    //Serial.println("button2"); 
    buttontwopressed(buttonState2);
  }
  if (buttonState3 == HIGH) 
  {
    //turn LED on:
    digitalWrite( LED_BUILTIN, HIGH);
    Serial.println("button3");
    buttonthreepressed(buttonState3);
  }
}

void buttononepressed(int buttonState1)
{
  int count1 = 0;
  buttonState1 = val1;  // save the new state in our variable
  for (int sum = 1; sum <= 3; sum++)
  {
    if (buttonState1 == HIGH)
    {
      // turn LED on:
      digitalWrite(LED_BUILTIN, HIGH);

      sum = sum + count1;
      //Serial.println(sum); // add 1 to the count
      delay(500);
    }
  }

  val1 = digitalRead(5);                     // read input value and store it in value

  if (val1 != buttonState1)
  { // the button state has changed!
    if (val1 == LOW)
    { // check if the button is pressed
      buttonPresses1++;
      // increment the buttonPresses variable
      Serial.print("Button 1 has been pressed ");
      Serial.print(buttonPresses1);
      Serial.println(" times");
    }
  }
  if (buttonPresses1 > 9)
  {
    buttonPresses1 = 0;
  }
}

int buttontwopressed(int buttonState2)
{
  int count2 = 0;
  buttonState2 = val2;
  for (int sum2 = 1; sum2 <= 3; sum2++)
  {
    if (buttonState2 == HIGH)
    {
      // turn LED on:
      digitalWrite(LED_BUILTIN, HIGH);

      sum2 = sum2 + count2;
      //Serial.println(sum); // add 1 to the count
      delay(500);
    }
  }

  val2 = digitalRead(4);                     // read input value and store it in value

  if (val2 != buttonState2)
  { // the button state has changed!
    if (val2 == LOW)
    { // check if the button is pressed
      buttonPresses2++;
      // increment the buttonPresses variable
      Serial.print("Button 2 has been pressed ");
      Serial.print(buttonPresses2);
      Serial.println(" times");
    }
  }

  if (buttonPresses2 > 9)
  {
    buttonPresses2 = 0;
  }
}

int buttonthreepressed(int buttonState3)
{
  int count3 = 0;
  buttonState3 = val3;  // save the new state in our variable
  for (int sum3 = 1; sum3 <= 3; sum3++)
  {
    if (buttonState3 == HIGH)
    {
      // turn LED on:
      digitalWrite(LED_BUILTIN, HIGH);

      sum3 = sum3 + count3;
      //Serial.println(sum); // add 1 to the count
      delay(500);
    }
  }

  val3 = digitalRead(0);                     // read input value and store it in value

  if (val3 != buttonState3)
  { // the button state has changed!
    if (val3 == LOW)
    { // check if the button is pressed
      buttonPresses3++;
      // increment the buttonPresses variable
      Serial.print("Button 3 has been pressed ");
      Serial.print(buttonPresses3);
      Serial.println(" times");
    }
  }
  if (buttonPresses3 > 9)
  {
    buttonPresses3 = 0;
  }
}
  • 2
    Can you add your sketch and circuit? – Michel Keijzers Aug 2 at 11:04
  • 2
    Are you using interrupts to detect switch presses? Or, perhaps when you press a switch you are not exiting the loop() function properly. Hard to tell with out seeing your code. – st2000 Aug 2 at 13:32
  • I fixed the formatting of all the question; please be careful when editing, and when you modify something please double check that the resulting output is what you want. I also added back the description and picture from the second revision; I think that you did not want to cancel it. – frarugi87 Aug 5 at 9:23
  • 1
    Regarding your question, your code is quite odd to me, and I could not understand what you want to do with your code. You read the button status a lot of times, then comparing it (for instance, buttonstatus1 is read and then discarded, then you use val1, then you sum it three times, ....). Please explain what you want to do, what you think you should get and what you are instead getting. I don't think the fact that you get slow readings is the final effect, but give more details – frarugi87 Aug 5 at 9:27
  • The buttonXpressed() functions doesn't make sense to me. For example you write in a comment, that you add 1 to sum, but actually you are adding count1, which is always zero. – chrisl Aug 5 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.