Need help placing this code

I make it short.

I want to place this code (that works fine):

int dark=1000; //Specified in Hz
int light=1300; //Specified in Hz
int buzzPin=12; 
int timeOn=300; //specified in milliseconds
int timeOff=300; //specified in millisecods


void setup() {

}

void loop() {
 tone(buzzPin, dark);
 delay(timeOn);
 noTone(buzzPin);
 tone(buzzPin, light);
 delay(timeOff);
}

Placed innside this code (That also works fine):

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "tdycdPRXW5ovPMtxJ6U9MffR0sdvJm-e";

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

int ledred = 14;
int ledyellow = 12;
int ledblue = 13; 

void ledgrid(){
  if (sensors.getTempCByIndex(0) <20){
  digitalWrite(ledblue, HIGH);
  digitalWrite(ledyellow, LOW);
  digitalWrite(ledred, LOW);
  }
if (sensors.getTempCByIndex(0) >=20 && sensors.getTempCByIndex(0) <=22){
  digitalWrite(ledblue, LOW);
  digitalWrite(ledyellow, HIGH);
  digitalWrite(ledred, LOW);
}
if (sensors.getTempCByIndex(0) >25){
  digitalWrite(ledblue, LOW);
  digitalWrite(ledyellow, LOW);
  digitalWrite(ledred, HIGH);
  Blynk.notify("WARNNG! Over 60 grader i boksen!");
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledblue, OUTPUT);
}

void loop() {
  Blynk.run();
  sensors.requestTemperatures();
  Serial.print("Temperatur: ");
  Serial.println(sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));
  ledgrid();
  }

So what do i want the first code to do inside the last one?

When the temperature is over 25 i want the buzzer to play the alarm signal in the first code. When the temperature gets under 25, i want the rest of the code run.

I have tried with while loop and the buzzer started and it worked, but it never get outs of the while lopp again and my serial monitor doesent print any values when it was in while loop.

How cn i do that in the simpliest way?

Put the code of the loop() function from the first code in a function with a different name at the end of the second code. When you want the tones to play then call the new function. Don't forget to turn the tone off when you are finished with it, perhaps at the end of the new function

don't share your SSID and Password publicly on the internet....

char ssid[] = "Jabexxxxxxi";
char pass[] = "Birxxxxx";

you want to test When the temperature is over 25, so use a if, not a while (the loop loops so you'll come back there)

this way you do what you need to do and then keep on with the rest of the code.

note that Blynk.run();needs to be called often, so don't make your buzzer's delays too long, you are already spending 0.6s there, it's a looooong time... you might need a state machine and read the Using millis() for timing. A beginners guide tutorial.

UKHeliBob:
Put the code of the loop() function from the first code in a function with a different name at the end of the second code. When you want the tones to play then call the new function. Don't forget to turn the tone off when you are finished with it, perhaps at the end of the new function

Can you please give me a quick example of what you mean here?

Bjerknez:
Can you please give me a quick example of what you mean here?

void buzzzzz() {
 tone(buzzPin, dark);
 delay(timeOn);
 tone(buzzPin, light);
 delay(timeOff);
 noTone(buzzPin);
}

Thanks :slight_smile:

i have done that now, and the buzzer turns off when temperature falls under 25, but the shift between high and low freq tone is uneven. Most likely because the rest of the code executes before it start over again etc.

When i tried the while loop it worket, but i could not get out of the while loop again...

Off course, i could just make it beep constantly instead og the frequency change, but i want to get it to work properly with 200ms high and 200ms low freq.

Her is my last code:

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int dark=1000; //Specified in Hz
int light=1300; //Specified in Hz
int buzzPin=15;
int timeOn=300; //specified in milliseconds
int timeOff=300; //specified in millisecods

char auth[] = "***";

char ssid[] = "***";
char pass[] = "***";

int ledred = 14;
int ledyellow = 12;
int ledblue = 13; 

void buzzer(){
 tone(buzzPin, dark);
 delay(timeOn);
 noTone(buzzPin);
 tone(buzzPin, light);
 delay(timeOff);
}

void ledgrid(){
  if (sensors.getTempCByIndex(0) <20){
  digitalWrite(ledblue, HIGH);
  digitalWrite(ledyellow, LOW);
  digitalWrite(ledred, LOW);
  noTone(buzzPin);
  }
if (sensors.getTempCByIndex(0) >=20 && sensors.getTempCByIndex(0) <=25){
  digitalWrite(ledblue, LOW);
  digitalWrite(ledyellow, HIGH);
  digitalWrite(ledred, LOW);
  noTone(buzzPin);
}
if (sensors.getTempCByIndex(0) >25){
  digitalWrite(ledblue, LOW);
  digitalWrite(ledyellow, LOW);
  digitalWrite(ledred, HIGH);
  buzzer();
  Blynk.notify("WARNNG! Over 60 grader i boksen!");
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledblue, OUTPUT);
  pinMode(buzzPin, OUTPUT);
}

void loop() {
  Blynk.run();
  sensors.requestTemperatures();
  Serial.print("Temperatur: ");
  Serial.println(sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));
  ledgrid();
  }

For Blynk to work properly your void loop should be clean. See Blynk documentation about this. You should only have Blynk.run(); and timer.run(); in your loop. The way Blynk works with functions is you must include BlunkTimer at the beginning then you create your void functions and lastly in your setup you state how often you want the timer to run.

Example

BlynkTimer timer; // declare the timer

BLYNK_CONNECTED()
{
  Blynk.syncAll() // sync your virtual pins
}

void someAction()
{
 Actionable stuff here
}

void setup()
{
  Blynk.connect();
  timer.setInterval(1000L, someAction);
}

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

Can you please show me how the finnish code should look like if you place my codes over in that blynk code?

I have read about Blynk timer, but i do not fully understand how it works...yet...

Here you go. I have used the second code you posted with a few amendments showing you how the BlynkTimer works

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
BlynkTimer timer;


char auth[] = "";
char ssid[] = "";
char pass[] = "";

//PIN ASSIGNEMENTS//
int ledred = 14;
int ledyellow = 12;
int ledblue = 13;
#define ONE_WIRE_BUS 2

//GLOBAL VARIABLES//
float SensorTemp; //New variable to hold temp
OneWire oneWire(ONE_WIRE_BUS);     
DallasTemperature sensors(&oneWire);  

void ledgrid()
{
  sensors.requestTemperatures();
  SensorTemp = sensors.getTempCByIndex(0); //Get current temp
  char T[6];
  sprintf(T, "%.2f", SensorTemp);
  if (SensorTemp <20)
  {
    digitalWrite(ledblue, HIGH);
    digitalWrite(ledyellow, LOW);
    digitalWrite(ledred, LOW);
    Blynk.virtualWrite(V5, T);
  }
  else if (SensorTemp >= 20 && SensorTemp <= 22)
  {
    digitalWrite(ledblue, LOW);
    digitalWrite(ledyellow, HIGH);
    digitalWrite(ledred, LOW);
    Blynk.virtualWrite(V5, T);
  }
  else if (SensorTemp >=25)
  {
    digitalWrite(ledblue, LOW);
    digitalWrite(ledyellow, LOW);
    digitalWrite(ledred, HIGH);
    Blynk.virtualWrite(V5, "ERROR");
    Blynk.notify("WARNNG! Over 60 grader i boksen!");
  }
  else
  {
    //INSERT CODE FOR WHEN NONE OF THE ABOVE CONDITIONS ARE TRUE OR ELSE DO NOTHING
  }
}
    
//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

void setup()
{
  Serial.begin(9600);
  Blynk.connect();
  Blynk.begin(auth, ssid, pass);
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledblue, OUTPUT);
  pinMode(ONE_WIRE_BUS, INPUT);
  sensors.begin();
  sensors.setResolution(10);
  timer.setInterval(1000L, ledgrid); // ledgrid function will happen every 1 second. Change to suit yor requirements
}

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

