Hello everbody,
i am trying to get this code for arduino with delays changed in sonething that works without delay but with timer… timer.interval/timerTimeout…
/* Soil Mositure Basic Example
This sketch was written by SparkFun Electronics
Joel Bartlett
August 31, 2015
Basic skecth to print out soil moisture values to the Serial Monitor
Released under the MIT License(http://opensource.org/licenses/MIT)
*/
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 setup()
{
Serial.begin(9600); // open serial over USB
pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor
}
void loop()
{
Serial.print("Soil Moisture = ");
//get soil moisture value from the function below and print it
Serial.println(readSoil());
//This 1 second timefrme is used so you can test the sensor and see it change in real-time.
//For in-plant applications, you will want to take readings much less frequently.
delay(1000);//take a reading every second
}
//This is a function used to get the soil moisture content
int readSoil()
{
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
}
my setup is a "UNO+WiFi R3 ATmega328P+ESP8266 (32Mb memory), USB-TTL CH340G"from aliexpress
and a " Arduino Automatic Smart Plant Watering Kit" not the version on aliexpress, without the oled screen…
My code so far is…
// UnoCorn add
//#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;
char auth[] = "***********************"; // BlynkServer WasRechts account
char ssid[] = "###########";
char pass[] = "****************";
char server[] = "000.000.0.00";
int port = 0000;
//UnoCorn add
//Serial 1 naar Serial zonder 1
#define EspSerial Serial
// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(0, 1); // RX, TX
// Your ESP8266 baud rate:
//UnoCorn add
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
//Smart Plant add
// set water relays and water pump
int Pump = 7; // Pomp 12volt
int Valve1 = 3; // Valve 1
int Valve2 = 4; // Valve 2
int Valve3 = 5; // Valve 3
int Valve4 = 6; // Valve 4
// Vpin sAFE? add
int v1update = 0;
int lastv1update = 0;
int buttonState = 0;
// VPIN NEW ADD
// This function will be called every time Slider or?and?Button? Widget
// in Blynk app writes values to the
// Virtual Pin 1
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
if (pinValue == 1) {
v1update = 1;
}
if (pinValue == 0) {
v1update = 0;
}
if (v1update != lastv1update) {
if (v1update == 1) {
// vAlve1 & pomp AAN
digitalWrite(Pump, HIGH); // Turn Pump ON.
digitalWrite(Valve1, HIGH); // Turn Valve 1 ON.
}
if (v1update == 0) {
// vAlve1 & pomp UIT
digitalWrite(Pump, LOW); // Turn Pump OFF.
digitalWrite(Valve1, LOW); // Turn Valve 1 OFF.
}
}
lastv1update = v1update;
}
// Virtual Pin 2
BLYNK_WRITE(V2)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
if (pinValue == 1) {
v1update = 1;
}
if (pinValue == 0) {
v1update = 0;
}
if (v1update != lastv1update) {
if (v1update == 1) {
// vAlve2 & pomp AAN
digitalWrite(Pump, HIGH); // Turn Pump ON.
digitalWrite(Valve2, HIGH); // Turn Valve 2 ON
}
if (v1update == 0) {
// vAlve2 & pomp UIT
digitalWrite(Pump, LOW); // Turn Pump OFF.
digitalWrite(Valve2, LOW); // Turn Valve 2 OFF.
}
}
lastv1update = v1update;
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
if (pinValue == 1) {
v1update = 1;
}
if (pinValue == 0) {
v1update = 0;
}
if (v1update != lastv1update) {
if (v1update == 1) {
// vAlve3 & pomp AAN
digitalWrite(Pump, HIGH); // Turn Pump ON.
digitalWrite(Valve3, HIGH); // Turn Valve 3 ON.
}
if (v1update == 0) {
// vAlve3 & pomp UIT
digitalWrite(Pump, LOW); // Turn Pump OFF.
digitalWrite(Valve3, LOW); // Turn Valve 3 OFF.
}
}
lastv1update = v1update;
}
BLYNK_WRITE(V4)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
if (pinValue == 1) {
v1update = 1;
}
if (pinValue == 0) {
v1update = 0;
}
if (v1update != lastv1update) {
if (v1update == 1) {
// vAlve4 & pomp AAN
digitalWrite(Pump, HIGH); // Turn Pump ON.
digitalWrite(Valve4, HIGH); // Turn Valve 4 ON.
}
if (v1update == 0) {
// vAlve4 & pomp UIT
digitalWrite(Pump, LOW); // Turn Pump OFF.
digitalWrite(Valve4, LOW); // Turn Valve 4 OFF.
}
}
lastv1update = v1update;
}
void setup()
{
// Debug console
//UnoCorn add
//Serial.begin(9600);
//delay(10);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
//Smart PLant add
pinMode(Pump, OUTPUT); // Pump als output
pinMode(Valve1, OUTPUT); // Valve 1 als output
pinMode(Valve2, OUTPUT); // Valve 2 als output
pinMode(Valve3, OUTPUT); // Valve 3 als output
pinMode(Valve4, OUTPUT); // Valve 4 als
Blynk.begin(auth, wifi, ssid, pass, server, port );
}
void loop()
{
Blynk.run();
timer.run();
}
Does anybody have an idea how to start? I got the sensor working on Uno with the above code… I just dont know where to begin with the read of analog with timer and not delays…
this BlynkTimer example sounds logical and then… euhm??
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
who would have a smart idea how to? or how best to?
kind regards,