Need help printing day of week to Serial.print() using array

Hi,

Today is Saturday. Using the Time library and calling function weekday() I can see the day number for Saturday is 7 (Sunday is 1 etc).

I wish to use an array to associate the day number, drawn from weekday(), with the actual three letter day eg Sat.

Here is how I am trying to do that:

  char const * day0[] = {"dummy_value","Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
  int day_ = weekday();     // works fine
  Serial.print(day_);           // works fine
  Serial.print(day0[day_]); // does not work

My intention in the last line is to run along the array until I have the value associated with int variable day_ then print it to Serial.print().

It's not doing that.

Any ideas? :slight_smile:

"dummy_value"

Why are you wasting RAM like that?
On a micro controller, RAM is precious.

is your array index out of boundary?

try

  char const * wdays[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
  int day_ = weekday() % 7;
  Serial.print(day_);
  Serial.print(wdays[day_]);

Thank you robtillaart.

The "% 7" made all the difference. what does it do?

robtillaart:
is your array index out of boundary?

try

  char const * wdays[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};

int day_ = weekday() % 7;
  Serial.print(day_);
  Serial.print(wdays[day_]);

Why not start your array with "Sun", and use weekday() - 1 instead of a modulo operation?

Would this code work?

char week[] = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat\0";
Serial.print(week+((weekday()-1)*4));

Delta_G:

Serial.print(week+(((weekday()-1)%7)*4));

To what end the modulo 7 ?

Delta_G:
To make sure you don't go off the end of the array if ever weekday is some value larger than you expect.

And if the weekday value is 0 ... ?

Besides, if I get a bad weekday value, I want to know about it.

Serial.println("Bad Weekday");

I'm familiar with a bad hair day, this one is new ....

Yes, this topic is old, but i wanted to give some support with the following code


Declaring char (strings)

const char maandag[] PROGMEM = "maandag";
const char dinsdag[] PROGMEM = "dinsdag";
const char woensdag[] PROGMEM = "woensdag";
const char donderdag[] PROGMEM = "donderdag";
const char vrijdag[] PROGMEM = "vrijdag";
const char zaterdag[] PROGMEM = "zaterdag";
const char zondag[] PROGMEM = "zondag";
const char dummy[] PROGMEM = "dummy";

// Put the char day-names strings into Progmem
// zondag = 1
//
PGM_P const dagen[] PROGMEM =
{
dummy,
zondag,
maandag,
dinsdag,
woensdag,
donderdag,
vrijdag,
zaterdag
};


Show it as a serial print

Serial.print(F("Date / Time / Weekday: "));
Serial.print(year());
Serial.print(F("-"));
Serial.print(month());
Serial.print(F("-"));
Serial.print(day());
Serial.print(F(" "));
Serial.print(hour());
Serial.print(F(":"));
Serial.print(minute());
Serial.print(F(":"));
Serial.print(second());
Serial.print(F(" / "));

char prn_dag[10];
strcpy_P(prn_dag, (PGM_P)pgm_read_word(&(dagen[weekday()])));
Serial.print(prn_dag);
Serial.print(F(" ( "));
Serial.print(weekday());
Serial.print(F(" ) \n"));