First Code

I am trying to streamline this code a bit… let me explain its purpose first.

So I have three fish tanks 1,2,3 I wanted a system to log its data visually and digitally so basically it takes the temp from each tank and then displays it on a LCD and logs the information to an SD card the information then is plotted by GNUplot into a chart. Let me know what you think

ignore the pin numbers… I understand they conflict I just wrote the code before assembly I am waiting for the parts to come in.

//Welcome to Michael Meyer's Temp logging system, here is the theory of operation:
//First the code sets up the LCD display, probes, and SD sheild from there it then
//Displays information to the LCD during start-up
//Clears the LCD then prints basic information such as title of the program and each tank
//The program finds the temp of each tank
//After finding the temp it then prints the information to the LCD
//Then shortly after it logs the information to a pre-formated excel document stored on the SD card (Please note that the file is generated during the first run, the information then needs to be formated to an excel doc you-
//Can also use GNUplot for the information
//To look at the file simply take out the SD card and plug it into your computer and veiw the file.


//The Temp measuring device must be hooked up to GND, pin 10 or any PWM, and the 5V power supply from the 5V a resistor must be placed before the device

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

#define REDLITE 3 //pin for red
#define GREENLITE 5 // pin for green
#define BLUELITE 6  //pin for blue
#define Temp_precision 12 //how many bits of data are used for measuring temp
#define LOG_INTERVAL 1000 //mills between entries
#define echo_to_serial 0 // echo data to serial port
#define SYNC_INTERVAL 1000
uint32_t syncTime = 0;
#define wait_to_start 0 // wait for serial input in setup()

float tempF;
float tempF1;
float tempF2;

LiquidCrystal lcd(7,8,9,10,11,12); //Set pins to where ever the lcd is plugged into the board. This command intializes the library with the number of the interface pins

int brightness = 255; // brightness is a range of 0 - 255

const int busPin = 10; //Set to an PWM pin

OneWire bus(busPin); //Sets the bus to the variable of 10
DallasTemperature sensors(&bus); //sets the sensor to that bus
DeviceAddress Tank1, Tank2, Tank3; //An Array that holds device address

RTC_DS1307 RTC; //DEFINES THE REAL TIME CLOCK OBJET

const int chipSelect = 10; // sets the pin for the SD cs line

File logfile;


void setup ()
{
  Serial.begin(9600);
  Serial.println();
  lcd.begin(20,4); //Size of lcd display 16 being length and 2 being height
  lcd.print("Welcome to Michael's");
  lcd.setCursor(0,1); //sets the posision of where you are going to type next 1 equal the second line and 0 means the closest the left side
  lcd.print("Fish Watch Program");  
  pinMode(REDLITE, OUTPUT);
  pinMode(GREENLITE, OUTPUT);
  pinMode(BLUELITE, OUTPUT);
  
  brightness = 100;
  
  for (int i = 0; i < 255; i++) 
  { //this sets the backlight for the backlite LCD display
    setBacklight(i, 0, 255-i);
  }
  
  sensors.begin(); //this command starts the library for the senors

//Tank1 = {};
//Tank2 = {};
//Tank3 = {};

  sensors.setResolution(Tank1, Temp_precision);
  sensors.setResolution(Tank2, Temp_precision);
  sensors.setResolution(Tank3, Temp_precision);
  if (!sensors.getAddress(Tank1, 0)) Serial.println("Unable to find address for Tank1");
  if (!sensors.getAddress(Tank2, 1)) Serial.println("Unable to find address for Tank2");
  if (!sensors.getAddress(Tank3, 2)) Serial.println("Unable to find address for Tank3");

pinMode(10, OUTPUT); //this is default chip select it must be 10

lcd.setCursor (0,3);
lcd.print("Initializing SD card");
delay (100);

//See if the card is present and can be initialized
if (!SD.begin(chipSelect))
{
  lcd.setCursor (4,4);
  lcd.print("Card failed");
  return;
}
//Creates the log file 
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++)
{
  filename[6] = i/10 + '0';
  filename[7] = i%10 + '0';
  if (! SD.exists(filename)) {
    //only open a new file if it doesn't exist
    logfile = SD.open(filename, FILE_WRITE);
    break;
  }
}
  
  Wire.begin();
  if (!RTC.begin()) {
    logfile.println("RTC failed");
  }
  
  logfile.println("Time, Tank1Temp, Tank2Temp, Tank3Temp"); //this sets the headers

  
  delay (1000);
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Meyer Tank Montior");
  lcd.setCursor(0,1);
  lcd.print("Tank1:");
  lcd.setCursor(0,2);
  lcd.print("Tank2:");
  lcd.setCursor(0,3);
  lcd.print("Tank3:");
  lcd.noBlink();
}

void loop()
{
DateTime now;
//This sets the dleay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis()% LOG_INTERVAL));

sensors.requestTemperatures();

PrintTemperatureTank1;
PrintTemperatureTank2;
PrintTemperatureTank3;

uint32_t m = millis();
logfile.print(m);
logfile.print(",");

now=RTC.now();

logfile.print(now.unixtime());
logfile.print(", ");
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(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);

logfile.print(", ");
logfile.print(tempF); 
logfile.print(", ");
logfile.print(tempF1); 
logfile.print(", ");
logfile.print(tempF2); 

logfile.println();

if((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();

logfile.flush();

}

void PrintTemperatureTank1(DeviceAddress Tank1)//Gets Temp and displays on tank 1 line
{
  tempF = sensors.getTempF(Tank1); //used to be float tempF = sensors.getTempF(Tank1)
  lcd.setCursor(1,7);
  lcd.print(tempF);
}

void PrintTemperatureTank2(DeviceAddress Tank2) //Gets Temp and displys on tank 2 line
{
 tempF1 = sensors.getTempF(Tank2); //used to be float tempF1 = sensors.getTempF(Tank2)
 lcd.setCursor(2,7);
 lcd.print(tempF1);
 }
 
void PrintTemperatureTank3(DeviceAddress Tank3) //Gets Temp and displays on tank 3 line
{
  tempF2 = sensors.getTempF(Tank3); //used to be float tempF2 = sensors.getTempF(Tank3)
  lcd.setCursor(3,7);
  lcd.print(tempF2);
}

void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
  // normalize the red LED - its brighter than the rest!
  //this code is the program that sets the backlight
  r = map(r, 0, 255, 0, 100);
  g = map(g, 0, 255, 0, 150);
 
  r = map(r, 0, 255, 0, brightness);
  g = map(g, 0, 255, 0, brightness);
  b = map(b, 0, 255, 0, brightness);
 
  // common anode so invert!
  r = map(r, 0, 255, 255, 0);
  g = map(g, 0, 255, 255, 0);
  b = map(b, 0, 255, 255, 0);
  Serial.print("R = "); Serial.print(r, DEC);
  Serial.print(" G = "); Serial.print(g, DEC);
  Serial.print(" B = "); Serial.println(b, DEC);
  analogWrite(REDLITE, r);
  analogWrite(GREENLITE, g);
  analogWrite(BLUELITE, b);
}

So I have three fish tanks 1,2,3 I wanted a system to log its data visually and digitally so basically it takes the temp from each tank and then displays it on a LCD and logs the information to an SD card the information then is plotted by GNUplot into a chart. Let me know what you think

I think you need some of these .........................

I am trying to streamline this code a bit.

ignore the pin numbers.. I understand they conflict I just wrote the code before assembly I am waiting for the parts to come in.

Trying to optimize code before you have even tested it on hardware is premature.

PrintTemperatureTank1;
PrintTemperatureTank2;
PrintTemperatureTank3;

These statements might as well read: 47; 389; 123945; They do NOT call the functions.

How do they not call the functions? granted I forgot () at the end…

Have to agree with PaulS - you can think about the overall program design, but you need to have the hardware to test it out first. I would also recommend writing the code in a modular fashion - get the temp probes debugged and working first, then the LCD, etc. It will be much easier to debug that way.

What's the point of this line? delay((LOG_INTERVAL -1) - (millis()% LOG_INTERVAL));

Why not just say delay(logInterval)? Or set a time point when you want to take your next set of measurements (see the blink without delay example)

The printTemerature routines could likely made into one routine with different arguments (you don't have any arguments when you call them but the definitions have them, so you'll get an error). The function definitions also need types for the arguments.

The setBacklight routine is also probably largely unnecessary; you're probably going to pick a color and just leave it. Even if you use 2 you don't need that much code.

Thanks for the input I am just excited to get started the equipment gets here today lol…

But I revised the code with your input…

I am going to leave the code at the end for the back-lighting encase I want to turn the display red when the temp goes over a set amount.

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

#define REDLITE 3 //pin for red
#define GREENLITE 5 // pin for green
#define BLUELITE 6  //pin for blue
#define Temp_precision 12 //how many bits of data are used for measuring temp
#define echo_to_serial 0 // echo data to serial port
uint32_t syncTime = 0;
#define wait_to_start 0 // wait for serial input in setup()

long previousMillis = 0;
long interval = 60000;

float tempF;
float tempF1;
float tempF2;

LiquidCrystal lcd(7,8,9,10,11,12); //Set pins to where ever the lcd is plugged into the board. This command intializes the library with the number of the interface pins

int brightness = 255; // brightness is a range of 0 - 255

const int busPin = 10; //Set to an PWM pin

OneWire bus(busPin); //Sets the bus to the variable of 10
DallasTemperature sensors(&bus); //sets the sensor to that bus
DeviceAddress Tank1, Tank2, Tank3; //An Array that holds device address

RTC_DS1307 RTC; //DEFINES THE REAL TIME CLOCK OBJET

const int chipSelect = 10; // sets the pin for the SD cs line

File logfile;


void setup ()
{
  Serial.begin(9600);
  Serial.println();
  lcd.begin(20,4); //Size of lcd display 16 being length and 2 being height
  lcd.print("Welcome to Michael's");
  lcd.setCursor(0,1); //sets the posision of where you are going to type next 1 equal the second line and 0 means the closest the left side
  lcd.print("Fish Watch Program");  
  pinMode(REDLITE, OUTPUT);
  pinMode(GREENLITE, OUTPUT);
  pinMode(BLUELITE, OUTPUT);
  
  brightness = 100;
  
  for (int i = 0; i < 255; i++) 
  { //this sets the backlight for the backlite LCD display
    setBacklight(i, 0, 255-i);
  }
  
  sensors.begin(); //this command starts the library for the senors

//These are the device address' these must be found through an other code
//Tank1 = {};
//Tank2 = {};
//Tank3 = {};

  sensors.setResolution(Tank1, Temp_precision);
  sensors.setResolution(Tank2, Temp_precision);
  sensors.setResolution(Tank3, Temp_precision);
  if (!sensors.getAddress(Tank1, 0)) Serial.println("Unable to find address for Tank1");
  if (!sensors.getAddress(Tank2, 1)) Serial.println("Unable to find address for Tank2");
  if (!sensors.getAddress(Tank3, 2)) Serial.println("Unable to find address for Tank3");

pinMode(10, OUTPUT); //this is default chip select it must be 10

lcd.setCursor (0,3);
lcd.print("Initializing SD card");
delay (100);

//See if the card is present and can be initialized
if (!SD.begin(chipSelect))
{
  lcd.setCursor (4,4);
  lcd.print("Card failed");
  return;
}
//Creates the log file 
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++)
{
  filename[6] = i/10 + '0';
  filename[7] = i%10 + '0';
  if (! SD.exists(filename)) {
    //only open a new file if it doesn't exist
    logfile = SD.open(filename, FILE_WRITE);
    break;
  }
}
  
  Wire.begin();
  if (!RTC.begin()) {
    logfile.println("RTC failed");
  }
  
  logfile.println("Time, Tank1Temp, Tank2Temp, Tank3Temp"); //this sets the headers

  delay (5000);
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Meyer Tank Montior");
  lcd.setCursor(0,1);
  lcd.print("Tank1:");
  lcd.setCursor(0,2);
  lcd.print("Tank2:");
  lcd.setCursor(0,3);
  lcd.print("Tank3:");
  lcd.noBlink();
}

void loop()
{
unsigned long currentMillis = millis();

if(currentMillis = previousMillis > interval) {
  
  previousMillis = currentMillis;

  DateTime now;
//This sets the dleay for the amount of time we want between readings
sensors.requestTemperatures();

PrintTemperatureTank1(Tank1);
PrintTemperatureTank2(Tank2);
PrintTemperatureTank3(Tank3);

now=RTC.now();
uint32_t m = millis();
logfile.print(m);
logfile.print(",");
logfile.print(now.unixtime());
logfile.print(", ");
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(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
//sensor data printing
logfile.print(", ");
logfile.print(tempF); 
logfile.print(", ");
logfile.print(tempF1); 
logfile.print(", ");
logfile.print(tempF2); 
logfile.println();
logfile.flush();
}
}


void PrintTemperatureTank1(DeviceAddress deviceAddress)//Gets Temp and displays on tank 1 line
{
  tempF = sensors.getTempF(deviceAddress); //used to be float tempF = sensors.getTempF(Tank1)
  lcd.setCursor(1,7);
  lcd.print(tempF);
}

void PrintTemperatureTank2(DeviceAddress deviceAddress) //Gets Temp and displys on tank 2 line
{
 tempF1 = sensors.getTempF(deviceAddress); //used to be float tempF1 = sensors.getTempF(Tank2)
 lcd.setCursor(2,7);
 lcd.print(tempF1);
 }
 
void PrintTemperatureTank3(DeviceAddress deviceAddress) //Gets Temp and displays on tank 3 line
{
  tempF2 = sensors.getTempF(deviceAddress); //used to be float tempF2 = sensors.getTempF(Tank3)
  lcd.setCursor(3,7);
  lcd.print(tempF2);
}

void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
  // normalize the red LED - its brighter than the rest!
  //this code is the program that sets the backlight
  r = map(r, 0, 255, 0, 100);
  g = map(g, 0, 255, 0, 150);
 
  r = map(r, 0, 255, 0, brightness);
  g = map(g, 0, 255, 0, brightness);
  b = map(b, 0, 255, 0, brightness);
 
  // common anode so invert!
  r = map(r, 0, 255, 255, 0);
  g = map(g, 0, 255, 255, 0);
  b = map(b, 0, 255, 255, 0);
  Serial.print("R = "); Serial.print(r, DEC);
  Serial.print(" G = "); Serial.print(g, DEC);
  Serial.print(" B = "); Serial.println(b, DEC);
  analogWrite(REDLITE, r);
  analogWrite(GREENLITE, g);
  analogWrite(BLUELITE, b);
}

if(currentMillis = previousMillis > interval) {

You need to re-look at this one.... I think you mean the = to be a -, and probably needs an extra set of ().

ahh thank you :smiley:

How do they not call the functions? granted I forgot () at the end...

The name of the function without the parentheses on the end resolves to an address, like 47.

47;

does not call a function.

The name with the parentheses on the end is how you indicate that you want to call the function whose entry point is at that address.