Hi, while working on my project I noticed, that even when using millis function code inside it is skipped every 3-4 seconds. I tried to delete many things and nothing helped. There's pH monitoring, temperature, water flow, distance measuring... etc. I'm using Arduino Mega
#include <NewPing.h>
#include <Wire.h>
#include <SPI.h>
#include <TimerFreeTone.h>
#include "SdFat.h"
#include "Sodaq_DS3231.h"
#include "HX711.h"
#include <OneWire.h>
#include <Nextion.h>
#define Ka 0.0039083
#define Kb -5.775E-07
#define proud 1679.5
#define spodni 3346.0
#define horni 6016.0
#define privod 0.4
const int pinCidlaDS = 2;
OneWire oneWireDS(pinCidlaDS);
SdFat SD;
#define SD_CS_PIN SS
File Data;
HX711 scale;
NewPing sonar(12, 12, 200);
float spodni_a, horni_a;
int volt, hladinavody = 6;
byte sensorInterrupt = 0;
byte sensorPin = 2;
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
float celkem_litru;
unsigned long oldTime,oldTime1;
char buffer[100] = {0};
uint32_t number2 = 0;
NexButton b3 = NexButton(2, 8, "b3");
NexButton b4 = NexButton(2, 10, "b4");
NexSlider h0 = NexSlider(5, 14, "h0");
NexTouch *nex_listen_list[] =
{
&b3,
&b4,
&h0,
NULL
};
void b3PushCallback(void *ptr)
{ digitalWrite(23, HIGH);
digitalWrite(22, LOW); //
delay(70000);
digitalWrite(22, HIGH);
}
void b4PushCallback(void *ptr)
{
if(digitalRead(23)== HIGH)
{
digitalWrite(23,LOW);
}else{digitalWrite(23,HIGH);}
}
void h0PopCallback(void *ptr) // Release event for slider
{
}
void setup()
{
Serial.begin(115200);
pinMode(5,OUTPUT);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
Wire.begin();
rtc.begin();
SD.begin();
Data = SD.open("Data.txt", FILE_WRITE);
scale.begin(A1, A2, 32);
b3.attachPush(b3PushCallback);
b4.attachPush(b4PushCallback);
h0.attachPop(h0PopCallback);
}
void loop() {
DateTime now = rtc.now();
scale.set_gain(32);
float proud_a = scale.read_average(1) / 1000.0;
int prumer = int(proud_a);
float zbytek = proud_a - float(prumer);
if (zbytek < 0.5) {proud_a = float(prumer);}
else {proud_a = float(prumer) + 0.5;}
float podil = proud_a / proud;
scale.set_gain(64);
spodni_a = spodni * podil;
horni_a = horni * podil;
prumer = int(scale.read_average(1) / 1000.0);
float x = float(prumer);
float odpor = (x - spodni_a) * (180.0 - 100.0) / (horni_a - spodni_a) + 100.0 ;
float teplota = 1.7 + (( sqrt(Kb * odpor - 100.0 * Kb + 25.0 * pow(Ka, 2)) - 5.0 * Ka) / (10.0 * Kb)) - privod;
if ((millis() - oldTime) > 1000) /// Zobrazení a uložení dat na SD kartu každou vteřinu
{
oldTime = millis();
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
celkem_litru = totalMilliLitres / 1000;
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
///pH
volt = analogRead(0);
int measure = analogRead(0);
double voltage = 4.9 / 1024.0 * measure;
float Po = 7 + ((593 - measure) / 35.67);
///pH
///Hladina vody
hladinavody = sonar.ping_cm();
///Hladina vody
Serial.print("monitoring.pH.txt=\" " + String(Po) + "\"");
NextionEndCommand();
Serial.print("monitoring.teplota.txt=\" " + String(teplota) + " 0C\"");
NextionEndCommand();
if (hladinavody < 10) {
Serial.print("monitoring.t6.txt=\" " + String(hladinavody) + ".00 cm\"");
}
else {
Serial.print("monitoring.t6.txt=\" " + String(hladinavody) + ".00 cm\"");
}
NextionEndCommand();
Serial.print("monitoring.kondu.txt=\"---.-- uS.m-1\"");
NextionEndCommand();
if (celkem_litru < 10) {
Serial.print("monitoring.c_l.txt=\" " + String(celkem_litru) + " l\"");
}
else if (celkem_litru < 100) {
Serial.print("monitoring.c_l.txt=\" " + String(celkem_litru) + " l\"");
}
else {
Serial.print("monitoring.c_l.txt=\"" + String(celkem_litru) + " l\"");
}
NextionEndCommand();
Serial.print("monitoring.prutok.txt=\"" + String(flowMilliLitres) + ".00 ml.s-1\"");
NextionEndCommand();
if (now.second() < 10 && now.minute() < 10 ) {
Serial.print("Time.txt=\"" + String(now.date()) + "/" + String(now.month()) + "/" + String(now.year()) + " " + String(now.hour()) + ":0" + String(now.minute()) + ":0" + String(now.second()) + "\"");
} else if (now.second() < 10 && now.minute() > 10)
{
Serial.print("Time.txt=\"" + String(now.date()) + "/" + String(now.month()) + "/" + String(now.year()) + " " + String(now.hour()) + ":" + String(now.minute()) + ":0" + String(now.second()) + "\"");
}
else if (now.second() > 10 && now.minute() < 10)
{
Serial.print("Time.txt=\"" + String(now.date()) + "/" + String(now.month()) + "/" + String(now.year()) + " " + String(now.hour()) + ":0" + String(now.minute()) + ":" + String(now.second()) + "\"");
}
else {
Serial.print("Time.txt=\"" + String(now.date()) + "/" + String(now.month()) + "/" + String(now.year()) + " " + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()) + "\"");
}
NextionEndCommand();
}
if ( hladinavody <= 4)
{
TimerFreeTone(5, 3830, 500);
digitalWrite(22, LOW);
} else {
digitalWrite(22, HIGH);
}
if ((millis() - oldTime1) > 10)
{
oldTime1 = millis();
nexLoop(nex_listen_list);
}
}
void pulseCounter()
{
pulseCount++;
}
void NextionEndCommand()
{
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
It's really long code, is it problem with so much commands sent via Serial??? Sorry for not translating it, but it should give you basic idea.
Thank you for any suggestions.