Pump will not flash for less than a second

I am new to computer coding so I will try and explain it as best I can. I am designing a thermal control system but i am having issues with the switching of a pump. when the system reached a certain temperature the code switches a pump on. The pump is switched on via a small transistor circuit. I am using a library named LedFlasher so that I can adjust the time the pump is on and off. The problem I am having is if I change the figures to less than 1000 the pump will never stay on or remain off for less than a second. I would like the pump to switch on for anything above 100ms and off for 10s. I have tested the pump LedFlasher code outside of the main code and it worked fine. Could it be an issue with the Arduino Uno having to cycle through the code each time?

Here is my code:

#include <LiquidCrystal.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <OneWire.h>
#include <SD.h>
#include "RTClib.h"
#include <LedFlasher.h>


int LogInterval = 1000; // mills between entries
int WriteInterval = 1000;
unsigned long LogTimer;
uint32_t syncTime = 0; 
RTC_DS1307 RTC; // define the real time clock object 
const int LogPin = 10; 
File logfile;

#define ONE_WIRE_BUS 3
LiquidCrystal lcd(5,6,4);
OneWire oneWire (ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


int LedPin = 7;

int ThrottlePin = 9;

LedFlasher Pump (8,10000,100); // Pin 8, off for "ms, on for "ms


float coolingTemp = 23;  //Pump temperature threshold
float overheatTemp = 26; //Overheat threshold


void Log()
{
   DateTime now;              // fetch the time

  now = RTC.now();            // Tell the logger that this is the time to log
  
  logfile.println();
  LogTimer = millis();
}

void setup() {
  
lcd.begin(16,2);
sensors.begin();
SPI.begin();
Wire.begin();
Pump.begin();
lcd.clear();

lcd.print("Initializing SD card...");
delay(500);

pinMode(LogPin, OUTPUT);

if (!SD.begin(LogPin)){
  lcd.clear();
  lcd.print("Card failed");
  delay (10000);
  return;
}

lcd.clear();
lcd.print("Card initialized");
delay (500);

char filename[] = "TEST_000.CSV";
for (uint8_t i = 0; i< 100; i++) {
  filename[6] = i/10 + '0';
  filename[7] = i%10 + '0';
  if (!SD.exists(filename)) {
    logfile = SD.open(filename, FILE_WRITE);
    break;
  }
}

if (! logfile) {
  lcd.clear();
  lcd.print("Couldnt create file");
  return;
}

lcd.clear();
lcd.print("Logging to:");
lcd.setCursor(0, 1);
lcd.print(filename);
delay(1000); 
lcd.clear();

if (!RTC.begin()) {
  logfile.println("RTC failed");
  lcd.print("RTC failed");
  delay (10000);
  return;
}

logfile.println("Unix Time,Date,Time,DS18B20 LH,DS18B20 RH,Pump,Overheat");
  
   {
    pinMode(LedPin, OUTPUT);
    pinMode(ThrottlePin, OUTPUT);
  
}
}



void loop() 
{
  DateTime now;
  
  uint32_t m = millis();
  if ( (millis () - LogTimer) >= LogInterval)
  Log();
  if ((millis() - syncTime) < WriteInterval) return;
  syncTime = millis();
  logfile.flush();
 
  now = RTC.now();
  
  
  logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print(", ");
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(", ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print(", ");                            // Include comma to separate value (for CSV file)
  logfile.print(sensors.getTempCByIndex(0));      // Write ds18b20 1 value
  logfile.print(", ");    
  logfile.print(sensors.getTempCByIndex(1));      // Write ds18b20 2 value
  
  lcd.setCursor(0, 0);
  lcd.print("Temperature (C)");
  
  lcd.setCursor(0, 1);
  lcd.print("L:");
  lcd.print(sensors.getTempCByIndex(0));
  
  lcd.setCursor(9, 1);
  lcd.print("R:");
  lcd.print(sensors.getTempCByIndex(1));
  
  sensors.requestTemperatures();

  
if (sensors.getTempCByIndex(0) > coolingTemp && sensors.getTempCByIndex(0) < overheatTemp || sensors.getTempCByIndex(1) > coolingTemp && sensors.getTempCByIndex(1) < overheatTemp) 
{
 
  Pump.update ();
  digitalWrite(LedPin, LOW);
  digitalWrite(ThrottlePin, LOW);
  logfile.print (", ");
  logfile.print ("ON");
}
else if (sensors.getTempCByIndex(0)>= overheatTemp || sensors.getTempCByIndex(1) >= overheatTemp)
  {
    
    Pump.update ();
    digitalWrite(LedPin, HIGH);
    digitalWrite(ThrottlePin, HIGH);
    logfile.print (", ");
    logfile.print ("ON");
    logfile.print (", ");
    logfile.print ("HOT");
    lcd.clear ();
    lcd.setCursor (0, 0);
    lcd.print ("HOT");
    lcd.setCursor(0, 1);
    lcd.print("L:");
    lcd.print(sensors.getTempCByIndex(0));
    lcd.setCursor(9, 1);
    lcd.print("R:");
    lcd.print(sensors.getTempCByIndex(1));
    
  }
  else
  {
 
    digitalWrite(LedPin, LOW);
    digitalWrite(ThrottlePin, LOW);
  }
}

Thank you for your time, help would be very much appreciated.

This statement would prevent most of your loop() function from being executed most of the time:

if ((millis() - syncTime) < WriteInterval) return;

I suggest you adopt a more consistent approach to handling timed actions. For instance - put the logic about when to call Log() inside Log, so that you can just call it repeatedly and it will do whatever it needs to whenever it needs to. I suggest putting that logging writing code that is currently in loop(), and the code to flush the log file at regular intervals, inside Log() as well. You will probably want to pass in the current ‘on’ and ‘hot’ state variables as function arguments so it can log them when required.

I suggest you do the temperature range checks before the call to Log() so that your state variables are up to date when you make the call.

I don’t see any code to turn the ‘Pump’ off when it is not being flashed. You should either flash it, or turn it off. (You do turn off LedPin (7) but the LedFlasher called Pump is configured on pin 8 - maybe those are supposed to be the same?)

Thankyou very much for your advice, I don’t quite understand what you mean when you say " This statement would prevent most of your loop() function from being executed most of the time:
if ((millis() - syncTime) < WriteInterval) return; " does that mean I can move it into log(). Can I place anything to do with logging the data into log().

The library LedFlasher does not require code to turn the led off. The led on pin 7 is actually an LED which is used to allert the user that a certain temperature has been reached.

lmansi: Thankyou very much for your advice, I don't quite understand what you mean when you say " This statement would prevent most of your loop() function from being executed most of the time:

The code will make the function return (i.e. not execute any following code) unless WriteInterval has elapsed. That means that all the subsequent code won't be executed most of the time.

ok I understant. I have made some changes to the code but the problem is still present. the pump will not turn on for less than a second even if the interval is written as 100ms.

Also I dont know how to log the information in the loop if statement, so that it follows on from the already logged data with a subsequent column?

#include <LiquidCrystal.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <OneWire.h>
#include <SD.h>
#include "RTClib.h"
#include <LedFlasher.h>


int LogInterval = 1000; // mills between entries
int WriteInterval = 1000;
unsigned long LogTimer;
uint32_t syncTime = 0; 
RTC_DS1307 RTC; // define the real time clock object 
const int LogPin = 10; 
File logfile;

#define ONE_WIRE_BUS 3
LiquidCrystal lcd(5,6,4);
OneWire oneWire (ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


int LedPin = 7;

int ThrottlePin = 9;

LedFlasher Pump (8,1000,100); // Pin 8, off for "ms, on for "ms


float coolingTemp = -200;  //Pump temperature threshold
float overheatTemp = -200; //Overheat threshold


void Log()
{
   DateTime now;              // fetch the time
  now = RTC.now();   


  logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print(", ");
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(", ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print(", ");                            // Include comma to separate value (for CSV file)
  logfile.print(sensors.getTempCByIndex(0));      // Write ds18b20 1 value
  logfile.print(", ");    
  logfile.print(sensors.getTempCByIndex(1));  // Write ds18b20 2 value

  logfile.println();

}

void setup() {
  
LogTimer = millis();  
lcd.begin(16,2);
sensors.begin();
SPI.begin();
Wire.begin();
Pump.begin();
lcd.clear();

lcd.print("Initializing SD card...");
delay(500);

pinMode(LogPin, OUTPUT);

if (!SD.begin(LogPin)){
  lcd.clear();
  lcd.print("Card failed");
  delay (10000);
  return;
}

lcd.clear();
lcd.print("Card initialized");
delay (500);

char filename[] = "TEST_000.CSV";
for (uint8_t i = 0; i< 100; i++) {
  filename[6] = i/10 + '0';
  filename[7] = i%10 + '0';
  if (!SD.exists(filename)) {
    logfile = SD.open(filename, FILE_WRITE);
    break;
  }
}

if (! logfile) {
  lcd.clear();
  lcd.print("Couldnt create file");
  return;
}

lcd.clear();
lcd.print("Logging to:");
lcd.setCursor(0, 1);
lcd.print(filename);
delay(1000); 
lcd.clear();

if (!RTC.begin()) {
  lcd.print("RTC failed");
  delay (10000);
  return;
}

logfile.println("Unix Time,Date,Time,DS18B20 LH,DS18B20 RH,Pump,Overheat");
  
   {
    pinMode(LedPin, OUTPUT);
    pinMode(ThrottlePin, OUTPUT);
  
}
}


void loop() 
{
  
  sensors.requestTemperatures();

  
if (sensors.getTempCByIndex(0) > coolingTemp && sensors.getTempCByIndex(0) < overheatTemp || sensors.getTempCByIndex(1) > coolingTemp && sensors.getTempCByIndex(1) < overheatTemp) 
{
 
  Pump.update ();
  digitalWrite(LedPin, LOW);
  digitalWrite(ThrottlePin, LOW);
  logfile.print (", ");
  logfile.print ("ON");
  
}
else if (sensors.getTempCByIndex(0)>= overheatTemp || sensors.getTempCByIndex(1) >= overheatTemp)
  {
    
    Pump.update ();
    digitalWrite(LedPin, HIGH);
    digitalWrite(ThrottlePin, HIGH);
    
    lcd.clear ();
    lcd.setCursor (0, 0);
    lcd.print ("HOT");
    lcd.setCursor(0, 1);
    lcd.print("L:");
    lcd.print(sensors.getTempCByIndex(0));
    lcd.setCursor(9, 1);
    lcd.print("R:");
    lcd.print(sensors.getTempCByIndex(1));
    logfile.print (", ");
    logfile.print ("ON");
    logfile.print (", ");
    logfile.print ("HOT");
    
  }
  else
  {
 
    digitalWrite(LedPin, LOW);
    digitalWrite(ThrottlePin, LOW);
    
  }
 

  
  lcd.setCursor(0, 0);
  lcd.print("Temperature (C)");
  
  lcd.setCursor(0, 1);
  lcd.print("L:");
  lcd.print(sensors.getTempCByIndex(0));
  
  lcd.setCursor(9, 1);
  lcd.print("R:");
  lcd.print(sensors.getTempCByIndex(1));
  
    uint32_t m = millis(); // keep track of time for the logger
  if ( (millis () - LogTimer) >= LogInterval) //checking how long it has been since last log and comparing to the interval set between logs
  Log();
  if ((millis() - syncTime) < WriteInterval) return; // checking how long it has been since last time data was written to the SD card and comparing to the interval set between writes
  syncTime = millis();
  logfile.flush();  // Tell the logger that this is the time to log
  
}