Blynk Slider value Set

Hi,
rewriting some code to work with Blynk without delays. Having issue with a slider being able to set a value and be used in the below code:

int moistset = 350 sets the level initially.
The BLYNKWRITE works and sends the moistset to the serial monitor as a value
in the void soilsensor() section it does not pick up the moistset value in the BLYNKWRITE.
I’m new to this, what am i missing to get the slider value of moistset to be used in the void soilsensor section?

//BlynK
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
char auth[] = "xxxxxxxxxxxxxxxx";
#define W5100_CS  10
#define SDCARD_CS 4

//temp sensor
#include <DHT.h>
#define DHTPIN 8
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

BlynkTimer timer;

int soilMoistureValue = 0;
int soilsens = A0;
int water = 7;
int hotday = 20;
int moistset = 350;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void soilsensor()
{
//  program2 monisture sensorrelay
  soilMoistureValue = analogRead(soilsens);  //put Sensor insert into soil
if(soilMoistureValue < moistset)
{
 Serial.println("Wet");
  digitalWrite(water, LOW);
  Blynk.virtualWrite(V7, "Wet");
}
else
{
  Serial.println("Dry");
  Blynk.virtualWrite(V7, "Dry"); 
 if (dht.readTemperature()>= hotday)
  {digitalWrite(water, HIGH);
 }
 else
   digitalWrite(water, LOW);
 
}
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
int moistset  = pinValue;
Serial.println(moistset);
 
}

void setup() {
   Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
    Blynk.begin(auth);
    pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  dht.begin();
  pinMode(water, OUTPUT);
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, soilsensor);

}

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

This is your bug, you are redifining moistset so it’s no longer a global variable available to the rest of the sketch. Just use:

moistset = pinValue;

or skip that line and make the line above:

moistset = param.asInt(); // assigning incoming value from pin V1 to a variable

3 Likes

You Sir,

Are a CHAMPION!

Probably pretty simple to you, but to a noob like me you just saw something I have struggled 2 days with.

thanks again.

1 Like

We all start off the same way… but much reading, and lots of trial and error helps :wink:

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

1 Like

Sir, can you please help me? I am facing so much problem with my project.
The BLYNK_WRITE syntax is not working in my case.

void loop()
{
  Blynk.run();

BLYNK_WRITE(V0){
  lightbtnState = param.asInt();  // Virtual lightbtnState will be 0 or 1 
    if(lightbtnState == 1){
      digitalWrite(lightPin, HIGH);   // Digital pin 3
      }
     else{
      digitalWrite(lightPin, LOW);
  }
}


BLYNK_WRITE(V1){
  fanbtnState = param.asInt();  // Virtual lightbtnState will be 0 or 1 
    if(fanbtnState == 1){
      digitalWrite(fanPin, HIGH);    // Digital pin 5
      }
     else{
      digitalWrite(fanPin, LOW);
  }
}

BLYNK_WRITE(V2){          // Slider for Light
  lightsliderValue = param.asInt();  // sliderValue will be between 0 and 100, representing percentages
  
}

BLYNK_WRITE(V3){          // Slider for Fan
  fansliderValue = param.asInt();  // sliderValue will be between 0 and 100
}

  
}

Can you please explain how the slider will work?? The light and fan pins ae D3 and D5 respectively. Then how the virtual pin v2 and v3 change the slider value??

@daspriyanka from your grotesquely formatted sketch it’s hard for even a seasoned pro to see what’s going off.

  1. Learn to post formatted sketches to forums as you will get more assistance that way.

  2. Quick glance suggests you have BLYNK_WRITE() in loop() which is an absolute no, no. They are functions in their own right, see PUSH_DATA and all the other examples for syntax.

I can help format your posted code this time…

Blynk - FTFC

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);


int lightbtnState;
int fanbtnState;

int lightsliderValue;
int fansliderValue;

const int lightPin= 3;  // Digital pin, pwm
const int fanPin= 5;


void setup()
{
  // Debug console
  Serial.begin(9600);

  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  pinMode(lightPin, OUTPUT);
  pinMode(fanPin, OUTPUT);

  BLYNK_CONNECTED() {
    Blynk.syncAll(); //restores all the Widget’s values based on the last saved values on the server. All analog and digital pin states will be restored
  }
}
  
}

void loop()
{
  Blynk.run();

BLYNK_WRITE(V0){
  lightbtnState = param.asInt();  // Virtual lightbtnState will be 0 or 1 
    if(lightbtnState == 1){
      digitalWrite(lightPin, HIGH);   // Digital pin 3
      }
     else{
      digitalWrite(lightPin, LOW);
  }
}


BLYNK_WRITE(V1){
  fanbtnState = param.asInt();  // Virtual lightbtnState will be 0 or 1 
    if(fanbtnState == 1){
      digitalWrite(fanPin, HIGH);    // Digital pin 5
      }
     else{
      digitalWrite(fanPin, LOW);
  }
}

BLYNK_WRITE(V2){          // Slider for Light
  lightsliderValue = param.asInt();  // sliderValue will be between 0 and 100, representing percentages
  
}

BLYNK_WRITE(V3){          // Slider for Fan
  fansliderValue = param.asInt();  // sliderValue will be between 0 and 100
}

  
}

Sir this is the whole code…can u make me understood how the slider value will work?..because there is no connection with v2 and v3 pins with the light and fan pins…

Sorry, No… you can’t even follow basic directions in formatting your posted code, so how can we help with more?

@daspriyanka Seriously? I have removed all the redundant posts… now please stop dumping code here and go back and edit your post so that it shows properly.

can you please tell me you are talking about which format? and how to edit it in your way?

Look at the image I provided… when you paste your code in, put the three back-tick characters and the letters cpp in the beginning and three more back-ticks at the end… simple.

Blynk - FTFC

These instructions are also mentioned in the Welcome Thread… which you have read… correct? :wink:

EDIT - and in the end I had to do it for you again :frowning:

1 Like

See the post I just wrote for you at Blynk for Beginners and help with your project - #46 by Costas

Where?

Click on the green link above :wink: