Issues with my slider

Hello, i am trying to create a project for my school, but i ran into some issues. My task was to create a program that would allow me to measure the temp and moisture in a room, and controll the ‘‘heating’’ via slider in the blynk app (the temp/moisture info should be displayed there too) I got trough the info part but i am stuck on the slider controlling the ‘‘heating’’. In my case its the LED on my MKR 1000 (wifi). Im supposed to turn the LED on once my slider goes over 50%. I tried looking on the forums but couldnt find anything. Please help.

This is what my code looks like so far, as i said it connects to my phone and shows the info i need, i just need help with the slider.

Im using a
DHT11 sensor
Arduino MKR 1000 (wifi)
Blynk for android
(led on my mkr1000)


#define BLYNK_PRINT SerialUSB
 
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>
#include <DHT.h>
 

char auth[] = "o";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "o";
char pass[] = "o";
 
#define DHTPIN 2
 

#define DHTTYPE DHT11     
DHT dht(2,11);
BlynkTimer timer;
 

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h);
}
 
void setup()
{
  pinMode(14, OUTPUT);
  // Debug console
  SerialUSB.begin(9600);
 
  
 
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
 
  dht.begin();
 
  
  timer.setInterval(1000L, sendSensor);
}

BLYNK_WRITE(V1){  // This function gets called each time something changes on the widget
  int value = param.asInt();  // This gets the 'value' of the Widget as an integer
  analogWrite(14  value);  // this send a PWM signal based on 'value' to the GPIO pin
}
 
void loop()
{
  
  
  
  Blynk.run();
  
 
  timer.run();
}

This is my first post here so im sorry in advance if i messed something up, had to turn to this website since my mentor isnt of any help.

That seems like a strange simulation of a climate control system.
Normally, you’d be using the slider to set the threshold at which the LED turns on/off, and taking the reading from the reading from the DHT11 and comparing the two.
If for example the slider is set to 50% and the DHT humidity reading is 80% then you want the LED on, but if the DHT humidity drops below 50%, if the slider setting is increased above 80%, then you want the LED to go off.

Pete.

Thanks Pete, that actually sounds way better, and i think it might work, but how do i start coding that? Is there a thread on this site that could help me out? Im new to Blynk and arduino so all ive done so far is copy stuff i saw online…

This piece of code gets the value from the slider and stores it in a LOCAL variable called value.
Because it’s a local variable, you can’t use it anywhere else in your code. So, you need to change this to a GLOBAL variable by declaring it at the top of your code, where you declare your other variables such as ssid, pass etc.
I’d also recommend that you give it a more appropriate name, maybe humidty_setpoint.

Once you’ve done this your BLYNK_WRITE(V1) will look like this:

BLYNK_WRITE(V1)
{
  humidty_setpoint = param.asInt(); 
}

Then, in your sendSensor() function (which also ought to have a more appropriate name), you then need to do a comparison between the humidity reading that you’ve just taken and the humidty_setpoint value using an 'if` statement.

Your logic may then be that if the humidity is higher than the humidty_setpoint then turn our LED on, else turn your LED off.
I’ll let you translate that into actual code, otherwise you wont learn anything.

Also, don’t use an analogWrite command to turn the LED on/off, use a digitalWrite command instead.
If your LED is active HIGH (on when a HIGH signal is applied) then digitalWrite(14, HIGH) will turn it on, and LOW will turn it off. But, the ,logic may be reversed, you’ll need to check.

The other thing you’ll need is a Blynk.syncVirtual(V1) command in a BLYNK_CONNECTED function, to get the value from the slider at start-up.

More info on that here:

In real-world control systems like this, you’d obviously normally monitor the temperature rather than the humidity, and put the heating on if the temperature is below the set point, and turn it off if it’s above the set point.
With real systems what you want to avoid is the heating constantly turning on an off as the temperature hovers around the set point. You achieve this by building-in some “insensitivity” to the system, by waiting until the temperature is slightly below the set point before turning the heating on, and not turning the heating off again until the temperature is slightly above the set point. This is known as Hysteresis.

