Check my code please

Hi everyone,

I’m trying to read several temperature sensors, write their value into a file on a microSD and also RTC.
My code currently takes up 52% which I feel is a lot. I’d appreciate input on what I can make easier or more streamlined. Please keep in mind that I’m a beginner and still learning. Code verifies but I couldn’t test it yet due to lack of a batterie for the RTC.
I’m doing all this with an Arduino Uno.
Thank you!

/*

Setup of Arduino Uno and SD card
* analog sensors on analog A0 to A5
 * SD card attached as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 
 ** power to 5, need to check output as USB doesn't provide enough power to run it. Gives error of partition not found if not enough juice
RTC
**VCC to 5V
**GND to GND
**SDA to SDA
**SCL to SCL
**SQW unused

//  Thermistor connection
//    Vin (5V) --\/\thermistor\/\---- Vout to analog input pin
//                                 |
//                                  ---(\/\/\R2\/\/\)--- GND

//  For best results select R1 equal to the thermistor resistance
//    at the temperature being read (maximizes resolution of ADC)
//  (25k Ohm coresponds to ~6C)
// Thermistor: Thermistors, US Sensor PS103J2, 10K ohm 615-1003-ND Digikey
// Resistor: RESISTOR, 0.01% 25K; Resistance:25kohm; Resistance Tolerance:± 0.01% USR2-0808-25KT-ND Digikey

print_time() returns a string in the format mm-dd-yyyy hh:mm:ss
*/

#include <RTClib.h>
#include <SPI.h>
#include <SD.h>


RTC_DS1307 rtc;
const int chipSelect = 4; // where SD CS is connected

File dataFile;

String print_time(DateTime timestamp) {
  char message[120];

  int Year = timestamp.year();
  int Month = timestamp.month();
  int Day = timestamp.day();
  int Hour = timestamp.hour();
  int Minute = timestamp.minute();
  int Second= timestamp.second();

  sprintf(message, "%d-%d-%d %02d:%02d:%02d", Month,Day,Year,Hour,Minute,Second);
  
  return message;
}

void setup(){
  Serial.begin(9600); // opens connection to arduino
  
  pinMode(4, OUTPUT); // select pin for output, where CS is on SD card
  if (!SD.begin(4)){
    Serial.println("Error: SD card would not initiate.");
  } else {Serial.println("initialization done.");
  }
  
  rtc.begin();
  if (!rtc.isrunning()){
    Serial.println("Clock is not running");
  }

  
  dataFile = SD.open("TMSt1.csv", FILE_WRITE); // makes file and read and writes
  
  if (dataFile) {
    Serial.println("writing to TMSt1.csv ");
  dataFile.println("Date, Time, TM1, TM2, TM3, TM4, TM5"); // writes header into file once, if reset pressed it will do it again at the end of the file before new reading begin
  dataFile.close(); // ensures that header is saved, always need to close file to save
  } else {
    Serial.println("TMSt1.csv didn't open ");
  }                                             
}

void loop(){    // as a loop it keeps writing the values into file on sd card
  
 // there has to be a more elegant way of doing this // 3.3 or 5 depending on where on arduino it is connected, //might need to change this to 12 bit
  int sensorValue_A1 = analogRead(A1); //pin at which thermistor is connected (A1-A5)
  // convert analog to voltage
  float voltage_A1 = sensorValue_A1 * (5 / 1023.0); 
  int sensorValue_A2 = analogRead(A2); //pin at which thermistor is connected (A1-A5)
  // convert analog to voltage
  float voltage_A2 = sensorValue_A2 * (5 / 1023.0); 
  int sensorValue_A3 = analogRead(A3); //pin at which thermistor is connected (A1-A5)
  // convert analog to voltage
  float voltage_A3 = sensorValue_A3 * (5 / 1023.0);
  int sensorValue_A4 = analogRead(A4); //pin at which thermistor is connected (A1-A5)
  // convert analog to voltage
  float voltage_A4 = sensorValue_A4 * (5 / 1023.0);
  int sensorValue_A5 = analogRead(A5); //pin at which thermistor is connected (A1-A5)
  // convert analog to voltage
  float voltage_A5 = sensorValue_A5 * (5 / 1023.0);
  
  Serial.println(voltage_A1); // to check it's reading something and if it makes sense, need to check how to write all readings in one line

 File dataFile = SD.open("TMSt1.csv", FILE_WRITE);
 if (dataFile){
  
 
  
  DateTime now = rtc.now(); //gets current date and time from rtc
  dataFile.println(print_time(now)); //write date and time reading in file
  dataFile.println(",");
  dataFile.print(voltage_A1); 
  dataFile.println();
  dataFile.print(",");
  dataFile.print(voltage_A2); //writes voltage in file
  dataFile.print(","); //seperates voltage from date and time
  dataFile.print(voltage_A3);
  dataFile.print(",");
  dataFile.print(voltage_A4);
  dataFile.print(",");
  dataFile.println(voltage_A5); 
  dataFile.close();  //works perfectly now, write everything until println in same line
 }
 else{ Serial.println("couldn't open file");
 }
  delay(5000); //prints time and voltage every 5 seconds
}

Well I cannot see where you have said which Arduino you are using, is it a secret ?

But if the code works, and only occupies 52% of the not revealed Arduino, why is it a problem ?

Right, I totally forgot about that. Arduino Uno

I see a bug in the code.

String print_time(DateTime timestamp) {
  char message[120];

  int Year = timestamp.year();
  int Month = timestamp.month();
  int Day = timestamp.day();
  int Hour = timestamp.hour();
  int Minute = timestamp.minute();
  int Second= timestamp.second();

  sprintf(message, "%d-%d-%d %02d:%02d:%02d", Month,Day,Year,Hour,Minute,Second);
  
  return message;
}

Or maybe not. Since you had it return String (there’s a bunch of code you don’t need) it’s probably making a copy of the local buffer.

Still, this would be better (and smaller since that’s what you’re asking) to return a char pointer instead of a String object. I’m that case the message array would need to be static so you’re not returning a pointer to something in the trash can.