Hi I am using Arduino nano to make on-line flower watering system.
When I run my code with out Blynk connecting to the server the millis() works fine! When I run it with the Blynk everyting slows down! I don´t want to use delay() what can I do to have accurate time?
Here is my code.
bool ResetTime = false;
int SensorPin_1 = A1; // Pinni fyrir rakaskynjara er A1
float sensorValue_1 = 0; //Fasti fyrir rakaskynjara
int Snuningstakki = A0; //Pinni fyrir stillanlegt viðnám
int lesa_takki_fasti; //Fasti fyrir lestur á snúningstakka
int skrifa_takki_fasti; //Fasti fyrir srif á snúningstakka
int Mosfet_pin = 5; // Pinni 5 til að stýra mosfet (slökkva skynjara milli lesturs)
int Relay_pin = 8; // Pinni til að stýra relay
int Relay_state =LOW; //Fasti fyrir millis()vökvun
int Float_sensor = 7; //Flot skynjari er skráður á pinna 7.
int Float_fasti = 0; //Fasti fyrir flot skynjara
const int LED_RED = 2; //rauður
const int LED_BLUE = 4; //blár
const int LED_GREEN = 4; //grænn
int ledState = LOW; // Fasti fyrir blikk á LED peru
unsigned long previousTime_2 = 0; // Fasti til að telja tíma fyrir peru
const long Led_blink_timi = 400; // TÍMI BLIKKS ÞEGAR FORÐABÚR ER TÓMT
const unsigned long Sencor_lestur = 5000;
unsigned long previousTime_1 = 0;
const unsigned long Vokvun_lestur = 21600000;
unsigned long previousTime_3 = 0;
int Deala_vatni = 2000;
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
char auth[] = "LRCJewtdQZG7FfTXYL2fWpuQYGbPbQ4P"; // Auth Token kemur í email þegar búið er að búa til verkefni Blynk appinu.
//char ssid[] = "XXXXXX"; // ÞRÁÐLAUST NET HEITI
//char pass[] = "XXXXXXX"; // ÞRÁÐLAUST NET LEYNIORÐ
WidgetLCD lcd(V2);
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(Relay_pin, OUTPUT);
pinMode(Mosfet_pin, OUTPUT);
pinMode(SensorPin_1, INPUT);
pinMode (Snuningstakki, INPUT);
pinMode(Float_sensor,INPUT_PULLUP);
pinMode (LED_BLUE,OUTPUT);
pinMode (LED_RED,OUTPUT);
pinMode (LED_GREEN,OUTPUT);
digitalWrite(Relay_pin, LOW);
digitalWrite(Mosfet_pin, HIGH);
}
void loop() {
Blynk.run(); //Keyra upp Blynk forrit
unsigned long currentTime = millis(); //Keyra upp millisekúndur og skýra "currentTime" TÍMAKLUKKA FORRITSINS.
lesa_takki_fasti = analogRead (Snuningstakki);
skrifa_takki_fasti = (5000./1023.) * lesa_takki_fasti; // Lesa pot pinna og reikna yfir í gildi frá 0-1023 yfir í 255 analog Volt (0-5).
Serial.print ("Millisec ");
Serial.println (currentTime);
if (currentTime - previousTime_3 >= Vokvun_lestur) {
while (millis() < currentTime + Deala_vatni) {
digitalWrite(Relay_pin, HIGH);
}
previousTime_3 = currentTime;
}
if (currentTime - previousTime_1 >= Sencor_lestur) {
digitalWrite(Mosfet_pin, LOW);
digitalWrite (LED_GREEN, HIGH);
sensorValue_1 = ((analogRead(SensorPin_1)/1023.0)*500);
Float_fasti = digitalRead (Float_sensor);
Serial.print("Val_1 ");
Serial.println(sensorValue_1);
Serial.println(" ");
// delay(20);
previousTime_1 = currentTime;
}
digitalWrite(Mosfet_pin, HIGH);
digitalWrite (LED_GREEN, LOW);
//delay(20);
/*
if (sensorValue_1 >= 250)
{
digitalWrite (Relay_pin, HIGH);
delay (1000);
digitalWrite (Relay_pin, LOW);
delay (skrifa_takki_fasti);
}
else
{
digitalWrite (Relay_pin, LOW);
}
*/
// Fylgjast með flotskynjara í vatnsforðabúinu ________________________________________________________________________
if (Float_fasti !=1)
{
if (currentTime - previousTime_2 >= Led_blink_timi) {
// save the last time you blinked the LED
previousTime_2 = currentTime;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(LED_RED, ledState);
}
else
{
digitalWrite (LED_RED, LOW);
}
}
Blynk.virtualWrite(V1,skrifa_takki_fasti);
if (Float_fasti !=1)
{
lcd.clear();
lcd.print(1, 0, "VATN Á TANKIR");
}
if (Float_fasti !=0);
{
lcd.clear();
lcd.print(0, 0, "TANKUR AÐ TÆMAST");
}
if (millis() > 3960000000)
{
ResetTime = true;
}
if (millis() < 400 && ResetTime)
{
previousTime_1 = 0;
previousTime_2 = 0;
previousTime_3 = 0;
ResetTime = false;
}
}