Button pressed, Arduino went offline

Hi guys,
i need to connect my Arduino that will check if button is pressed or not.
I needed also if Arduino was connected or not and I chose “notify when hardware goes offline” in notification settings.

Why Arduino goes offline when I press the button on pin 2?
I can use it only if I close serial monitor and run it again.

Another question: when I press button, in serial monitor I see a lot of “Button pressed”, but I press button only one time!!
Hope someone help me…
here code:

#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress arduino_ip ( 192, 168, 1, 73);
IPAddress dns_ip ( 8, 8, 8, 8);
IPAddress gateway_ip ( 192, 168, 1, 254);
IPAddress subnet_mask(255, 255, 255, 0);
#define W5100_CS 10
#define SDCARD_CS 4

void notifyOnButtonPress()
{
// Invert state, since button is “Active LOW”
int isButtonPressed = !digitalRead(2);
if (isButtonPressed) {
Serial.println(“Button is pressed.”);

// Note:
//   We allow 1 notification per 15 seconds for now.
Blynk.notify("Yaaay... button is pressed!");

}
}

void setup()
{
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
Serial.begin(9600);
Blynk.begin(auth, “blynk-cloud.com”, 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);

// Setup notification button on pin 2
pinMode(2, INPUT_PULLUP);
// Attach pin 2 interrupt to our handler
attachInterrupt(digitalPinToInterrupt(2), notifyOnButtonPress, CHANGE);
}

void loop()
{
Blynk.run();
}

It is because arduino is running @16mhz, so when you press down button, it will “flood” the server with blynk.notify, and your HW gets disconnected. Check out docs for more info about timers and how to use them.

Thanks @wiipro
I tried also this sketch with timer.
How can I do to implement notifing process on this?
Please help me.




 #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";

//IPAddress server_ip (blynk-cloud.com);

// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress arduino_ip ( 192,   168,   1,  73);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,   168,   1,   254);
IPAddress subnet_mask(255, 255, 255,   0);

#define W5100_CS  10
#define SDCARD_CS 4
// Select your pin with physical button
const int btnPin = 7;

WidgetLED led3(V3);

SimpleTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "blynk-cloud.com", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
  // Or like this:
  //Blynk.begin(auth, "blynk-cloud.com", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);

  // Setup physical button pin (active low)
  pinMode(btnPin, INPUT_PULLUP);

  timer.setInterval(500L, buttonLedWidget);
}

// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
  // Read button
  boolean isPressed = (digitalRead(btnPin) == LOW);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led3.on();
    } else {
      led3.off();
    }
    btnState = isPressed;
  }
}

void loop()
{
  Blynk.run();
  timer.run();
}

 

Try to add Blynk.notify(“blablabla”); to if (isPressed)

you also need to be aware of the limitations of Notify:

http://docs.blynk.cc/#widgets-notifications-push-notifications

1 Like