Error: too many initializers for 'char []'

Hey everyone, I am working on a system which stores the time and date when a key is pressed and returns it when another key is pressed.
I am using an Arduino Uno and a Ds 1307 Real Time Clock.

I began by taking the example given in the rct library and adapting it to meet what I want to achieve.
It worked in storing and returning the date and time but it broke after I changed something which I can't figure out. It shows an error which says: "too many initializers for 'char []'".

I left the code for a short while due to schedule but now I can't remember what I changed that broke it.
I have gone through over and over.

#include <Keypad.h>
#include <EEPROM.h>
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>


RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"};
char mymonth2[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
char lasttime[10] = {};
char lastdate;
char day_of_week[4] = {};
String mysecond, myminute, myhour, myhour2, myday, mymonth, myyear, mytime, mydate, am, datetime;

const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = 1, d7 = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


const byte numRows = 4;         //number of rows on the keypad
const byte numCols = 4;         //number of columns on the keypad

//The keymap for the keypad

char keymap[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

char keypressed;

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3

Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);


void setup ()
{
  lcd.begin(20, 4);

  if (! rtc.begin()) {
    lcd.print("Couldn't find RTC");
    while (1);
  }
}

void stringconversions() {
  DateTime now = rtc.now();

  //Here, the time details are converted to strings to be printed later on
  myhour = String(now.hour(), DEC);
  if (myhour.toInt() >= 12){
    myhour2 = myhour.toInt() - 12;
    myhour2 = String(myhour2);
    am = "PM";
  }
  else{
    myhour2 = myhour;
    am = "AM";
  }
  myminute = String(now.minute(), DEC);
  mysecond = String(now.second(), DEC);

  //Here, the date details are also converted to strings to be printed too
  myday = String(now.day(), DEC);
  mymonth = String(now.month(), DEC);
  mymonth = mymonth2[mymonth.toInt() + 1];
  myyear = String(now.year(), DEC);

  //Here, the strings for date and time are concatenated together
  mytime = myhour2 + ":" + myminute + ":" + mysecond + " " + am;
  mydate = myday + "/" + mymonth + "/" + myyear;

  datetime = mytime + "" + "-" + "" + mydate;
  
}


void loop ()
{
  keypressed = myKeypad.getKey();
  //DateTime now = rtc.now();
  //stringconversions();
  //lcd.print(mytime);

  //  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);



  if (keypressed == '0') {
    stringconversions();

    //itt is to increment the address for each character stored in the EEPROM
    int att = 35;//this does the same for the characters in the array being saved
    int itt;
    for (itt = 0; itt < 21; itt++) {
      EEPROM.write(att, datetime[itt]);
      att++;
      }
      lcd.print(mydate);
    }

  if (keypressed == '1') {
    int apx = 0;
    int attt = 35;
    int itt;
    for (itt = 0; itt < 21; itt++) {
      lasttime[apx] = EEPROM.read(attt);
      apx++;
      attt++;
    }

    lcd.clear();
    //lcd.setCursor(0, 0);
    lcd.print(lasttime);

  }
}

I would really appreciate any help.

can you post the error msgs?

Why didn't you post the error message?

It told you where the problem is.

char daysOfTheWeek[7][12] Using 84 bytes of precious RAM to store 28 bytes of data is not a good use of resources.

Oh sorry, I meant to indicate the line that it showed but forgot to.

The error is on this line:
char mymonth2[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};

That line was commented-out in your original post.char* mymonth2[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};

Or you could write it like you wrote your days array, but less wastefully.

Wait, char mymonth2[] = {"Jan" etc.? I thought a char could contain only one character? A char array on the other hand... [HINT]

Erik_Baas:
Wait, char mymonth2[] = {"Jan" etc.? I thought a char could contain only one character?

That's right.
Hence "char*"

char lasttime[10] = {};

You are going beyond the boundaries of your lasttime array in these two eeprom loops.

for (itt = 0; itt < 21; itt++) {
      EEPROM.write(att, datetime[itt]);
      att++;
      }

for (itt = 0; itt < 21; itt++) {
      lasttime[apx] = EEPROM.read(attt);
      apx++;
      attt++;
    }

This works well, if you get rid of the unnecessary "t" in "Sept"

char months[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println(months[1]);
}

TheMemberFormerlyKnownAsAWOL:
That's right.
Hence "char*"

warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] :frowning:

Warning.
Meh.

Erik_Baas:
warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] :frowning:

Chances are you don't plan on changing the names of the months. Nor do you plan on pointing the pointers to an alternate set of months. So.....

const char* const mymonth2[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};

Always endeavor to eliminate warnings.

Always endeavor to eliminate warnings.

Including those array boundary warnings. :slight_smile:

Hey everyone.
I have been able to make some corrections (except the array one; I'm not sure the new dimensions to use) and it now works perfectly well. I truly appreciate the quick and helpful responses you've given. I've given karma to all.

The problem is that while the code for storing and retrieving time works on its own when I try to integrate it into the main program where I want it to live and work, it shows the wrong date and time. After I tried to do some figuring to see where the error could be coming from, I see that right from where it does the rtc.now line to obtain time and performs string operations, the "datetime" variable has a wrong value. This is even before storing into EEPROM.

While alone it retrieves the accurate time from the RTC (which was 12:0x pm-24/Mar/21 as at last check) but when put in the main code shows something like 3:0x am-30/May/30.

I have gone through but I cannot seem to find any hints to help solve this.

The main program is quite long and also I'm not sure how I would paste the two so what I'd do is that I'll paste the isolated program and attach the combined program.

I hope this arrangement makes it easy for me to obtain help here.
Regards.

#include <Keypad.h>
#include <EEPROM.h>
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>


RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"};
const char* mymonth2[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char lasttime[25] = {};
char lastdate;
char day_of_week[4] = {};

String mysecond, myminute, myhour, myhour2, myday, mymonth, myyear, mytime, mydate, am, datetime;

const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = 1, d7 = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


const byte numRows = 4;         //number of rows on the keypad
const byte numCols = 4;         //number of columns on the keypad

//The keymap for the keypad

char keymap[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

char keypressed;

byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3

Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);


void setup ()
{
  lcd.begin(20, 4);

  if (! rtc.begin()) {
    lcd.print("Couldn't find RTC");
    while (1);
  }
}

void stringconversions() { //the RTC does this to obtain the details of time and date from the function which holds them
  DateTime now = rtc.now();

  //Here, the time details are converted to strings to be printed later on
  myhour = String(now.hour(), DEC);
  if (myhour.toInt() > 12) {
    myhour2 = myhour.toInt() - 12;
    myhour2 = String(myhour2);
    am = "PM";
  }
  else if (myhour.toInt() == 0) {
    myhour2 = myhour.toInt() + 12;
    am = "AM";
  }
  else if (myhour.toInt() == 12){
    myhour2 = myhour.toInt();
    am = "PM";
  }
  else {
    myhour2 = myhour;
    am = "AM";
  }
  myminute = String(now.minute(), DEC);
  mysecond = String(now.second(), DEC);

  //Here, the date details are also converted to strings to be printed too
  myday = String(now.day(), DEC);
  mymonth = String(now.month(), DEC);
  mymonth = mymonth2[mymonth.toInt() + 1];
  myyear = String(now.year(), DEC);
  myyear = String(myyear.toInt() - 2000);

  //Here, the strings for date and time are concatenated together
  mytime = myhour2 + ":" + myminute + ":" + mysecond + "" + am;
  mydate = myday + "/" + mymonth + "/" + myyear;

  datetime = mytime + "-" + mydate;

}


void loop ()
{
  keypressed = myKeypad.getKey();
  //DateTime now = rtc.now();
  //stringconversions();
  //lcd.print(mytime);

  //  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);



  if (keypressed == '0') {
    stringconversions();

    //itt is to increment the address for each character stored in the EEPROM
    int att = 35;//this does the same for the characters in the array being saved
    int itt;
    for (itt = 0; itt < 21; itt++) {
      EEPROM.write(att, datetime[itt]);
      att++;
    }
    lcd.print(mydate);
  }

  if (keypressed == '1') {
    int apx = 0;
    int attt = 35;
    int itt;
    for (itt = 0; itt < 21; itt++) {
      lasttime[apx] = EEPROM.read(attt);
      apx++;
      attt++;
    }

    lcd.clear();
    //lcd.setCursor(0, 0);
    lcd.print(lasttime);

  }
}

Almost_done.ino (23.3 KB)

One mistake is

if (stats = 0) {

'=' is for assignment, '==' is for compare. The are a few like that.

Not an error but you can reduce this

  //EEPROM retrieval of the stored master pin
  EEPROM.get(30, master[0]);
  EEPROM.get(31, master[1]);
  EEPROM.get(32, master[2]);
  EEPROM.get(33, master[3]);

to

  //EEPROM retrieval of the stored master pin
  EEPROM.get(30, master);

Same for code.

This is a personal irritation

short a = 0, i = 0, s = 0, j = 0, v = 0, q = 0; //Variables used later

Where are they used? It's a mission to find most of those variables back in your code; e.g. the letter a occurs 735 times in your code and most of the time it's not the variable.

Especially for global variables, use longer names. E.g. aaa would already be better but a variable name that describes what it actually holds is golden. E.g. a variable with the name i is usually used as a counter. So call it e.g. cnt or counter. a seems to reflect the size of the entered code, so call it e.g. codeSize. j seems to indicate the x position on the screen so call it e.g. lcdPosX.

Thanks a lot.
I used most of those single-letter variables as either counters or flags.
I'd settle to clean up and optimize after I'm done with everything. I feel if I do it now I might begin to mix things up probably because I'm already used to the naming.

Can you post some simplified example code which compiles, runs and demonstrates your issue.

judahben149:
I'd settle to clean up and optimize after I'm done with everything. I feel if I do it now I might begin to mix things up probably because I'm already used to the naming.

The point of using decent names is to help you while you're coding, not to make it pretty for someone to evaluate after the fact.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.