Invert Output

Yes @Costas, you are reading my mind!

@zara Have you tried looking at example sketches that come with the Blynk Library?

This one shows how to get data from the app. Please read the comments carefully.

I got you…
BLYNK_WRITE(V1) is a subroutine with (V1) variable

but there is still one question:
How to bind gpio2, for example, to V1?
I cant do if in UI!)))

BLYNK_WRITE(V1)
{
  if (param.asInt() == 0) {
    digitalWrite(2, HIGH);
  } else {
    digitalWrite(2, LOW);
  }
}

Got it! thankyou very mutch for your help

Pleace help me once again))))

How can i set default gpio state?
I add this code to sketch and got an invert output, but
BLYNK_WRITE(V1)
{
if (param.asInt() == 0) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
}
It does not work, then there is no button with gpio2 binded
but if that button exist it sets default gpio2 state to HIGH and my relay turning on after powerlost and return

Here, I think this is everything you are wanting…

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

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "SID", "Pass");
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);  // Make this what ever you want you default value to be.
}

BLYNK_WRITE(V1)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(2, HIGH);
  } 
else
  {
    digitalWrite(2, LOW);
  }
}

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

Could I request that this capability be added as an enhancement to the Button widget? Although it can be done using a virtual pin, I think it’s a common enough occurrence that it would be nice to be able to set inverted output in the widget itself. You could just add another switch for normal or inverted output, like the present one that allows selecting push or switch mode.

It would also be nice to have the same capability for the Timer widget.

All the other Controller widgets already allow you to invert the output by setting a “high to low” range instead of “low to high”

The problem with this is that the Button still “lights up” with the colour when it’s on, even if you’ve changed the label to “OFF”. It would be confusing if you had some active high buttons and some active low.

1 Like

Sure. We will implement this some day.

1 Like

Thank you! The problem was solved with one remark
I have to write HIGH to set LOW defaul value and LOW to set HIGH)))

Hi what is the cod3 for inverted the relay? Thanks

BLYNK_WRITE(V1)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(2, HIGH);
  } 
else
  {
    digitalWrite(2, LOW);
  }
}

As was said somewhere above here :slight_smile:

we are waiting for that update soon :slight_smile:

1 Like

Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.

 Downloads, docs, tutorials: http://www.blynk.cc
 Blynk community:            http://community.blynk.cc
 Social networks:            http://www.fb.com/blynkapp
                             http://twitter.com/blynk_app

Blynk library is licensed under MIT license
This example code is in public domain.


This example shows how to use Arduino Ethernet shield (W5100)
to connect your project to Blynk.
Feel free to apply it to any other example. It’s simple!

NOTE: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
DON’T use them in your sketch directly!

WARNING: If you have an SD card, you may need to disable it
by setting pin 4 to HIGH. Read more here:
https://www.arduino.cc/en/Main/ArduinoEthernetShield

**************************************************************/

#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>
#include <TimeLib.h>
#include <WidgetRTC.h>

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

const int ledPin = 7; // out
const int btnPin = 8; // in

SimpleTimer timer;
WidgetLED led1(V15);
WidgetRTC rtc;

BLYNK_ATTACH_WIDGET(rtc, V10);

void checkPhysicalButton();

int ledState = LOW;
int btnState = HIGH; //HIGH
//Sets Relays to Off Position**********
#define TURN_ON 0 // TURN_ON and TURN_OFF are defined to account for Active High relays
#define TURN_OFF 1 // Used to switch relay states for on/off of AC devices