More info here…

Pete.

  1. the code you posted doesn’t include OneWire, are you now using a different sketch?

  2. you don’t see these lines of code??

  1. I’m guessing from this comment that you’ve decided to abandon your code and use the code from the link I posted. That wasn’t what I was suggesting you should do.

Pete.

Yeah i used the one you posted, now im kinda confused on what im supposed to do with the code you sent, I dont really know how and if i should combine them. Maybe i need to read everything again, :face_with_raised_eyebrow:

#define BLYNK_PRINT SerialUSB
 
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>
#include <DHT.h>
 
float targetTemp;
float temp;
char auth[] = "o";
void takeTempReading()
{
  if (sensors.read())
  {
    temp = sensors.fahrenheit() // Take a temperature reading and save it to the variable temp
    Serial.printf("Temperature  F ");
    Serial.println(temp); // Print the saved temperature value without having to re-read the sensor 
    Blynk.virtualWrite(V1, targetTemp); // Write the saved temperature out to the Blynk widget on pin V1
  }
  if(temp >= targetTemp)
 {
  digitalWrite(D7, HIGH);
  digitalWrite(D0, HIGH);
  Serial.println ("Temperature is at or above target");
 }
 else
 {
   digitalWrite(D7, LOW);
   digitalWrite(D0, LOW);
   Serial.println("Temperature is below target");
 }
}
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V26); // Ask the Blynk server for the current Target Temperature
  Blynk.notify("CONNECTED");
}
BLYNK_WRITE(V26) // Step widget used to set the Target Temperature
{
  targetTemp = param.asFloat();
  Blynk.virtualWrite(V25, targetTemp); //
  Serial.printf("Step");
  Serial.println(targetTemp);
}
Serial.printf("Temperature  F ");
    Serial.println(temp); // Print the saved temperature value without having to re-read the sensor   
    Blynk.virtualWrite(V1, targetTemp); // Write the saved temperature out to the Blynk widget on pin V1
  } 
char ssid[] = "o";
char pass[] = "o";
#define DHTPIN 2
#define DHTTYPE DHT11     
DHT dht(2,11);
BlynkTimer timer;
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h);
}
void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth);
  pinMode(D0, OUTPUT);
  pinMode(D7, OUTPUT);
  timer.setInterval(5000, takeTempReading);
}
{
  pinMode(14, OUTPUT);
  // Debug console
  SerialUSB.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80); 
  dht.begin();
  timer.setInterval(1000L, sendSensor);
}

BLYNK_WRITE(V1){  // This function gets called each time something changes on the widget
  int value = param.asInt();  
  analogWrite(14  value);  
}
 
void loop()
{
  Blynk.run();
  timer.run();
}

I know i messed something up here and i know im probably looking dumb right now but this is my attempt at combining the 2 codes (if thats even what im supposed to do.)
Oh yea i also tried renaming the '‘BLYNK_WRITE(V1){ // This function gets called each time something changes on the widget
int value = param.asInt();
analogWrite(14 value); ‘’’ to humidity_setpoint but i dont think i defined it right, it kept giving me an error.

I feel kinda dumb right now :sweat_smile:

Probably!

Pete.

void takeTempReading()
{
  if (sensors.read())
  {
    temp = sensors.fahrenheit() // Take a temperature reading and save it to the variable temp
    
    Serial.printf("Temperature  F ");
    Serial.println(temp); // Print the saved temperature value without having to re-read the sensor
    
    Blynk.virtualWrite(V1, targetTemp); // Write the saved temperature out to the Blynk widget on pin V1
  }

  if(temp >= targetTemp)
 {
  digitalWrite(D7, HIGH);
  digitalWrite(D0, HIGH);
  Serial.println ("Temperature is at or above target");
 }
 else
 {
   digitalWrite(D7, LOW);
   digitalWrite(D0, LOW);
   Serial.println("Temperature is below target");
 }
}

I think i understand this part of the code you sent me,
The first few lines take the reading and the desired temp, then the IF command compares them, if the temp is at at above the desired temp the LED glows, if its under it stops glowing? Unless i got it wrong and its the other way around, im using my own logic here, high = on, low= off.

I just dont know how and where to place that in my code and to not break something in the process.

I didn’t ‘send’ you that code, I provided a link to a later post in that topic which discussed hysteresis in more detail.

If you want to plagiarise that code for your school project then you you’re taking the wrong approach to learning coding in my opinion.

Pete.

Im not trying to take it and say its mine, i wanna make it work and understand why it works, the project is something i wanted to do for fun not something i had to do and present, dont get me wrong im not using this code to steal it, im trying to learn how and why it works, Its kinda hard since i have 0 support from my mentor in school so i kinda have to teach myself, thats how i found this site and ended up here,

So start with the code you posted in post #1 of this topic and implement the changes I suggested. Ignore the link to the topic about hysteresis until you’re ready to implement that feature.

Pete.

#define BLYNK_PRINT SerialUSB
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>
#include <DHT.h>
  

char auth[] = "o";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "o";
char pass[] = "o";
float temp_setpoint; 
#define DHTPIN 2
 

#define DHTTYPE DHT11     
DHT dht(2,11);
BlynkTimer timer;
 

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Blynk.virtualWrite(V5, temperature);
  Blynk.virtualWrite(V6, humidity);
}
 
void setup()
{
  pinMode(14, OUTPUT);
  // Debug console
  SerialUSB.begin(9600);
 
  
 
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
 
  dht.begin();
 
  
  timer.setInterval(1000L, sendSensor);
}
if (temperature >= temp_setpoint)
{
  digitalWrite(3,HIGH);

}
else
{digitalWrite(3,LOW);
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1); 
}

BLYNK_WRITE(V1){  
  int temp_setpoint = param.asInt();  
  analogWrite(14  value);  
}
 
void loop()
{
  
  
  
  Blynk.run();
  
 
  timer.run();
}

Does this look ok? I tried adding the IF function like you suggested. And the ```
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V1);
}

I went back to the start and read trought everything yet again!
I think it might be ok.

Screenshot_1 :roll_eyes: or not

You haven’t put the if statement in the correct place.

You are re-declaring your global floating point variable called temp_setpoint as a local integer variable in BLYNK_WRITE(V1)…

This line makes no sense…

Your if statement is controlling GPIO3, which has no pinMode statement in void setup.

You’ve decided to mix t and h and temperature and ‘humidity` as variable names.

I would suggest that you go back to the code you had in post #1 and read the comments that I made in post #4 very carefully then implement each of these changes one at a time.

I’ve taken the time to explain things to you in great detail, at least take the time to read this information and use it for your benefit.

Pete.

Im sorry, i actually did read everything multiple times, im trying my best to understand. Ill post my steps one by one as i implement things.

  1. I declared ‘’ value’’(changed the name to humidty_setpoint) at the top with the SSID and PASS
float humidty_setpoint;

My BLYNK_WRITE(V1) looks like this now

BLYNK_WRITE(V1){  
  int humidty_setpoint = param.asInt();  
  analogWrite(14  value);

2, I tried adding the IF function at the sendsensor function.
It looks like this now.

timer.setInterval(1000L, sendSensor) if (h >humidty_setpoint)
  {digitalWrite(14,HIGH) } else {digitalWrite(14,LOW);
}

3.Okay this one had me a bit puzzled, I didnt notice the drop down arrow on thie BLYNK_WRITE post you shared up untill now, so i just gave that a read too. I ended up adding this

void setup()
{
 pinMode(3, OUTPUT);
}
BLYNK_WRITE(V1)

{ if(param.asInt() == 1)
{
  digitalWrite(3,HIGH);
}
else
{digitalWrite(3,LOW);
}
}

Now, I know that thats for a button and not a slider, but if its going to help me understand this better i will use a button as my heating controller for now and once i have a better understanding of this (or if the code i just sent ends up being ok) i will change back to my slider option and see if i can do that next , (i dont even know if i managed to implement this code correctly :crossed_fingers:

Ok so now that i’ve listed all the steps i did i will post the whole code in case you need to see the rest of it. I hope i managed to pull it off this time. Sorry in advance if i messed it up, i am really trying.

#define BLYNK_PRINT SerialUSB
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>
#include <DHT.h>
 

char auth[] = "o";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "o";
char pass[] = "o";
float humidty_setpoint;
#define DHTPIN 2
 

#define DHTTYPE DHT11     
DHT dht(2,11);
BlynkTimer timer;
 

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h);
}
 
void setup()
{
 pinMode(3, OUTPUT);
}
BLYNK_WRITE(V1)

{ if(param.asInt() == 1)
{
  digitalWrite(3,HIGH);
}
else
{digitalWrite(3,LOW);
}
}

{
  pinMode(14, OUTPUT);
  // Debug console
  SerialUSB.begin(9600);
 
  
 
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
 
  dht.begin();
 
  
  timer.setInterval(1000L, sendSensor) if (h >humidty_setpoint)
  {digitalWrite(14,HIGH) } else {digitalWrite(14,LOW);
}

BLYNK_WRITE(V1){  
  int humidty_setpoint = param.asInt();  
  analogWrite(14  value);
}
 
void loop()
{
  
  
  
  Blynk.run();
  
 
  timer.run();
}

Which is strange, because I said …

That isn’t the sendSensor() function, it’s the timer definition in your void setup, which causes the sendSensor() function to be called every 1 second.
Your sendSensor function is the bit of code that starts with…

BTW, trying to read a DHT11 sensor once every second won’t work very well. You should increase the timer to 5 seconds (5000ms) at least.

Everything you’ve written in (3) is irrelevant and will not help you at all.
GO BACK TO THE CODE YOU POSTED IN #1 and stop doing stupid stuff!

Pete.

Ok. back to square 1,
BLYNK_WRITE(V1) looks like you said it would. i renamed the ‘‘value’’ to humidity_setpoint.
I also changed the read time for DHT11 to 5 seconds. Ill work on the IF function right now

image

is that ok now?

Its no longer giving me any errors so i hope it is.

Don’t you think that the better approach would be…

  • Take a temperature and humidity reading
  • Check whether that temperature/humidity reading is higher or lower than your set point
  • Take the appropriate action

What you are doing is performing this check before taking the sensor readings.

Pete.

No? Nothing was deleted. I just took a picture of the part i edited. heres the whole code.

#define BLYNK_PRINT SerialUSB
 
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>
#include <DHT.h>
 

char auth[] = "o";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "o";
char pass[] = "o";
float humidty_setpoint,h,t;
#define DHTPIN 2
 

#define DHTTYPE DHT11     
DHT dht(2,11);
BlynkTimer timer;
 

void sendSensor()

  
{
 
float h = dht.readHumidity(5);
  float t = dht.readTemperature(5); 
   
   
   
   if (h > humidty_setpoint)
{digitalWrite(14,HIGH); } 
else 
{digitalWrite(14,LOW);}
  
  
  
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h);
}
 
void setup()
{
  pinMode(14, OUTPUT);
  // Debug console
  SerialUSB.begin(9600);
 
  
 
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
 
  dht.begin();
 
  
  timer.setInterval(1000L, sendSensor);
}

BLYNK_WRITE(V1){ 
  int humidty_setpoint = param.asInt(); 
}
 
void loop()
{
  
  
  
  Blynk.run();
  
 
  timer.run();
}

I have to edit my old post because i cant make any new replys.

Yes, except your original sketch had some code to reject invalid readings, which you now appear to have deleted. Why have you done this?

Pete.

1 Like