Push button with command

When there is a command (char ‘b’) will allow to get data from the button to turn off the led, if I do not press the button but send a command ‘t’ then the led will also turn off. I need help. Sorry if my English not good. Thank you :frowning:

My code

#include <Button.h>

#define BUTTON_PIN 7
#define PULLUP true
#define INVERT true
#define DEBOUNCE_MS 50
#define LED 13

Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);

char command = 0;

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  timer.setTimeout(5000);
  pinMode(LED, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()
{
  if (Serial.available())
  {
    command = Serial.read();
    Serial.flush();
    switch (char(command))
    {
      case 'b':
        {
          myBtn.read();
          Serial.print("Had char ");
          Serial.print((char)command);
          digitalWrite(13, HIGH);

          if (myBtn.wasPressed())
          {
            digitalWrite(13, LOW);
            command = 0;
          }
        }
        break;
      case 't':
        {
          digitalWrite(13, LOW);
          Serial.println("Da nhan duoc T");
        }
      default:
        Serial.print("Out");
    }
  }
}

I'm not sure what you want help with here? What is the problem?

PORTB: I'm not sure what you want help with here? What is the problem?

i press button, but nothing to action

1) You don't tell us what doesn't work. What does happens for example?

2) You use a library, link to it. We don't know all libraries available.

3) You call while (!Serial);, which Arduino do you use?

4) You call flush but never send anything => it's useless. .flush only affect sending serial. Read the reference.

5)

digitalWrite(13, HIGH);

          if (myBtn.wasPressed())
          {
            digitalWrite(13, LOW);

Why turn it on if you might turn it off gain right away?

6) More a tip, use constants (for example const byte) instead of macros (define) for stuff like pin definitions. It's the saver C++ alternative.

Sorry, i was fixed my problem cause i don’t know how explain this, but i have new problem need help.
When i send a command ‘b’ from server ( c# winform), but my button not get value to off led.
Some one help me this problem. Thank you.
My code use Ethernet shield and Ethernet library
I use Arduino Uno R3 board.

#include <Button.h>



#include <Ethernet.h>

#define BUTTON_PIN 7
#define PULLUP true
#define INVERT true
#define DEBOUNCE_MS 50
#define ledDongY 8


Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);

char command=0;

////client1 IP
//byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x48, 0xD3 };
//IPAddress ip(192, 168, 1, 4);
//const String ClientName = "Client1";

//client2 IP
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x49, 0xD4 };
IPAddress ip(192, 168, 1, 5);
const String ClientName = "Client2";

//client3 IP
//byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x50, 0xD5 };
//IPAddress ip(192, 168, 1, 6);
//const String ClientName = "Client3";

IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
//IPAddress server(192, 168, 1, 3);//ethernet
IPAddress server(192, 168, 1, 2);//wifi


EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);

  Serial.begin(9600);
  while (!Serial);

  pinMode(ledDongY, OUTPUT);
  digitalWrite(ledDongY, LOW);

  Serial.println("Dang ket noi den Server.....");
  delay(10000);
  if (client.connect(server, 9899))
  {
    Serial.println("Da ket noi");
  }
  else
  {
    Serial.println("Ket noi khong thanh cong");
  }
  delay(50);
}

void loop()
{

  if (client.available())
  {
    command = client.read();
        client.flush();
  }

  switch (command)
  {
    case (char)'b':
      {
        myBtn.read();
        Serial.println("Ky tu nhan duoc ");
        Serial.print((char)command);
        digitalWrite(ledDongY, HIGH);
        if (myBtn.wasPressed())
        {
          digitalWrite(ledDongY, LOW);
          Serial.println("Da nhan nut");
           client.println("stoptimer");
          command = 0;
        }
        break;
      }
      break;
    case (char)'t':
      {
        digitalWrite(ledDongY, LOW);
        Serial.println("Ky tu nhan duoc ");
        Serial.print((char)command);
        command = 0;
      }
      break;
    default:;
  }
}

Maybe try the board in your native language. This is getting you nowhere...

i’m using Arduino Uno R3 with Ethernet Shield

#include <Button.h>



#include <Ethernet.h>

#define BUTTON_PIN 7
#define PULLUP true
#define INVERT true
#define DEBOUNCE_MS 50
#define ledDongY 8


Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);

char command=0;

////client1 IP
//byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x48, 0xD3 };
//IPAddress ip(192, 168, 1, 4);
//const String ClientName = "Client1";

//client2 IP
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x49, 0xD4 };
IPAddress ip(192, 168, 1, 5);
const String ClientName = "Client2";

//client3 IP
//byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x50, 0xD5 };
//IPAddress ip(192, 168, 1, 6);
//const String ClientName = "Client3";

IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
//IPAddress server(192, 168, 1, 3);//ethernet
IPAddress server(192, 168, 1, 2);//wifi


EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);

  Serial.begin(9600);
  while (!Serial);

  pinMode(ledDongY, OUTPUT);
  digitalWrite(ledDongY, LOW);

  Serial.println("Dang ket noi den Server.....");
  delay(10000);
  if (client.connect(server, 9899))
  {
    Serial.println("Da ket noi");
  }
  else
  {
    Serial.println("Ket noi khong thanh cong");
  }
  delay(50);
}

void loop()
{

  if (client.available())
  {
    command = client.read();
        client.flush();
  }

  switch (command)
  {
    case (char)'b':
      {
        myBtn.read();
        Serial.println("Ky tu nhan duoc ");
        Serial.print((char)command);
        digitalWrite(ledDongY, HIGH);
        if (myBtn.wasPressed())
        {
          digitalWrite(ledDongY, LOW);
          Serial.println("Da nhan nut");
           client.println("stoptimer");
          command = 0;
        }
        break;
      }
      break;
    case (char)'t':
      {
        digitalWrite(ledDongY, LOW);
        Serial.println("Ky tu nhan duoc ");
        Serial.print((char)command);
        command = 0;
      }
      break;
    default:;
  }
}

septillion:
2) You use a library, link to it. We don’t know all libraries available.

And

septillion:
Maybe try the board in your native language. This is getting you nowhere…

septillion: And

i'm using https://playground.arduino.cc/Code/Button libraries and Ethernet libraries.

i can't find anything related in my language

I don't know that library (and it's very old) but I think you use it wrong. Only isPressed() reads the switch. So you need to call that all the time to poll the switch.

And about language, it's not if there is info in your language but if there is a board in you language it might be easier to post it there.