void setup()
{
Serial.begin(9600);
Blynk.begin(auth, “192.168.1.203”, 8442);
//Blynk.begin(auth);
// You can also specify server.
// For more options, see BoardsAndShields/Arduino_Ethernet_Manual example
Blynk.begin(auth, “192.168.1.203”, 8442);
//Blynk.begin(auth, IPAddress(192,168,1,100), 8888);
{
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP); // input_pullup
digitalWrite(ledPin, ledState);
digitalWrite(ledPin, TURN_OFF); // remain off till command is recieve
//digitalWrite(ledPin, LOW); //button

// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);

}
while (Blynk.connect() == false) {
// Wait until connected
}
{ // Begin synchronizing time
rtc.begin();
// Display digital clock every 10 seconds
timer.setInterval(5000L, clockDisplay);
timer.setInterval(1000L, clockvalue);
}
}
void checkPin()
{
// Invert state, since button is “Active LOW”
if (digitalRead(7)) {
led1.off();
} else {
led1.on();
}
}
// Every time we connect to the cloud…
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V0);

// Alternatively, you could override server state using:
//Blynk.virtualWrite(V2, ledState);
}
// When App button is pushed - switch state
BLYNK_WRITE(V0) {
ledState = param.asInt();
digitalWrite(ledPin, ledState);
{
//BLYNK_LOG(“Got a value: %s”, param.asStr());
// You can also use:
int i = param.asInt();
int state;
// Switch mode inverse//
if (i == 0) {
digitalWrite(ledPin, HIGH);
Serial.println(“Relay 1 = OFF”); // turn OFF Relay 1
}
if (i == 1) {
digitalWrite(ledPin, LOW); // turn ON Relay 1
Serial.println(“Relay 1 = ON”);
delay (300);
}
}
/*{
if (param.asInt() == 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
} */
}

void checkPhysicalButton()
{
if (digitalRead(btnPin) == LOW) {
// btnState is used to avoid sequential toggles

if (btnState != LOW) {

  // Toggle LED state
  ledState = !ledState;

  digitalWrite(ledPin, ledState);

  // Update Button Widget
  Blynk.virtualWrite(V0, ledState);
}
btnState = LOW;

} else {
btnState = HIGH;
}

/*
}
BLYNK_WRITE (V2)
{
if (param.asInt() == 0)
{
digitalWrite(7, HIGH);
}
else
{
digitalWrite(7, LOW);
}
*/
}

void clockDisplay()
{
// You can call hour(), minute(), … at any time
// Please see Time library examples for details
BLYNK_LOG(“Current time: %02d:%02d:%02d %02d %02d %d”,
hour(), minute(), second(),
day(), month(), year());
}

// Digital clock display of the time
void clockvalue()
{

int gmthour = hour();
if (gmthour == 24) {
gmthour = 0;
}
String displayhour = String(gmthour, DEC);
int hourdigits = displayhour.length();
if (hourdigits == 1) {
displayhour = “0” + displayhour;
}
String displayminute = String(minute(), DEC);
int minutedigits = displayminute.length();
if (minutedigits == 1) {
displayminute = “0” + displayminute;
}
String displaysecond = String(second(), DEC);
int seconddigits = displaysecond.length();
if (seconddigits == 1) {
displaysecond = “0” + displaysecond;
}
String displaycurrenttime = displayhour + “:” + displayminute + “:” + displaysecond;
Blynk.virtualWrite(V11, displaycurrenttime);

}

void loop()
{
Blynk.run(); // Run Blynk
timer.run(); // Run SimpleTimer
}

Button is working fine, you can invert the status in any condition as you like by changing the sate from high to low and low to high, defend on your requirements. just include the simple timer. look for ( V0 SKETCH above) your problem will be solve…

This kecth was tested for UNO and MEGA 2560…

Enjoy Have fun Blynking…

thanks bro! this code works here!

Hi!

This works perfectly, however, how would you “invert” the PWM, such as a slider ?

Thanks and all the best, M

Did you try swapping the range numbers in slider settings?

OK, I got the “inverted PWM” going, not exact solution, but simple and does the job for me at the moment…

BLYNK_WRITE(V2)
{
// assigning incoming value from pin V2 to a variable
int pinValue = param.asInt();
int new_Value = 1023 - pinValue*4;
analogWrite(2, new_Value);
Serial.print(“V2 original value:”);
Serial.println(pinValue);
Serial.print(“V2 new value:”);
Serial.println(new_Value);
}

And YES, swapping the slider setting does the job as well! :grimacing: