Function not working

Hey,
I am using the following components to make a datalogging weather station:-

  1. DHT11 (humidity and temperature) sensor
  2. LDR (a standard light intensity measurement) sensor
  3. DS3231 RTC module (for time keeping purpose)
  4. A standard SD Card module for data storage purpose.
    This is the code I've written for it :-
#include<Wire.h>
#include<SD.h>
#include<dht.h>
dht DHT;
const int flux=A0;
const int temp_humid=6;
const int CS=10;
const int power=2;
#define address 0x68
int decToBcd(int val)
{
  return( (val/10*16) + (val%10) );
}
int bcdToDec(int val)
{
  return( (val/16*10) + (val%16) );
}
void setup()
{
  Wire.begin();
  pinMode(flux,INPUT_PULLUP); //for light
  pinMode(CS,OUTPUT); //chip select
  pinMode(power,OUTPUT); //power for SD card
  pinMode(temp_humid,INPUT); //DHT sensor
  digitalWrite(power,HIGH); //setting power pin to high for SD card
  Serial.begin(9600); //initializing Serial communication
  if(!SD.begin(CS))
  {
    Serial.println("Card failed");
  }
  else
  {
    Serial.println("Card loaded !");
  }
  File datafile=SD.open("log.csv",FILE_WRITE);
  if(datafile)
  {
    datafile.println(",,,,,,,,,,");
    String header="#,Temperature,Humidity,Light,Date,Month,Year,Hour,Minute,Second,Day";
    datafile.println(header);
    datafile.close();
  }
  // setDS3231time(30,42,21,4,26,11,14);
}
void setDS3231time(int second, int minute, int hour, int dayOfWeek, int dayOfMonth, int month, int year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(address);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(int *second, int *minute, int *hour, int *dayOfWeek, int *dayOfMonth, int *month, int *year)
{
  Wire.beginTransmission(address);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(address, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void Weekday(int l,char s[10]) //for converting number to Day of Week
{
  if(l==1)
  strcpy(s,"Sunday");
  else if(l==2)
  strcpy(s,"Monday");
  else if(l==3)
  strcpy(s,"Tuesday");
  else if(l==4)
  strcpy(s,"Wednesday");
  else if(l==5)
  strcpy(s,"Thursday");
  else if(l==6)
  strcpy(s,"Friday");
  else if(l==7)
  strcpy(s,"Saturday");
}
float ldrsensor() //function for retrieving light percentage
{
  float val;
  val=analogRead(flux);
  val=map(val,15,1005,100,0);
  return val;
}
long id=1; //for ID purpose
void loop()
{
 float light; //variable for retrieving light value
 light=ldrsensor();
 char s[10];
 DHT.read11(temp_humid); //reading DHT sensor data
 int second, minute, hour, dayOfWeek, dayOfMonth, month, year; //variables for retrieving RTC data
 readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); //reading RTC data
 File datafile=SD.open("log.csv",FILE_WRITE);
 Weekday(dayOfWeek,s[10]);
 String rawdata=String(id)+", "+String(DHT.temperature)+", "+String(DHT.humidity)+", "+String(light)+", "+String(dayOfMonth)+", "+String(month)+", "+String(year)+", "+String(hour)+", "+String(minute)+", "+String(second)+", "+String(dayOfWeek);
 String disdata=String(id)+", "+String(DHT.temperature)+" C "+String(DHT.humidity)+"% "+String(light)+"% "+String(dayOfMonth)+"/"+String(month)+"/"+String(year)+" "+String(hour)+":"+String(minute)+":"+String(second)+" "+String(dayOfWeek);
 if(datafile)
 {
   Serial.println(disdata);
   datafile.println(rawdata);
   id++;
   datafile.close();
 }
 else
 {
   Serial.println("Can't save to card");
 }
 delay(1000);
}

Since the DS3231 returns the day of the week (variable-dayOfWeek) in form of a number ranging from 1-7, I had to make a new function called Weekday to convert that number into corresponding days (i.e. Sunday,Monday, et al). For debugging purposes, I've displayed the data onto the Serial Monitor.
The problem that I'm encountering is that the Weekday function is not working. When I pass in the parameters and load the code, the day of week part disappears altogether. When I comment out that part, the code works perfectly, yet I'm stuck with a raw number (1-7) instead of the name of the day.
Can anyone resolve this anomaly ?

The code you posted cannot possibly compile. You are passing the wrong datatype...

 Weekday(dayOfWeek,s[10]);

But the code did compile. What didn't happen was that the dayOfWeek section disappeared altogether. Any leads on this ?

UseWeekday(dayOfWeek, s);

You want to pass the address to the first buffer char, not the value of a byte behind your buffer.

Your Weekday function assumes that l will always be between 1 and 7 inclusive. Things can go wrong and you should end the function something like this to trap errors...

  else if(l==7)
      strcpy(s,"Saturday");
  else {
      strcpy(s,"L error") ;
  }
}
    datafile.println(",,,,,,,,,,");
    String header="#,Temperature,Humidity,Light,Date,Month,Year,Hour,Minute,Second,Day";
    datafile.println(header);

That is stupid. There is NO reason to piss away resources wrapping a string literal, copied from flash to SRAM uselessly, in a String object and then making the println() function unwrap the String object.

datafile.println(F("#,Temperature,Humidity,Light,Date,Month,Year,Hour,Minute,Second,Day"));

Why does Weekday() not return a char *?

const char *Weekday(byte dayOfWeek)
{
   switch(dayOfWeek)
   {
       case 1:
         return "Sunday";
         break;
       // Other cases
       default:
          return "Invalid";
          break;
    }
}

Returning a pointer to a literal (stored somewhere in SRAM) is perfectly valid.

Using the correct argument type is better, and using a meaningful name for the argument is WAY better.

Experiment with this:

const char *days[] = {
  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
  "Friday", "Saturday", "Sunday"
};


void setup() {
  Serial.begin(9600);
  Serial.println("Enter day-of-week (1 - 7): ");
}

void loop() {
  char result[10];
  int day;
  
  if (Serial.available() > 0) {
    day = (int) (Serial.read() - '0');    // Convert digit char to int
    if (day > 0 && day < 8) {             // In range?
      strcpy(result, days[day]);
    } else {
      strcpy(result, "Error");  
    }
    Serial.println(result);    
  }
}

and follow what it does. Most programmers avoid the String class as it chews up more memory than is really necessary. Use C strings (note lower case 's') built up from char arrays instead.

-fpermissive makes things possible ... >:( >:( >:( >:( >:( >:(

oqibidipo:
-fpermissive makes things possible ...

Must not be enabled in my version. :slight_smile:

Even if you copy the 'dayOfWeek' name into 's' it won't be displayed since you use 'dayOfWeek' instead of 's':

 Weekday(dayOfWeek,s[10]);
 String rawdata=String(id)+", "+String(DHT.temperature)+", "+String(DHT.humidity)+", "+
String(light)+", "+String(dayOfMonth)+", "+String(month)+", "+String(year)+", "
+String(hour)+", "+String(minute)+", "+String(second)+", "+String(dayOfWeek);

 String disdata=String(id)+", "+String(DHT.temperature)+" C "+String(DHT.humidity)+"% "
+String(light)+"% "+String(dayOfMonth)+"/"+String(month)+"/"+String(year)+" "
+String(hour)+":"+String(minute)+":"+String(second)+" "+String(dayOfWeek);