johnwasser:
It looks like you can make your sketch most efficient by using the ternary operator as it was intended:data_buf[0] = (y < 32) ? 1 : 3;
data_buf[1] = (y < 32) ? 2 : 4;
This uses 1946 bytes of PROGMEM where using an 'if' statement OR using the ternary operator as if it were an 'if' statement both use 1968 bytes. You save 22 bytes of PROGMEM!
int y, data_buf[4];
void setup()
{
Serial.begin(115200);
for (y = 30; y < 34; y++)
{
#define FORM 3
#if FORM == 1
// Use an IF statement
if (y < 32)
{
data_buf[0] = 1;
data_buf[1] = 2;
}
else
{
data_buf[0] = 3;
data_buf[1] = 4;
}
#elif FORM == 2
// Proper use of ternary operator
data_buf[0] = (y < 32) ? 1 : 3;
data_buf[1] = (y < 32) ? 2 : 4;
#else
// Use ternary operator like an IF statement and use
// comma operators and parens like semicolons and brackets
(y < 32) ? (data_buf[0] = 1, data_buf[1] = 2)
: (data_buf[0] = 3, data_buf[1] = 4);
#endif
data_buf[2] = 5;
data_buf[3] = 6;
Serial.print(y);
Serial.print(" ");
for (int i = 0; i < 4; i++)
{
Serial.print(data_buf[i]);
Serial.print(" ");
}
Serial.println();
}
}
void loop() {}
Thank you so much for clearing different versions of this task.
Also pointing that using ternary operator is ok in this regard, one of my goals in this thread is to know how the programming community think of my pieces of code that I do once and a while
this one turned up to be ok and also there's saving in memory, so that's a plus too !