I want to activate a relay for a ammount of time if a flame sensor is true

So i want to build a fire extinguishing robot and,to be honest,i’m kinda lazy to search the blynk community for an answer so ill put the code below:


BlynkTimer timer1;


int ok1=0;
void terminat1()
{
  ok1=0;
}

void setup() {
  dht.begin();
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(Releu,OUTPUT);
  pinMode(Flacara,INPUT);
  lcd.begin(16,2);
  lcd.clear();
  servo.attach(servoPin);
  digitalWrite(Releu, HIGH);
  timer1.setTimeout(2000L, terminat1);
  //timer3.setInterval(2000L,Deschis);
  servo.write(120);
}

void loop() {
  Blynk.run();
  int flacara=digitalRead(Flacara);
  if(flacara==LOW)
  {
    digitalWrite(Releu,LOW);
    ok1=1;
    timer1.run();
  }
  else if(ok1==0)
  {
    digitalWrite(Releu,HIGH);
  }

the"Releu" stands for Relay and “Flacara” stands for flame

below is the example with the delay function


if(flacara==LOW)
  {
    digitalWrite(Releu,LOW);
    delay(2000);
  }
else
 {
digitalWrite(Releu,HIGH);
}

As you can see,this code means that the relay will stay open for another 2 seconds after the flame sensor gets high

Now,is it possible to make this with a blynk timer?

Oh dear!
Cluttered void loop, a pointless timeout timer in void setup, and lots of stuff missing from your sketch.

A little bedtime reading for you…

Pete.

I know that my code is incomplete because I have a servo that needs to do a similar thing to what I describe and an LCD to print something on it.

I just wanna know if I can do what I described with a blynk timer so I can go on with my code

Your code is missing Blynk libraries, Blynk and WiFi credentials variable definitions etc etc.

If you read the Bynk/SimpleTimer post then you’ll see that it’s possible using a timeout timer.

Pete.

I didn’t read that good enough,thanks!

Posting partial code often isn’t useful.

Yes…

Pete.

#define BLYNK_TEMPLATE_ID "TMPL4J58z1nC7"
#define BLYNK_TEMPLATE_NAME "Nextlab"
#define BLYNK_AUTH_TOKEN "EIU_I1FTjr8odEmX7V38s5ul0Kz9_QDw"
#define BLYNK_PRINT Serial 

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>



char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "Eu_xxx";
char pass[] = "adelin26";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(11, 12); // RX, TX


#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

#include <Servo.h>
#include <Ultrasonic.h>
#include <DHT.h>
#include <LiquidCrystal.h>

#define ultrasonic_pin 2
#define DHTPIN A1
#define DHTTYPE DHT11
#define Flacara A3
#define Releu 13

int servoPin = 3,distanta,temp,hum;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DHT dht(DHTPIN, DHTTYPE);
//Servo servo;
Ultrasonic ultrasonic(ultrasonic_pin);

BlynkTimer timer;

void setup() {
  dht.begin();
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(Releu,OUTPUT);
  pinMode(Flacara,INPUT);
  lcd.begin(16,2);
  lcd.clear();
  //servo.attach(servoPin);
  //digitalWrite(Releu,HIGH);
}

void loop() {
  Blynk.run();
  int flacara=digitalRead(Flacara);
  Serial.print(flacara);
  if(flacara==LOW)
  {
    digitalWrite(Releu,LOW);
  }
  else
  timer.setTimeout(10000L,[]()
    {
      digitalWrite(Releu,HIGH);
    });

  temp = dht.readTemperature();
  hum = dht.readHumidity();
  lcd.setCursor(0,0);
  lcd.print("Temperatura:");
  lcd.print(temp);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Umiditate:");
  lcd.print(hum);
  lcd.print("%");
  distanta=ultrasonic.read();
  delay(100);
}

now i have other issues.So i tried to introduce the timeout function in my if and it isn’t working,my relay stays on.If i uncomment the //digitalWrite(Releu,HIGH); in the setup void,it will reset itself because the void repeats itself.And if i tried to put the timeout timer in the if,not the else,it wouldn’t work either

can you help me?

and also,should i let the lcd printing in the loop void or should i change it

Restructure your code as explained in the “keep your void loop clean” article, so that you just have Blynk.run() and timer.run() in your void loop and you’ll see a very different result.

Pete.

:slight_smile: It is true that i will see very different result:)

#define BLYNK_TEMPLATE_ID "TMPL4J58z1nC7"
#define BLYNK_TEMPLATE_NAME "Nextlab"
#define BLYNK_AUTH_TOKEN "EIU_I1FTjr8odEmX7V38s5ul0Kz9_QDw"
#define BLYNK_PRINT Serial 

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>



char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "Eu_xxx";
char pass[] = "adelin26";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(11, 12); // RX, TX


#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

#include <Servo.h>
#include <Ultrasonic.h>
#include <DHT.h>
#include <LiquidCrystal.h>

#define ultrasonic_pin 2
#define DHTPIN A1
#define DHTTYPE DHT11
#define Flacara A3
#define Releu 13

int servoPin = 3,distanta,temp,hum;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DHT dht(DHTPIN, DHTTYPE);
Servo servo;
Ultrasonic ultrasonic(ultrasonic_pin);

