Bouba
August 6, 2015, 3:50pm
1
Hi, I'm finished with my project but in order to learn better and make the code more efficient, I would like to know how I can optimize it. One of the thing that would help the most would be to use some kind of 'dynamic variable', or a variable in a variable... As an exemple, in the function below, I mostly repeat the same code twice:
void readAlarm(int aN = 0) {
switch (aN) {
case 1:
if (alarm_hour1 < 10) {
TimeDisp[0] = tm1637.encodeDigit(0);
TimeDisp[1] = tm1637.encodeDigit(alarm_hour1);
} else {
str_hour = String(alarm_hour1);
TimeDisp[0] = tm1637.encodeDigit(str_hour.charAt(0));
TimeDisp[1] = tm1637.encodeDigit(str_hour.charAt(1));
}
if (alarm_minute1 < 10) {
TimeDisp[2] = tm1637.encodeDigit(0);
TimeDisp[3] = tm1637.encodeDigit(alarm_minute1);
} else {
str_minute = String(alarm_minute1);
TimeDisp[2] = tm1637.encodeDigit(str_minute.charAt(0));
TimeDisp[3] = tm1637.encodeDigit(str_minute.charAt(1));
}
break;
case 2:
if (alarm_hour2 < 10) {
TimeDisp[0] = tm1637.encodeDigit(0);
TimeDisp[1] = tm1637.encodeDigit(alarm_hour2);
} else {
str_hour = String(alarm_hour2);
TimeDisp[0] = tm1637.encodeDigit(str_hour.charAt(0));
TimeDisp[1] = tm1637.encodeDigit(str_hour.charAt(1));
}
if (alarm_minute2 < 10) {
TimeDisp[2] = tm1637.encodeDigit(0);
TimeDisp[3] = tm1637.encodeDigit(alarm_minute2);
} else {
str_minute = String(alarm_minute2);
TimeDisp[2] = tm1637.encodeDigit(str_minute.charAt(0));
TimeDisp[3] = tm1637.encodeDigit(str_minute.charAt(1));
}
break;
}
}
Instead, I would rather want to use some thing like this:
void readAlarm(int aN = 0) {
if (alarm_hour#aN < 10) {
TimeDisp[0] = tm1637.encodeDigit(0);
TimeDisp[1] = tm1637.encodeDigit(alarm_hour#aN);
} else {
str_hour = String(alarm_hour#aN);
TimeDisp[0] = tm1637.encodeDigit(str_hour.charAt(0));
TimeDisp[1] = tm1637.encodeDigit(str_hour.charAt(1));
}
if (alarm_minute#aN < 10) {
TimeDisp[2] = tm1637.encodeDigit(0);
TimeDisp[3] = tm1637.encodeDigit(alarm_minute#aN);
} else {
str_minute = String(alarm_minute#aN);
TimeDisp[2] = tm1637.encodeDigit(str_minute.charAt(0));
TimeDisp[3] = tm1637.encodeDigit(str_minute.charAt(1));
}
}
So I was wondering, is it possible, and if so how is it called and how can I do it? Thank you!
system
August 6, 2015, 4:03pm
2
That is awfully complicated code for something relatively simple.
char time[4];
sprintf(time, "%02d", alarm_hour1);
TimeDisp[0] = tm1637.encodeDigit(time[0]);
TimeDisp[1] = tm1637.encodeDigit(time[1]);
As for repeating the code for different alarm_hourN values, you really should be using an array instead of numbering scalar variables.
What you want to do is not possible because variables have addresses, not names, at run time.
Robin2
August 6, 2015, 8:40pm
3
In a compiled language such as C/C++ the variable names do not exist when the program is running so you cannot write code that is based on dynamic changes to the names. That sort of thing is possible with interpreted languages. Whether it is wise to do so is another matter.
As @PaulS has said the simple and usual solution where you have several similar entities is to use an array.
...R
To do it with arrays:
// int alarm_hour1;
// int alarm_hour2;
int alarm_hour[2];
// int alarm_minute1;
// int alarm_minute2;
int alarm_minute[2];
void readAlarm(int aN = 0) {
if (alarm_hour[aN] < 10) {
TimeDisp[0] = tm1637.encodeDigit(0);
TimeDisp[1] = tm1637.encodeDigit(alarm_hour[aN]);
} else {
str_hour = String(alarm_hour[aN]);
TimeDisp[0] = tm1637.encodeDigit(str_hour.charAt(0));
TimeDisp[1] = tm1637.encodeDigit(str_hour.charAt(1));
}
if (alarm_minute[aN] < 10) {
TimeDisp[2] = tm1637.encodeDigit(0);
TimeDisp[3] = tm1637.encodeDigit(alarm_minute[aN]);
} else {
str_minute = String(alarm_minute[aN]);
TimeDisp[2] = tm1637.encodeDigit(str_minute.charAt(0));
TimeDisp[3] = tm1637.encodeDigit(str_minute.charAt(1));
}
}
NOTE: Array indexes start at 0, not 1, so your two entries will be alarm_xxx[0] and alarm_xxx[1].
Bouba
August 7, 2015, 12:40am
5
Great! Thanks everyone, the array is the ideal solution to lighten the code, as well as easing the addition of more alarms in the future.
Following your advice I came up with this:
void readAlarm(int aN) {
char chr_hour[4];
sprintf(chr_hour, "%02d", alarm_hour[aN]);
TimeDisp[0] = tm1637.encodeDigit(chr_hour[0]);
TimeDisp[1] = tm1637.encodeDigit(chr_hour[1]);
char chr_minute[4];
sprintf(chr_minute, "%02d", alarm_minute[aN]);
TimeDisp[2] = tm1637.encodeDigit(chr_minute[0]);
TimeDisp[3] = tm1637.encodeDigit(chr_minute[1]);
}
and obviously it has also lighten the other parts of code as well
system
August 7, 2015, 12:51am
6
and obviously it has also lighten the other parts of code as we
You can re-use the char array, for both hours and minutes. No need to two arrays. Save 4 bytes.