Wow, thanks mate :slight_smile:

The only things i did not understand is this line of code is:

 char T[6];
  sprintf(T, "%.2f", SensorTemp);

And i understand that the buzzer beeping should be placed under else statement?

Google "sprintf"

But what does this two lines of code actually do? I understand it have something to to with the temperatur, bit do i have to use this code, or can i use an another code that does the same, that i understand?

It allocates memory for a text buffer, and then fills the buffer with a text conversion of the floating point value that is passed to it. You would find out the same thing if you looked it up as I suggested.

In this code, the result 'T' is never used, so it's a complete red herring. You can delete it.

sprintf in this situation will give you a temp reading to 2 decimal places

Proietti:
sprintf in this situation will give you a temp reading to 2 decimal places

If you print 'T', that is...

Yes correct if you print T. In Blynk I found that sometimes if a reading had multiple (more than 2 decimal places) and if your widget was not big enough it would just print dots hence the sprintf T. So in your code just change

Blynk.virtualWrite(V5, T);

Proietti:
Here you go. I have used the second code you posted with a few amendments showing you how the BlynkTimer works

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//OneWire oneWire(ONE_WIRE_BUS);    //Take this out
//DallasTemperature sensors(&oneWire);  //Take this out
BlynkTimer timer;

char auth[] = "";
char ssid[] = "";
char pass[] = "";

//PIN ASSIGNEMENTS//
int ledred = 14;
int ledyellow = 12;
int ledblue = 13;
#define ONE_WIRE_BUS 2

//GLOBAL VARIABLES??
float SensorTemp; //New variable to hold temp

void ledgrid()
{
  sensors.requestTemperatures();
  SensorTemp = sensors.getTempCByIndex(0); //Get current temp
  char T[6];
  sprintf(T, "%.2f", SensorTemp);
  if (SensorTemp <20)
  {
    digitalWrite(ledblue, HIGH);
    digitalWrite(ledyellow, LOW);
    digitalWrite(ledred, LOW);
    Blynk.virtualWrite(V5, SensorTemp);
  }
  else if (SensorTemp >= 20 && SensorTemp <= 22)
  {
    digitalWrite(ledblue, LOW);
    digitalWrite(ledyellow, HIGH);
    digitalWrite(ledred, LOW);
    Blynk.virtualWrite(V5, SensorTemp);
  }
  else if (SensorTemp >=25)
  {
    digitalWrite(ledblue, LOW);
    digitalWrite(ledyellow, LOW);
    digitalWrite(ledred, HIGH);
    Blynk.virtualWrite(V5, "ERROR");
    Blynk.notify("WARNNG! Over 60 grader i boksen!");
  }
  else
  {
    //INSERT CODE FOR WHEN NONE OF THE ABOVE CONDITIONS ARE TRUE OR ELSE DO NOTHING
  }
}
   
//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

void setup()
{
  Serial.begin(9600);
  Blynk.connect();
  Blynk.begin(auth, ssid, pass);
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledblue, OUTPUT);
  pinMode(ONE_WIRE_BUS, INPUT);
  sensors.begin();
  sensors.setResolution(10);
  timer.setInterval(1000L, ledgrid); // ledgrid function will happen every 1 second. Change to suit yor requirements
}

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

Tried this code now.

That code does not compile. "Sensors not declared in the scope"

... because you commented out the declaration of 'sensors'. Oh wait, it wasn't you... but it's commented out.

Sorry my fault there comment back in the two lines that have the comment "take this out". I was writing that code using my mobile and I wanted to move them to the global variables section and forgot to.

I just reworked my own code in the meantime and it seems to work now :slight_smile:

I have just not understood that something can run in loop outside loop. But with Blynk i now see that is possible :slight_smile: