i just cant get my head around this part of the code
{
digitalWrite(soilPower, HIGH);//turn D7 "On"
delay(10);//wait 10 milliseconds
val = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
return val;//send current moisture value
}
i want to check if i can rewrite this without the delay… maybe make it a void?
I don’t think a delay of 10 milliseconds will give you any problems. Especially if you are only reading the sensor every couple of seconds.
The other option is to use a timeout
digitalWrite(soilPower, HIGH);//turn D7 "On"
timer.setTimeout(10L, []() { // Lambda Timer Function
val = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
return val;//send current moisture value
}); // END
digitalWrite(soilPower, HIGH);//turn D7 "On"
timer.setTimeout(10L, []() { // Lambda Timer Function
val = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
return val;//send current moisture value
}); // END
i get this error?
exit status 1
expected ‘)’ before ‘}’ token
or when i try ) before } and then ;
i get this error?
exit status 1
expected primary-expression before ‘)’ token
invalid user-defined conversion from ‘myTimerEvent()::__lambda0’ to ‘timer_callback {aka void (*)()}’ [-fpermissive]
What?
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer; // Announcing the timer
char auth[] = "00000000000000000"; // Blynk_Server "hostname.try"
char ssid[] = "##########";
char pass[] = "**************";
// Soil Moisture add
int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power
//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.
void myTimerEvent()
{
digitalWrite(soilPower, HIGH);//turn D7 "On"
timer.setTimeout(10L, []() { // Lambda Timer Function
val = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
return val;//send current moisture value
}); // END
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the
//hostname add
Serial.printf("Default hostname: %s\n", WiFi.hostname().c_str());
WiFi.hostname("Moisture.try");
Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());
Blynk.begin(auth, ssid, pass, IPAddress(***, ***, ***, ***), ****);
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run(); // running timer every second
}
You need a closing bracket on the myTimerEvent function.
Plus I would probably ditch the return, and just use the global variable.
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer; // Announcing the timer
char auth[] = "00000000000000000"; // Blynk_Server "hostname.try"
char ssid[] = "##########";
char pass[] = "**************";
// Soil Moisture add
int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power
//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.
void myTimerEvent()
{
digitalWrite(soilPower, HIGH);//turn D7 "On"
timer.setTimeout(10L, []() { // Lambda Timer Function
val = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
}); // END
} //you are missing this
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the
//hostname add
Serial.printf("Default hostname: %s\n", WiFi.hostname().c_str());
WiFi.hostname("Moisture.try");
Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());
Blynk.begin(auth, ssid, pass, IPAddress(***, ***, ***, ***), ****);
timer.setInterval(1000L, myTimerEvent); // running timer every second
}
void loop()
{
Blynk.run();
timer.run(); // running timer
}