My plan is to make ARDUINO read DIGITAL PIN 12 and if it’s high , ARDUINO should drive a 5v relay in PIN 13 . also blynk print “phase fault” as string in blynk widget !
seems normal or what ?? or should I pasted the algorithm inside void loop ?
const int relay = 13; //relay driver
const int pin = 12; //input digital
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
char auth[] = "**************";
SoftwareSerial SerialBLE(10, 11); // RX, TX
BlynkTimer timer;
void myTimerEvent()
{
if(digitalRead(pin)==HIGH)
{
digitalWrite(relay,HIGH);
serial.Println("R phase fault");
Blynk.virtualWrite(V0, "R phase fault");
}
else Blynk.virtualWrite(V0,"NORMAL");
}
void setup()
{
Serial.begin(9600);
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Waiting for connections...");
timer.setInterval(1600L, myTimerEvent);
pinMode(relay,OUTPUT);
pinMode(pin,INPUT);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
For how long kept gpio12 high? You needed for at least 1600ms in order to be detected by your timer based function “myTimerEvent”…
I’d use an interrupt to detect the change, rather than sampling the state of the pin on a regular basis.
Depending on which flavour of Arduino you’re using, you may need to use a different pin, because some models don’t allow interrupts to be attached to GPIO12.
Pete.
the DIGITAL PIN 12 could remain high 1.6 seconds
Dear @vis5254 see @PeteKnight suggestion. Interrupt driving event, could be better than timer polling …
2 Likes
thank you sir , but my earlier code could workout for the moment right ?? it’s just for a short term project purpose !
If your code works as desired, then why ask the question?
Pete.
1 Like
sorry sir , it was all about asking whether putting such a code inside a mytimerevent would do the trick or not ?? But since you suggested interrupt , I would like to know what change or an advantage could it bring ?? is there any simple example on using interrupt in ARDUINO UNO …
Or is it enough to go with this conventional code for the time being ?
When you attach an interrupt to a pin, you specify the name of the function that will be called when the state of the pin changes. You can specify the type of interrupt - CHANGE, RISING or FALLING. The names are self explanatory.
A CHANGE interrupt will trigger when the pin goes either LOW to HIGH or HIGH to LOW.
A RISING interrupt is triggered if the pin goes LOW to HIGH.
A FALLING interrupt is triggered if the pin goes HIGH to LOW.
The advantage of using an interrupt is that you don’t have to keep going and checking if the state of the pin in your code, instead you use the built-in interrupt routines that exist within the processor to monitor the state of the pin. This makes your code and the processing of the results simpler and more efficient.
A quick search of this forum and the internet will give you some code examples. It’s up to you to decide if it’s worth making the change. I’m not clear if your existing code is working or not. If not then I’d switch to using interrupts.
Pete.
@vis5254
Your code will work (perhaps not as you would like) and you should not put the code in the void loop.
as @PeteKnight and others have said using the interrupt handler would be better and more efficient. I think you would benefit from the learning experience so that is enough reason to try it.
2 Likes