BlynkTimer timer;

void main1()
{
   int flacara=digitalRead(Flacara);
  //Serial.print(flacara);
  if(flacara==LOW)
  {
    digitalWrite(Releu,LOW);
    timer.setTimeout(2000L,[]()
    {
      digitalWrite(Releu,HIGH);
    });
  }
  temp = dht.readTemperature();
  hum = dht.readHumidity();
  lcd.setCursor(0,0);
  lcd.print("Temperatura:");
  lcd.print(temp);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Umiditate:");
  lcd.print(hum);
  lcd.print("%");
  distanta=ultrasonic.read();
  delay(100);
}


void setup() {
  dht.begin();
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(Releu,OUTPUT);
  pinMode(Flacara,INPUT);
  lcd.begin(16,2);
  servo.attach(servoPin);
  timer.setInterval(1000L,main1);
  //digitalWrite(Releu,HIGH);
}

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

now my lcd would pulse blank for 1 second.:slight_smile: but i think i,ll find a soultion.
aaand the if is still not working.
can you tell me or give me a post about this kind of if other than that q&a about timers so i could inspire?cause i didn’t find anything.

and btw,in that tutorial about kepping void loop clean it is not explained how to do this with other things except sensors

What did the “keep your void loop clean” article say about delays?

Sorry, I don’t understand your question.

The principal is the same for all situations, but you are using a sensor, and also reading the value of a digital pin, which is also a sensor of sorts.

Pete.

Ok,so,first of all,the delay is for a ultrasonic sensor and it would be more complicated to add a timer
Second,i was reffering to the

  {
    digitalWrite(Releu,LOW);
    delay(2000);
  }
else
 {
digitalWrite(Releu,HIGH);
}

and if you saw posts like this and if you can help me with that.

and as for the sensor and etc,i can figure them out but first,i need help with that if.

Think of it like a car:what’s the point on repairing the body if the engine isn’t working

I might be too dumb to make this work,because i’m a begginer but i just need to insipire from someone so i should know what to when i encounter similar situations

i tried for almost 4 hours to make this code work

#define BLYNK_TEMPLATE_ID "TMPL4J58z1nC7"
#define BLYNK_TEMPLATE_NAME "Nextlab"
#define BLYNK_AUTH_TOKEN "EIU_I1FTjr8odEmX7V38s5ul0Kz9_QDw"
#define BLYNK_PRINT Serial 

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>



char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "Eu_xxx";
char pass[] = "adelin26";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(11, 12); // RX, TX


#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

#include <Servo.h>
#include <Ultrasonic.h>
#include <DHT.h>
#include <LiquidCrystal.h>

#define ultrasonic_pin 2
#define DHTPIN A1
#define DHTTYPE DHT11
#define Flacara A3
#define Releu 13

int servoPin = 3,distanta,temp,hum;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DHT dht(DHTPIN, DHTTYPE);
//Servo servo;
Ultrasonic ultrasonic(ultrasonic_pin);

BlynkTimer timer;



bool controlReleu = HIGH;
void setup() {
  dht.begin();
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  //Blynk.begin(auth, wifi, ssid, pass);
  pinMode(Releu,OUTPUT);
  pinMode(Flacara,INPUT);
  lcd.begin(16,2);
  lcd.clear();
  timer.setInterval(500L,releu);
  //servo.attach(servoPin);
  digitalWrite(Releu,controlReleu);
}

void loop() {
  Blynk.run();
  timer.run();
  temp = dht.readTemperature();
  hum = dht.readHumidity();
  lcd.setCursor(0,0);
  lcd.print("Temperatura:");
  lcd.print(temp);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Umiditate:");
  lcd.print(hum);
  lcd.print("%");
  distanta=ultrasonic.read();
  delay(100);
  
}

void releu()
{
  int flacara=digitalRead(Flacara);
  Serial.print(flacara);
  int ok=0;
if (flacara == LOW) {
  controlReleu=LOW;
  digitalWrite(Releu, LOW);
  ok=1;
}
  else if(ok==0||controlReleu==LOW)
  timer.setTimeout(2000L, []() {
    digitalWrite(Releu, HIGH);
    controlReleu=HIGH;
  });
}

I know that this code isn’t the most efficient and up to the blynk standards but i just want to find out how should i do my if.In this example,by my logic it should work,but it won’t.After i resolve with the if i could start putting my other things in timers and arrange my loop void

Your code execution goes something like this…

  • Interval timer calls function main1()
  • main1() executes, and ends with a 100ms delay. During this delay all code execution stops
  • Once the 100ms delay has completed void loop executes
  • timer.run() within the void loop checks if the interval timer is due to be executed again. Initially it won’t be due to be ececuted, so the void loop repeats.
  • After several thousand void loop executions it will be 1000ms since main1() was last executed, so this time it will execute.

As you can hopefully see, the 100ms delay at the end of main1() serves no purpose other than to prevent the void loop from being executed, and disrupting Blynk processes and the execution of the timer library.

It needs to be removed.

As far as your other issues are concerned, I’d suggest that you add some serial print statements so you can understand how your program flow is working.

Pete.