Define the pin as INPUT_PULLUP

Dear Sir/Madam,

I use Blynk to write a simple program to turn on/off a lamp, I only need to put the Blynk.run() in the loop(), I don’t need to define the pin on Arduino board as INPUT or OUTPUT.

What if I only control the pin 10 and 11 via the Blynk, will Bylnk change the state to other pins?

I have a sensor which is connected to pin 4 and define it as INPUT_PULLUP pin, should I need to define it in Arduino code or Bylnk? I want to read the signal from this pin and then do some calculation, finally I need to display a result on mobile, how can I do that?

Best regards,

Kelvin.

Incorrect, you still define them, but this shouldn’t be done in the void loop, regardless of whether or not you’re using Blynk.

I think you should start by reading these:
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-control-anything-with-blynk-app
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

Pete.

Dear Pete,

Yes I know defining the pin is not in void loop(), thanks for reminder.

Refer to your reply, “you still define them”, do you mean I need to define all pin I used for INPUT, OUTPUT? I read lot of the sample code from Blynk, there is no such coding to define the pinMode.

Best regards,

Kelvin.

Okay, I should probably rephrase my reply. You don’t NEED to declare you pinmodes, you could let Blynk handle that. However, I think it’s still good practice, especially if you’ll be handling Blynk/Wi-Fi disconnects in an elegant way and continuing to do processing offline.
It’s only a couple of lines of code, so not really a big issue as far as I can see.

Pete.

Dear Pete,

Or I try to explain my question with code quoting.

In my code, without using Blynk, I have to define an interruptPin before setup()
const byte interruptPin = 4;

I add below code in setup()

  pinMode(interruptPin, INPUT_PULLUP);
  attachInterupt(digitalPinToInterrupt(interruptPin), magnetFound, CHANGE);

and then there is a Serial.println() in the void magnetFound().

Now I want to show the Serial.println() to mobile using the Blynk, how can I do it? Do I still need to define the pinMode of interruptPin in code or Bylnk will handle it? How about the attachInterupt() function?

Below is the code of the magnetFound()

void magnetFound() {
  state = !state;
  revs++;
  Serial.print("No. of Revolution: ");
  Serial.println(revs);
}

I can’t see the output from Arduino Monitor even I am sure the magnetFound() is called, is the output be fully controlled by Blynk? How can I do?

Best regards,

Kelvin.

Hi Kevin.

You need a widget in the blynk application that allows you to display the values, such as a labeled value, an LCD, a terminal, or even a led, a button, etc.

After put the widget in the app configure the pin associated od that widget to a virtual pin like V0 and in your program code…

Blynk.virtualWrite(V0,“magnet is Found”) if the widget can handle text, some widgets has their own way to do this… like WidgetLCD.print(…) etc.

if is something else like a led you can do this:

Blynk.virtualWrite(V0,0); // if no magnet found
Blynk.virtualWrite(V0,255); // if yes

And forget another thing, you do not need to do more than use the code line for the correct state…

For the next time you have any questions it would be very helpful if you included your source code, so we would know better what you want to do, what things you have available (sensors, type of mainboard, etc) and we could assist you in a more satisfactory way.

This is an example:


#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(4, 5); // RX, TX
#define ESP8266_BAUD 115200

char auth[] = "YourAuthCode";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
const int pinSwitch = 2; //Pin Reed
const int pinLed = 9; //Pin LED

ESP8266 wifi(&EspSerial);

BLYNK_CONNECTED()
{
  Blynk.syncAll();  
}
void magnetFound()
{
  bool isFound=digitalRead(pinSwitch); 
  digitalWrite(pinLed, isFound);
  Serial.print((isFound)?"MAGNET FOUND":"MAGNET NOT FOUND");
  Blynk.virtualWrite(V0,isFound);
}
void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(pinLed, OUTPUT); //Imposto i PIN
  pinMode(pinSwitch, INPUT);
  attachInterrupt(digitalPinToInterrupt(pinSwitch), magnetFound, CHANGE);
}

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

I don’t know why you want to use interrupts… but keep in mind that:

Within the Interrupt function, delay () will not work and the value returned by Millis () will not increase. Serial data received, while the function is active can be lost. You must declare as volatile any variable that is modified within the interruption function.

So is better to use timers to check sensor value instead use interrupts…

Something like


#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
#include <Tasker.h>

#define ESP8266_BAUD 115200

Tasker Task(true);
bool PrevValue;
SoftwareSerial EspSerial(4, 5); // RX, TX
char auth[] = "YourAuthCode";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
const int pinSwitch = 2; //Pin Reed
const int pinLed = 9; //Pin LED

ESP8266 wifi(&EspSerial);

BLYNK_CONNECTED()
{
  Blynk.syncAll();  
}
void magnetFound()
{
  bool isFound=digitalRead(pinSwitch); 
  if (PrevValue!=IsFound)
  {
    PrevValue=IsFound;
    digitalWrite(pinLed, isFound);
    Serial.print((isFound)?"MAGNET FOUND":"MAGNET NOT FOUND");
    Blynk.virtualWrite(V0,isFound);
  }
}
void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(pinLed, OUTPUT); //Imposto i PIN
  pinMode(pinSwitch, INPUT);
  PrevValue=digitalRead(pinSwitch); 
  Task.setInterval(magnetFound, 100L);
}

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

Yes, what @trystan4861 said, with one slight amendment:

LED widgets work on 255 levels of brightness (simulating a PWM system) so lighting an LED widget to brightness 1 would make it appear as off still, so the correct approach would be to set it to 255 when lit and 0 when not.

Blynk.virtualWrite(V0,0);   // if no magnet found
Blynk.virtualWrite(V0,255); // if yes

Blynk (or any other system for that matter) wouldn’t be able to automatically assign interrupts to pins as there are so many options regarding the type of interrupt and which function you want to call when the interrupt is called. Having a system assign them automatically would make it unusable for most situations.
So, you need all the same interrupt definition code, just add the extra Blynk stuff into the function that’s called when the interrupt is triggered.

Pete.

1 Like

Dear Trystan4861,

Many thanks for your sample code, I will study in depth.

Best regards,

Kelvin.

Dear trystan4861,

Many thanks for your reply, I learnt a lot from from your code, I find there is a Tasker.h library and it set the interval 100L, may I know if the time between two time of magnet found is very short, will the count be lost? I want to use a hall effect sensor to detect a magnet present on a wheel, so that’s why I use the interrupt rather then time interval because I never know how frequent I will find the magnet.

Best regards,

Kelvin.

Hi :slight_smile:
My code try to find the magnet 10 times in a second… and when detect a change in the magnet sensor (found->not found/not found->found)… do something…

For your project you must adapt the intervals to avoid to miss one magnet, you must know at least the time one maget will be near the sensor… and then put the interval smaller than that time…

My code is only for put you on the way… maybe it don’t adapt perfectly to your needs

Of course you can put intervals like 10L or even smaller :stuck_out_tongue: