# Problem with my arrays

``````void alarmTimes (){
int alarmhour[12]={0};
int alarmminute[12]={0};

if (dy==1){ //Monday
alarmday=1;
alarmhour[12]={8,8,9,9,10,10,10,11,12,14,15,16};
alarmminute[12]={0,40,0,40,0,20,50,10,0,30,10,10};
}

else if (dy==2){ //Tuesday
alarmday =2;
alarmhour[12]={7,7,8,9,9,10,10,11,11,13,16,17};
alarmminute[12]={0,40,20,0,45,12,13,0,55,0,0,10};
}

else if (dy==3){ //Wednesday
alarmday = 3;
alarmhour[12] ={};
alarmminute[12]={};
}

else if(dy==4){ //Thursday
alarmday = 4;
alarmhour[12] ={};
alarmminute[12]={};
}

else if(dy==5){ //Friday
alarmday = 5;
alarmhour[12] ={};
alarmminute[12]={};
}

else if(dy==6){ //Saturday
alarmday = 6;
alarmhour[12] ={};
alarmminute[12]={};
}

else{
}

if(dy == alarmday){
int km = 0;
if (alarmhour[km]==hr&&alarmminute[km]==alarmminute){
ringalarm();}
km++;
}

}
``````

exit status 1
cannot convert ‘’ to ‘int’ in assignment

I am trying to declare arrays with 12 objects, then change their values inside a function.
I have only populated the times for Monday and Tuesday.
It’s for an alarm clock that supports different alarm times during different days.

You can't do

``````  int alarmhour[12]={0};
int alarmminute[12]={0};
``````

and later do

``````alarmhour[12]={8,8,9,9,10,10,10,11,12,14,15,16};
``````

initialization of the array only works at declaration time (the compiler does the work for you to set up the memory in the right way)

so either you do

``````// let the compiler calculate the size of the array based on the declaration
int alarmhour[]={8,8,9,9,10,10,10,11,12,14,15,16};
int alarmminute[]={0,40,0,40,0,20,50,10,0,30,10,10};
``````

at declaration time

or you need to set them up manually such as

``````  int alarmhour[12];
int alarmminute[12];
``````

and you initialize them later

``````...
alarmhour[0] = 8;
alarmhour[1] = 8;
....
``````

Assigning multiple values when declaring an array is possible. But it is not possible to do that runtime.

This is code that works.

``````int alarmhour[12]={1, 100, 10, 5, 50, 500, 5000, 3, 30, 300 };

alarmhour[0] = 8;
alarmhour[1] = 7;
alarmhour[2] = 15;
alarmhour[3] = 18;
...
``````

You could use pointers to constant lists, or with array indexes for the different days. What about packing everything in an array ?

``````// alarm for hour and minutes.
// alarm[][][1] for minutes
// alarm[][][0] for hours
// alarm[][?][] for the 12 alarms for each day.
// alarm[?][][] for days

const int alarm[7][12][2] =
{
// day 0
{
{ 8, 0}, { 8, 40}, { 9, 0}, { 9, 40}, ....
},

// day 1
{
{ 7, 0}, { 7, 40}, { 8, 20}, { 9, 0}, ....
...
};
``````

You have to find something that you feel comfortable with. If you don't understand it, or don't like it, then find an other solution.

const int alarm[7][12][12]

of course that lets you declare all your week stuff in one go but uses more memory (which you could reduce in half because hours, minutes and days are less than 255 so you can safely use bytes instead of int)

Cool, I didn’t think of that :

``````const byte alarm[7][12][2] =
...
``````

That is 7122 bytes = 168 bytes.
168 is the number of hours in a week and is the total number of spots in a domino set.

Koepel:
Cool, I didn’t think of that :

``````const byte alarm[7][12][2] =
``````

``````

That is 7*12*2 bytes = 168 bytes.
<sub>168 is the number of hours in a week and is the total number of spots in a domino set.</sub>
``````

actually the minutes should be of size 12 too if I look at the OP intent in his first code

so 71212 = so if using BYTES not INT that’s half the memory of a UNO… but that could be in PROGMEM if defined statically…

if the end user can create its own Alarms, best would be to know how many alarms are supported and record only day of week, hour, minute for the alarms ==> 3 bytes per alarm.

Thank you guys! I used the first method proposed by Koepel and it worked.

Much gratitude.

watch you memory usage!

Could represent each setting in just 2 bytes (word), so for an array of 12 settings = 24 bytes.

Would need to do some bit shifting and use some bit commands. The bit layout for each setting could be

day (5-bit): bits 15-11 hour (5-bit): bits 10-6 min (6-bit): bits 5-0

"day" could be weekday, could also hold each day of the month.

@dlloyd, if the sketch would be rewritten and every alarm would be stored seperately, then that is possible. If also the alarms are fixed, then it could be in PROGMEM as J-M-L wrote.

This is advanced programming, and the code with pgm_read and the pointer to the struct is so ugly !

``````//
// http://forum.arduino.cc/index.php?topic=416173.0
//
// A test to get a bitfield from PROGMEM
// With Arduino IDE 1.6.10 and a Arduino Micro board.
//

struct myAlarm_STRUCT
{
unsigned int day: 5;
unsigned int hour: 5;
unsigned int min: 6;
};

const myAlarm_STRUCT myAlarm[] PROGMEM =
{
{ 0, 8, 0 },
{ 0, 8, 40 },
{ 0, 9, 0 },
{ 0, 9, 40 },
{ 0, 10, 0 },
{ 0, 10, 20 },

{ 1, 7, 0 },
{ 1, 7, 40 },
{ 1, 8, 20 },
{ 1, 9, 0 },
};

void setup()
{
Serial.begin( 9600);
while(!Serial);       // For Leonarod/Micro : wait for serial monitor
Serial.println(F( "Started"));

Serial.print(F( "The size of one alarm element is "));
Serial.print( sizeof( myAlarm_STRUCT));
Serial.println(F( " bytes"));

Serial.print(F( "There are "));
Serial.print( sizeof( myAlarm) / sizeof( myAlarm_STRUCT));
Serial.println(F( " alarm elements"));

for( int i=0; i<sizeof( myAlarm) / sizeof( myAlarm_STRUCT); i++)
{
unsigned int x = pgm_read_word_near( &myAlarm[i]);
myAlarm_STRUCT *p = (myAlarm_STRUCT *) &x;

Serial.print(F( "day="));
Serial.print( p->day);
Serial.print(F( ", hour="));
Serial.print( p->hour);
Serial.print(F( ", min="));
Serial.print( p->min);
Serial.println();
}
}

void loop()
{
}
``````