I want to change the pictures (Widget Images) to change the value of the virtual pin eg 0 - 100 (V1). I do not know how to do it and it is very simple indeed.
Take a look at this topic:
Pete.
And an easier code?.
BLYNK_WRITE(V1) { // Slider set to 0-209 and Send on Release OFF (you will want a Local Server for this)
Blynk.virtualWrite(V0, param.asInt()); // Image Widget with 209 loaded images
}
Well, it is actually very simple, all it does is take a slider value and send the relevant image ID to the Image Widget… that would work the same with 2 images as it does with 209
You still need to have the image URLs already “loaded” into the Widget
I do not know how to do it I have this code. Is it possible for someone to write it to me?
On digitalPin it works but on virtualPin I do not know how to do it.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int pin = D1;
char auth[] = "xxxxxxxxx";
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
BLYNK_WRITE(V1) { // Button widget on V4
if (param.asInt() == 1) {
Blynk.virtualWrite(V0, 1); // Show image 1 - Widget on V3
} else {
Blynk.virtualWrite(V0, 2); // Show image 2 - Widget on V3
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
if(digitalRead(pin) == 1){
Blynk.virtualWrite(V0, 2);
}
else{
Blynk.virtualWrite(V0, 1);
}
}
First:
Second:
Third, study examples more:
https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FVirtualPinWrite
I did. Thank you for your help
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxxx";
char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V1) { // Button widget on V1
int pinValue = param.asInt();
if ( pinValue == 1) {
Blynk.virtualWrite(V0, 1); // Show image 1 - Widget on V0
} else if ( pinValue == 0) {
Blynk.virtualWrite(V0, 2); // Show image 2 - Widget on V0
}
}
How to do it to read from the ultrasound sensor so that the images change. The slider works for me. Code as above.
What is wrong?
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define TRIGGERPIN D7
#define ECHOPIN D6
char auth[] = "xxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
WidgetLCD lcd(V1);
BLYNK_WRITE(distance) { // Button widget on V1
int distance = param.asInt();
if ( distance <= 100) {
Blynk.virtualWrite(V0, 0); // Show image 1 - Widget on V0
}
if (( distance >= 101) && ( distance <= 190)) {
Blynk.virtualWrite(V0, 2); // Show image 2 - Widget on V0
}
if (( distance >= 200) && ( distance <= 300)) {
Blynk.virtualWrite(V0, 1); // Show image 2 - Widget on V0
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(TRIGGERPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
lcd.clear();
lcd.print(0, 0, "Distance in CM"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
long duration, distance;
digitalWrite(TRIGGERPIN, LOW);
delayMicroseconds(1);
digitalWrite(TRIGGERPIN, HIGH);
delayMicroseconds(12);
digitalWrite(TRIGGERPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println("Cm");
lcd.print(7, 1, distance);
Blynk.run();
delay(1000);
}
You’ve ignored the advice provided earlier about keeping your void loop clean.
Until you do that, there doesn’t seem much point in explaining the principles of variable scope.
Pete.
I think it’s good now.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
int cm ;
// Pins
const int TRIG_PIN = D7;
const int ECHO_PIN = D6;
// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;
char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxx";
void setup() {
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
Blynk.begin(auth, ssid, pass);
Serial.begin(9600);
timer.setInterval(1000L, czujnik);
}
void loop() {
Blynk.run();
timer.run();
}
void czujnik() {
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;
float inches;
// Hold the trigger pin high for at least 10 us
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Wait for pulse on echo pin
while ( digitalRead(ECHO_PIN) == 0 );
// Measure how long the echo pin was held high (pulse width)
// Note: the micros() counter will overflow after ~70 min
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;
// Calculate distance in centimeters and inches. The constants
// are found in the datasheet, and calculated from the assumed speed
//of sound in air at sea level (~340 m/s).
cm = pulse_width / 58.0;
inches = pulse_width / 148.0;
// Print out results
if ( pulse_width > MAX_DIST ) {
Serial.println("Out of range");
} else {
Serial.print(cm);
Serial.print(" cm ");
Serial.print(inches);
Serial.println(" in");
Blynk.virtualWrite(1, cm); // 1 Pin wirtualny
if ( cm <= 100) {
Blynk.virtualWrite(V0, 0); // Show image 1 - Widget on V0
}
if (( cm >= 101) && ( cm <= 190)) {
Blynk.virtualWrite(V0, 2); // Show image 2 - Widget on V0
}
if (( cm >= 200) && ( cm <= 300)) {
Blynk.virtualWrite(V0, 1); // Show image 2 - Widget on V0
}
}
// Wait at least 60ms before next measurement
delay(60);
}
I made a new - void photo() { - only now does not read me data from “cm”
what am I doing wrong ?
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
int cm ;
const int TRIG_PIN = D7;
const int ECHO_PIN = D6;
// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;
char auth[] = "xxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxxx";
void setup() {
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
Blynk.begin(auth, ssid, pass);
Serial.begin(9600);
timer.setInterval(800L, photo);
timer.setInterval(1000L, sensor);
}
void loop() {
Blynk.run();
timer.run();
}
void sensor() {
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
while ( digitalRead(ECHO_PIN) == 0 );
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;
cm = pulse_width / 58.0;
if ( pulse_width > MAX_DIST ) {
Serial.println("Out of range");
}
else {
Serial.print(cm);
Serial.print(" cm ");
}
}
void photo() {
Blynk.virtualWrite(1, cm); // 1 Pin wirtualny
if ( cm <= 5) {
Blynk.virtualWrite(V0, 1); // Show image 1 - Widget on V0
}
if (( cm >= 6) && ( cm <= 8)) {
Blynk.virtualWrite(V0, 2); // Show image 2 - Widget on V0
}
if (( cm >= 9) && ( cm <= 12)) {
Blynk.virtualWrite(V0, 3); // Show image 3 - Widget on V0
}
if (( cm >= 13) && ( cm <= 16)) {
Blynk.virtualWrite(V0, 4); // Show image 4 - Widget on V0
}
if (( cm >= 17) && ( cm <= 19)) {
Blynk.virtualWrite(V0, 5); // Show image 5 - Widget on V0
}
if (( cm >= 21) && ( cm <= 23)) {
Blynk.virtualWrite(V0, 6); // Show image 6 - Widget on V0
}
if (( cm >= 24) && ( cm <= 26)) {
Blynk.virtualWrite(V0, 7); // Show image 7 - Widget on V0
}
if (( cm >= 27) && ( cm <= 29)) {
Blynk.virtualWrite(V0, 8); // Show image 8 - Widget on V0
}
if (( cm >= 30) && ( cm < 302)) {
Blynk.virtualWrite(V0, 9); // Show image 9 - Widget on V0
}
delay(1000);
}
Try moving:
Blynk.virtualWrite(1, cm);
From void photo to void sensor (after serial print like before)
And also remove:
delay(1000);
From void photo because it is not needed
I did it but the pictures do not change.
Ah I think it maybe because you declare cm as int globally but then declare cm as float in sensor function.
Change: int cm; -to-> float cm;
And then remove: float cm; from your void sensor function.
Works great thanks.
I am entering the code that works.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
float cm ;
const int TRIG_PIN = D7;
const int ECHO_PIN = D6;
// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;
char auth[] = "xxxxxxxxx";
char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxx";
void setup() {
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
Blynk.begin(auth, ssid, pass);
Serial.begin(9600);
timer.setInterval(800L, photo);
timer.setInterval(1000L, sensor);
}
void loop() {
Blynk.run();
timer.run();
}
void sensor() {
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
while ( digitalRead(ECHO_PIN) == 0 );
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;
cm = pulse_width / 58.0;
if ( pulse_width > MAX_DIST ) {
Serial.println("Out of range");
}
else {
Serial.print(cm);
Serial.print(" cm ");
}
}
void photo() {
Blynk.virtualWrite(1, cm); // 1 Pin wirtualny
if ( cm <= 5) {
Blynk.virtualWrite(V0, 1); // Show image 1 - Widget on V0
}
if (( cm >= 6) && ( cm <= 8)) {
Blynk.virtualWrite(V0, 2); // Show image 2 - Widget on V0
}
if (( cm >= 9) && ( cm <= 12)) {
Blynk.virtualWrite(V0, 3); // Show image 3 - Widget on V0
}
if (( cm >= 13) && ( cm <= 16)) {
Blynk.virtualWrite(V0, 4); // Show image 4 - Widget on V0
}
if (( cm >= 17) && ( cm <= 19)) {
Blynk.virtualWrite(V0, 5); // Show image 5 - Widget on V0
}
if (( cm >= 21) && ( cm <= 23)) {
Blynk.virtualWrite(V0, 6); // Show image 6 - Widget on V0
}
if (( cm >= 24) && ( cm <= 26)) {
Blynk.virtualWrite(V0, 7); // Show image 7 - Widget on V0
}
if (( cm >= 27) && ( cm <= 29)) {
Blynk.virtualWrite(V0, 8); // Show image 8 - Widget on V0
}
if (( cm >= 30) && ( cm < 302)) {
Blynk.virtualWrite(V0, 9); // Show image 9 - Widget on V0
}
}