Hey,
I didnt know that this Sketch Builder existed…
Thanks, now i got a sketch, thats (almost) doing what i want.
The only problem is, that the button only works if the app connects to the arduino once. When i start the arduino, the button is not working, as soon as i connect the app to the arduino once, the button works even when the app is closed…
That is pretty annoying, because the power to the arduino cuts off many times so the arduino reboots and i´ve to connect the app once again to use the button…
Is there a way so the button will always work, even when the app hasnt connected?
Im now uising this sketch:
#define BLYNK_USE_DIRECT_CONNECT
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "630738becd584d4d8f007e4b9494036a";
const int ledPin = 10;
const int btnPin = 11;
BlynkTimer timer;
void checkPhysicalButton();
int ledState = LOW;
int btnState = LOW;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V2);
// Alternatively, you could override server state using:
//Blynk.virtualWrite(V2, ledState);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
ledState = param.asInt();
digitalWrite(ledPin, ledState);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin) == HIGH && btnState == LOW) {
// Toggle LED state
ledState = HIGH;
digitalWrite(ledPin, ledState);
// Update Button Widget
Blynk.virtualWrite(V2, ledState);
btnState = HIGH;
}
if (digitalRead(btnPin) == LOW && btnState == HIGH) {
// Toggle LED state
ledState = LOW;
digitalWrite(ledPin, ledState);
// Update Button Widget
Blynk.virtualWrite(V2, ledState);
btnState = LOW;
}
}
void setup()
{
// Debug console
DebugSerial.begin(9600);
DebugSerial.println("Waiting for connections...");
// Blynk will work through Serial,
// Do not read or write this serial manually in your sketch
Serial.begin(9600);
Blynk.begin(Serial, auth);
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
digitalWrite(ledPin, ledState);
timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
timer.run();
}
Thanks.
~Jan