Most efficient way of printing different types (from the same array?)
I have a small sketch that reads a wide variety of sensors, which all come down to either an integer or boolean value.
Every loop I want to print them as following:
"variableName; variableValue"
(which would give for example "distanceSenso; 432")
Currently this looks like
int flame, distance, <etc>
bool flame, touch;
void loop(){
noise = analogRead(SOUND_SENSOR);
distance = analogRead(DISTANCE_SENSOR);
..etc..
Serial.print("flame;");
Serial.println(flame);
Serial.print("distance;");
Serial.println(dist);
Serial.print("motion;");
Serial.println(motion);
Serial.print("touch;");
Serial.println(touch);
Serial.print("noise;");
Serial.println(noise);
<etc for many more sensors>
}
This works fine but isn't efficient. I would like to add each variable to an array and simply update the value each loop. This would allow be to use a simple for loop that prints each array element in a Serial.println
.
However, this isn't possible as I'm using both int and bool variables. I could make two separate arrays or forget about the for loop and keep my current approach, but I'm learning to code and would like to know what the most efficient way would be to do this.
So, my question: how could I print each variable in said syntax in the most efficient possible way?
c++ arduino
add a comment |
I have a small sketch that reads a wide variety of sensors, which all come down to either an integer or boolean value.
Every loop I want to print them as following:
"variableName; variableValue"
(which would give for example "distanceSenso; 432")
Currently this looks like
int flame, distance, <etc>
bool flame, touch;
void loop(){
noise = analogRead(SOUND_SENSOR);
distance = analogRead(DISTANCE_SENSOR);
..etc..
Serial.print("flame;");
Serial.println(flame);
Serial.print("distance;");
Serial.println(dist);
Serial.print("motion;");
Serial.println(motion);
Serial.print("touch;");
Serial.println(touch);
Serial.print("noise;");
Serial.println(noise);
<etc for many more sensors>
}
This works fine but isn't efficient. I would like to add each variable to an array and simply update the value each loop. This would allow be to use a simple for loop that prints each array element in a Serial.println
.
However, this isn't possible as I'm using both int and bool variables. I could make two separate arrays or forget about the for loop and keep my current approach, but I'm learning to code and would like to know what the most efficient way would be to do this.
So, my question: how could I print each variable in said syntax in the most efficient possible way?
c++ arduino
isn't efficient
. This is the most efficient method, you can only get slower.
– Kamil Cuk
Nov 21 '18 at 14:17
Does the order of the output matter? Is it okay to output0
for false and1
for true for bool sensors?
– Nominal Animal
Nov 21 '18 at 14:17
1
"This isn't possible as I'm using bothint
andbool
variables." --bool
is an integer type that can be stored in an integer array. What is the problem; do you need to store the value as abool
? Do you need to print the value as"true"
or"false"
forbool
values? More detail needed, and the question needs narrowing with "most efficient possible way". Most efficient in what way? Speed of execution? Lines of code? Memory usage?
– David Bowling
Nov 21 '18 at 14:17
2
What isn't efficient? Even a slowpoke AVR executes thousands of times faster than the RS-232. The real bottleneck here is the blocking Ardunio API. If it's too slow for your purposes, then maybe pick a MCU from this millennium, maybe even one with DMA, then write the code yourself.
– Lundin
Nov 21 '18 at 15:01
add a comment |
I have a small sketch that reads a wide variety of sensors, which all come down to either an integer or boolean value.
Every loop I want to print them as following:
"variableName; variableValue"
(which would give for example "distanceSenso; 432")
Currently this looks like
int flame, distance, <etc>
bool flame, touch;
void loop(){
noise = analogRead(SOUND_SENSOR);
distance = analogRead(DISTANCE_SENSOR);
..etc..
Serial.print("flame;");
Serial.println(flame);
Serial.print("distance;");
Serial.println(dist);
Serial.print("motion;");
Serial.println(motion);
Serial.print("touch;");
Serial.println(touch);
Serial.print("noise;");
Serial.println(noise);
<etc for many more sensors>
}
This works fine but isn't efficient. I would like to add each variable to an array and simply update the value each loop. This would allow be to use a simple for loop that prints each array element in a Serial.println
.
However, this isn't possible as I'm using both int and bool variables. I could make two separate arrays or forget about the for loop and keep my current approach, but I'm learning to code and would like to know what the most efficient way would be to do this.
So, my question: how could I print each variable in said syntax in the most efficient possible way?
c++ arduino
I have a small sketch that reads a wide variety of sensors, which all come down to either an integer or boolean value.
Every loop I want to print them as following:
"variableName; variableValue"
(which would give for example "distanceSenso; 432")
Currently this looks like
int flame, distance, <etc>
bool flame, touch;
void loop(){
noise = analogRead(SOUND_SENSOR);
distance = analogRead(DISTANCE_SENSOR);
..etc..
Serial.print("flame;");
Serial.println(flame);
Serial.print("distance;");
Serial.println(dist);
Serial.print("motion;");
Serial.println(motion);
Serial.print("touch;");
Serial.println(touch);
Serial.print("noise;");
Serial.println(noise);
<etc for many more sensors>
}
This works fine but isn't efficient. I would like to add each variable to an array and simply update the value each loop. This would allow be to use a simple for loop that prints each array element in a Serial.println
.
However, this isn't possible as I'm using both int and bool variables. I could make two separate arrays or forget about the for loop and keep my current approach, but I'm learning to code and would like to know what the most efficient way would be to do this.
So, my question: how could I print each variable in said syntax in the most efficient possible way?
c++ arduino
c++ arduino
edited Nov 21 '18 at 15:25
gre_gor
4,28092732
4,28092732
asked Nov 21 '18 at 13:27
user3287470user3287470
207
207
isn't efficient
. This is the most efficient method, you can only get slower.
– Kamil Cuk
Nov 21 '18 at 14:17
Does the order of the output matter? Is it okay to output0
for false and1
for true for bool sensors?
– Nominal Animal
Nov 21 '18 at 14:17
1
"This isn't possible as I'm using bothint
andbool
variables." --bool
is an integer type that can be stored in an integer array. What is the problem; do you need to store the value as abool
? Do you need to print the value as"true"
or"false"
forbool
values? More detail needed, and the question needs narrowing with "most efficient possible way". Most efficient in what way? Speed of execution? Lines of code? Memory usage?
– David Bowling
Nov 21 '18 at 14:17
2
What isn't efficient? Even a slowpoke AVR executes thousands of times faster than the RS-232. The real bottleneck here is the blocking Ardunio API. If it's too slow for your purposes, then maybe pick a MCU from this millennium, maybe even one with DMA, then write the code yourself.
– Lundin
Nov 21 '18 at 15:01
add a comment |
isn't efficient
. This is the most efficient method, you can only get slower.
– Kamil Cuk
Nov 21 '18 at 14:17
Does the order of the output matter? Is it okay to output0
for false and1
for true for bool sensors?
– Nominal Animal
Nov 21 '18 at 14:17
1
"This isn't possible as I'm using bothint
andbool
variables." --bool
is an integer type that can be stored in an integer array. What is the problem; do you need to store the value as abool
? Do you need to print the value as"true"
or"false"
forbool
values? More detail needed, and the question needs narrowing with "most efficient possible way". Most efficient in what way? Speed of execution? Lines of code? Memory usage?
– David Bowling
Nov 21 '18 at 14:17
2
What isn't efficient? Even a slowpoke AVR executes thousands of times faster than the RS-232. The real bottleneck here is the blocking Ardunio API. If it's too slow for your purposes, then maybe pick a MCU from this millennium, maybe even one with DMA, then write the code yourself.
– Lundin
Nov 21 '18 at 15:01
isn't efficient
. This is the most efficient method, you can only get slower.– Kamil Cuk
Nov 21 '18 at 14:17
isn't efficient
. This is the most efficient method, you can only get slower.– Kamil Cuk
Nov 21 '18 at 14:17
Does the order of the output matter? Is it okay to output
0
for false and 1
for true for bool sensors?– Nominal Animal
Nov 21 '18 at 14:17
Does the order of the output matter? Is it okay to output
0
for false and 1
for true for bool sensors?– Nominal Animal
Nov 21 '18 at 14:17
1
1
"This isn't possible as I'm using both
int
and bool
variables." -- bool
is an integer type that can be stored in an integer array. What is the problem; do you need to store the value as a bool
? Do you need to print the value as "true"
or "false"
for bool
values? More detail needed, and the question needs narrowing with "most efficient possible way". Most efficient in what way? Speed of execution? Lines of code? Memory usage?– David Bowling
Nov 21 '18 at 14:17
"This isn't possible as I'm using both
int
and bool
variables." -- bool
is an integer type that can be stored in an integer array. What is the problem; do you need to store the value as a bool
? Do you need to print the value as "true"
or "false"
for bool
values? More detail needed, and the question needs narrowing with "most efficient possible way". Most efficient in what way? Speed of execution? Lines of code? Memory usage?– David Bowling
Nov 21 '18 at 14:17
2
2
What isn't efficient? Even a slowpoke AVR executes thousands of times faster than the RS-232. The real bottleneck here is the blocking Ardunio API. If it's too slow for your purposes, then maybe pick a MCU from this millennium, maybe even one with DMA, then write the code yourself.
– Lundin
Nov 21 '18 at 15:01
What isn't efficient? Even a slowpoke AVR executes thousands of times faster than the RS-232. The real bottleneck here is the blocking Ardunio API. If it's too slow for your purposes, then maybe pick a MCU from this millennium, maybe even one with DMA, then write the code yourself.
– Lundin
Nov 21 '18 at 15:01
add a comment |
2 Answers
2
active
oldest
votes
Sequential constructs in code are often the most efficient in terms of speed. If you are looking for improvements in readability (eg, how many lines of code) then looping will help, but likely reduce efficiency in other ways. That said, to reduce number of lines, say in a loop...
Use a combination of enum
, const char
array and struct
. (struct is optional here, but something I often use for readability when working with a large number of members)
I do not have your environment, but for illustration using ANSI C, the following shows how enumerated values can tie string descriptions together with measurement values into the same struct instance, allowing results to be reported in a loop:
enum {
FLAME_SENSOR, // enumerated from 0 to max_sensor
DISTANCE_SENSOR,
MOTION_SENSOR,
TOUCH_SENSOR,
SOUND_SENSOR,
// add more sensors???
MAX_SENSOR
};
typedef struct { // optional struct
int val;
char descr[20];
}PARAM;
const char str[MAX_SENSOR][20] = {"flame","distance","motion","touch","noise"};
//simulation prototype
int analogRead(int type);
int main(void)
{
int i;
PARAM p;
char buf[20];
int result = 0;
for(i=0;i<MAX_SENSOR;i++)
{
p.val = analogRead(i);// for use with struct
result = analogRead(i);//for use without struct
if(p.val /*(or result)*/ == some error)
{
//handle error
}
strcpy(p.descr, str[i]);//for use without struct
sprintf(buf, "%s: %dn", p.descr, p.val);//for use with struct
sprintf(buf, "%s: %dn", str[i], result);//for use without struct
printf(buf);
sleep(10); //10ms delay, For simulation only, to allow clock tick for rand() function
}
return 0;
}
//simple simulation of analog read function
int analogRead(int type)
{
int meas = 0;
srand(clock());
switch(type) {
case FLAME_SENSOR:
// meas = read flame sensor instrument
meas = rand()%10;
break;
case DISTANCE_SENSOR:
// meas = read dist sensor instrument
meas = rand()%10;
break;
case MOTION_SENSOR:
// meas = read motion sensor instrument
meas = rand()%10;
break;
case TOUCH_SENSOR:
// meas = read touch sensor instrument
meas = rand()%10;
break;
case SOUND_SENSOR:
// meas = read sound sensor instrument
meas = rand()%10;
break;
// add more case statements ???
default:
meas = some error
break;
}
return meas;
}
whystrcpy(p.descr, str[i]); printf("%s"... p.descr)
when you can justsprintf("%s" str[i])
?
– Kamil Cuk
Nov 21 '18 at 14:16
@KamilCuk - Ummm. I don't know. Good catch :) (i'll fix that) Thanks.
– ryyker
Nov 21 '18 at 14:18
add a comment |
From arduino reference:
> analogRead()
>
> Returns
> int(0 to 1023)
So you are getting integers anyway.
Usually I create a temporary structure will all the different data and context I need to iterate through. Then a simple loop is sufficient.
struct data_s {
const char *desc;
int pin;
enum data_type_s {
DATA_INT,
DATA_BOOL,
} data_type;
} const datas = {
{ SOUND_SENSOR, "sound", DATA_INT },
{ DISTANCE_SENSOR, "distance", DATA_INT },
{ SOMEBOOLEAN_SENSOR, "someboolean", DATA_BOOL },
... and so on ...
};
for (size_t i = 0; i < sizeof(datas)/sizeof(*datas); ++i) {
Serial.print(data[i].desc);
Serial.print(";");
const int readed_value = analogRead(data[i].pin);
switch (data[i].type) {
case DATA_BOOL:
// custom int->bool conversion
Serial.print(value ? "true" : "false");
break;
case DATA_INT:
Serial.print(value);
break;
}
Serial.print("n");
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413090%2fmost-efficient-way-of-printing-different-types-from-the-same-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sequential constructs in code are often the most efficient in terms of speed. If you are looking for improvements in readability (eg, how many lines of code) then looping will help, but likely reduce efficiency in other ways. That said, to reduce number of lines, say in a loop...
Use a combination of enum
, const char
array and struct
. (struct is optional here, but something I often use for readability when working with a large number of members)
I do not have your environment, but for illustration using ANSI C, the following shows how enumerated values can tie string descriptions together with measurement values into the same struct instance, allowing results to be reported in a loop:
enum {
FLAME_SENSOR, // enumerated from 0 to max_sensor
DISTANCE_SENSOR,
MOTION_SENSOR,
TOUCH_SENSOR,
SOUND_SENSOR,
// add more sensors???
MAX_SENSOR
};
typedef struct { // optional struct
int val;
char descr[20];
}PARAM;
const char str[MAX_SENSOR][20] = {"flame","distance","motion","touch","noise"};
//simulation prototype
int analogRead(int type);
int main(void)
{
int i;
PARAM p;
char buf[20];
int result = 0;
for(i=0;i<MAX_SENSOR;i++)
{
p.val = analogRead(i);// for use with struct
result = analogRead(i);//for use without struct
if(p.val /*(or result)*/ == some error)
{
//handle error
}
strcpy(p.descr, str[i]);//for use without struct
sprintf(buf, "%s: %dn", p.descr, p.val);//for use with struct
sprintf(buf, "%s: %dn", str[i], result);//for use without struct
printf(buf);
sleep(10); //10ms delay, For simulation only, to allow clock tick for rand() function
}
return 0;
}
//simple simulation of analog read function
int analogRead(int type)
{
int meas = 0;
srand(clock());
switch(type) {
case FLAME_SENSOR:
// meas = read flame sensor instrument
meas = rand()%10;
break;
case DISTANCE_SENSOR:
// meas = read dist sensor instrument
meas = rand()%10;
break;
case MOTION_SENSOR:
// meas = read motion sensor instrument
meas = rand()%10;
break;
case TOUCH_SENSOR:
// meas = read touch sensor instrument
meas = rand()%10;
break;
case SOUND_SENSOR:
// meas = read sound sensor instrument
meas = rand()%10;
break;
// add more case statements ???
default:
meas = some error
break;
}
return meas;
}
whystrcpy(p.descr, str[i]); printf("%s"... p.descr)
when you can justsprintf("%s" str[i])
?
– Kamil Cuk
Nov 21 '18 at 14:16
@KamilCuk - Ummm. I don't know. Good catch :) (i'll fix that) Thanks.
– ryyker
Nov 21 '18 at 14:18
add a comment |
Sequential constructs in code are often the most efficient in terms of speed. If you are looking for improvements in readability (eg, how many lines of code) then looping will help, but likely reduce efficiency in other ways. That said, to reduce number of lines, say in a loop...
Use a combination of enum
, const char
array and struct
. (struct is optional here, but something I often use for readability when working with a large number of members)
I do not have your environment, but for illustration using ANSI C, the following shows how enumerated values can tie string descriptions together with measurement values into the same struct instance, allowing results to be reported in a loop:
enum {
FLAME_SENSOR, // enumerated from 0 to max_sensor
DISTANCE_SENSOR,
MOTION_SENSOR,
TOUCH_SENSOR,
SOUND_SENSOR,
// add more sensors???
MAX_SENSOR
};
typedef struct { // optional struct
int val;
char descr[20];
}PARAM;
const char str[MAX_SENSOR][20] = {"flame","distance","motion","touch","noise"};
//simulation prototype
int analogRead(int type);
int main(void)
{
int i;
PARAM p;
char buf[20];
int result = 0;
for(i=0;i<MAX_SENSOR;i++)
{
p.val = analogRead(i);// for use with struct
result = analogRead(i);//for use without struct
if(p.val /*(or result)*/ == some error)
{
//handle error
}
strcpy(p.descr, str[i]);//for use without struct
sprintf(buf, "%s: %dn", p.descr, p.val);//for use with struct
sprintf(buf, "%s: %dn", str[i], result);//for use without struct
printf(buf);
sleep(10); //10ms delay, For simulation only, to allow clock tick for rand() function
}
return 0;
}
//simple simulation of analog read function
int analogRead(int type)
{
int meas = 0;
srand(clock());
switch(type) {
case FLAME_SENSOR:
// meas = read flame sensor instrument
meas = rand()%10;
break;
case DISTANCE_SENSOR:
// meas = read dist sensor instrument
meas = rand()%10;
break;
case MOTION_SENSOR:
// meas = read motion sensor instrument
meas = rand()%10;
break;
case TOUCH_SENSOR:
// meas = read touch sensor instrument
meas = rand()%10;
break;
case SOUND_SENSOR:
// meas = read sound sensor instrument
meas = rand()%10;
break;
// add more case statements ???
default:
meas = some error
break;
}
return meas;
}
whystrcpy(p.descr, str[i]); printf("%s"... p.descr)
when you can justsprintf("%s" str[i])
?
– Kamil Cuk
Nov 21 '18 at 14:16
@KamilCuk - Ummm. I don't know. Good catch :) (i'll fix that) Thanks.
– ryyker
Nov 21 '18 at 14:18
add a comment |
Sequential constructs in code are often the most efficient in terms of speed. If you are looking for improvements in readability (eg, how many lines of code) then looping will help, but likely reduce efficiency in other ways. That said, to reduce number of lines, say in a loop...
Use a combination of enum
, const char
array and struct
. (struct is optional here, but something I often use for readability when working with a large number of members)
I do not have your environment, but for illustration using ANSI C, the following shows how enumerated values can tie string descriptions together with measurement values into the same struct instance, allowing results to be reported in a loop:
enum {
FLAME_SENSOR, // enumerated from 0 to max_sensor
DISTANCE_SENSOR,
MOTION_SENSOR,
TOUCH_SENSOR,
SOUND_SENSOR,
// add more sensors???
MAX_SENSOR
};
typedef struct { // optional struct
int val;
char descr[20];
}PARAM;
const char str[MAX_SENSOR][20] = {"flame","distance","motion","touch","noise"};
//simulation prototype
int analogRead(int type);
int main(void)
{
int i;
PARAM p;
char buf[20];
int result = 0;
for(i=0;i<MAX_SENSOR;i++)
{
p.val = analogRead(i);// for use with struct
result = analogRead(i);//for use without struct
if(p.val /*(or result)*/ == some error)
{
//handle error
}
strcpy(p.descr, str[i]);//for use without struct
sprintf(buf, "%s: %dn", p.descr, p.val);//for use with struct
sprintf(buf, "%s: %dn", str[i], result);//for use without struct
printf(buf);
sleep(10); //10ms delay, For simulation only, to allow clock tick for rand() function
}
return 0;
}
//simple simulation of analog read function
int analogRead(int type)
{
int meas = 0;
srand(clock());
switch(type) {
case FLAME_SENSOR:
// meas = read flame sensor instrument
meas = rand()%10;
break;
case DISTANCE_SENSOR:
// meas = read dist sensor instrument
meas = rand()%10;
break;
case MOTION_SENSOR:
// meas = read motion sensor instrument
meas = rand()%10;
break;
case TOUCH_SENSOR:
// meas = read touch sensor instrument
meas = rand()%10;
break;
case SOUND_SENSOR:
// meas = read sound sensor instrument
meas = rand()%10;
break;
// add more case statements ???
default:
meas = some error
break;
}
return meas;
}
Sequential constructs in code are often the most efficient in terms of speed. If you are looking for improvements in readability (eg, how many lines of code) then looping will help, but likely reduce efficiency in other ways. That said, to reduce number of lines, say in a loop...
Use a combination of enum
, const char
array and struct
. (struct is optional here, but something I often use for readability when working with a large number of members)
I do not have your environment, but for illustration using ANSI C, the following shows how enumerated values can tie string descriptions together with measurement values into the same struct instance, allowing results to be reported in a loop:
enum {
FLAME_SENSOR, // enumerated from 0 to max_sensor
DISTANCE_SENSOR,
MOTION_SENSOR,
TOUCH_SENSOR,
SOUND_SENSOR,
// add more sensors???
MAX_SENSOR
};
typedef struct { // optional struct
int val;
char descr[20];
}PARAM;
const char str[MAX_SENSOR][20] = {"flame","distance","motion","touch","noise"};
//simulation prototype
int analogRead(int type);
int main(void)
{
int i;
PARAM p;
char buf[20];
int result = 0;
for(i=0;i<MAX_SENSOR;i++)
{
p.val = analogRead(i);// for use with struct
result = analogRead(i);//for use without struct
if(p.val /*(or result)*/ == some error)
{
//handle error
}
strcpy(p.descr, str[i]);//for use without struct
sprintf(buf, "%s: %dn", p.descr, p.val);//for use with struct
sprintf(buf, "%s: %dn", str[i], result);//for use without struct
printf(buf);
sleep(10); //10ms delay, For simulation only, to allow clock tick for rand() function
}
return 0;
}
//simple simulation of analog read function
int analogRead(int type)
{
int meas = 0;
srand(clock());
switch(type) {
case FLAME_SENSOR:
// meas = read flame sensor instrument
meas = rand()%10;
break;
case DISTANCE_SENSOR:
// meas = read dist sensor instrument
meas = rand()%10;
break;
case MOTION_SENSOR:
// meas = read motion sensor instrument
meas = rand()%10;
break;
case TOUCH_SENSOR:
// meas = read touch sensor instrument
meas = rand()%10;
break;
case SOUND_SENSOR:
// meas = read sound sensor instrument
meas = rand()%10;
break;
// add more case statements ???
default:
meas = some error
break;
}
return meas;
}
edited Nov 21 '18 at 14:38
answered Nov 21 '18 at 14:01
ryykerryyker
12.6k22959
12.6k22959
whystrcpy(p.descr, str[i]); printf("%s"... p.descr)
when you can justsprintf("%s" str[i])
?
– Kamil Cuk
Nov 21 '18 at 14:16
@KamilCuk - Ummm. I don't know. Good catch :) (i'll fix that) Thanks.
– ryyker
Nov 21 '18 at 14:18
add a comment |
whystrcpy(p.descr, str[i]); printf("%s"... p.descr)
when you can justsprintf("%s" str[i])
?
– Kamil Cuk
Nov 21 '18 at 14:16
@KamilCuk - Ummm. I don't know. Good catch :) (i'll fix that) Thanks.
– ryyker
Nov 21 '18 at 14:18
why
strcpy(p.descr, str[i]); printf("%s"... p.descr)
when you can just sprintf("%s" str[i])
?– Kamil Cuk
Nov 21 '18 at 14:16
why
strcpy(p.descr, str[i]); printf("%s"... p.descr)
when you can just sprintf("%s" str[i])
?– Kamil Cuk
Nov 21 '18 at 14:16
@KamilCuk - Ummm. I don't know. Good catch :) (i'll fix that) Thanks.
– ryyker
Nov 21 '18 at 14:18
@KamilCuk - Ummm. I don't know. Good catch :) (i'll fix that) Thanks.
– ryyker
Nov 21 '18 at 14:18
add a comment |
From arduino reference:
> analogRead()
>
> Returns
> int(0 to 1023)
So you are getting integers anyway.
Usually I create a temporary structure will all the different data and context I need to iterate through. Then a simple loop is sufficient.
struct data_s {
const char *desc;
int pin;
enum data_type_s {
DATA_INT,
DATA_BOOL,
} data_type;
} const datas = {
{ SOUND_SENSOR, "sound", DATA_INT },
{ DISTANCE_SENSOR, "distance", DATA_INT },
{ SOMEBOOLEAN_SENSOR, "someboolean", DATA_BOOL },
... and so on ...
};
for (size_t i = 0; i < sizeof(datas)/sizeof(*datas); ++i) {
Serial.print(data[i].desc);
Serial.print(";");
const int readed_value = analogRead(data[i].pin);
switch (data[i].type) {
case DATA_BOOL:
// custom int->bool conversion
Serial.print(value ? "true" : "false");
break;
case DATA_INT:
Serial.print(value);
break;
}
Serial.print("n");
}
add a comment |
From arduino reference:
> analogRead()
>
> Returns
> int(0 to 1023)
So you are getting integers anyway.
Usually I create a temporary structure will all the different data and context I need to iterate through. Then a simple loop is sufficient.
struct data_s {
const char *desc;
int pin;
enum data_type_s {
DATA_INT,
DATA_BOOL,
} data_type;
} const datas = {
{ SOUND_SENSOR, "sound", DATA_INT },
{ DISTANCE_SENSOR, "distance", DATA_INT },
{ SOMEBOOLEAN_SENSOR, "someboolean", DATA_BOOL },
... and so on ...
};
for (size_t i = 0; i < sizeof(datas)/sizeof(*datas); ++i) {
Serial.print(data[i].desc);
Serial.print(";");
const int readed_value = analogRead(data[i].pin);
switch (data[i].type) {
case DATA_BOOL:
// custom int->bool conversion
Serial.print(value ? "true" : "false");
break;
case DATA_INT:
Serial.print(value);
break;
}
Serial.print("n");
}
add a comment |
From arduino reference:
> analogRead()
>
> Returns
> int(0 to 1023)
So you are getting integers anyway.
Usually I create a temporary structure will all the different data and context I need to iterate through. Then a simple loop is sufficient.
struct data_s {
const char *desc;
int pin;
enum data_type_s {
DATA_INT,
DATA_BOOL,
} data_type;
} const datas = {
{ SOUND_SENSOR, "sound", DATA_INT },
{ DISTANCE_SENSOR, "distance", DATA_INT },
{ SOMEBOOLEAN_SENSOR, "someboolean", DATA_BOOL },
... and so on ...
};
for (size_t i = 0; i < sizeof(datas)/sizeof(*datas); ++i) {
Serial.print(data[i].desc);
Serial.print(";");
const int readed_value = analogRead(data[i].pin);
switch (data[i].type) {
case DATA_BOOL:
// custom int->bool conversion
Serial.print(value ? "true" : "false");
break;
case DATA_INT:
Serial.print(value);
break;
}
Serial.print("n");
}
From arduino reference:
> analogRead()
>
> Returns
> int(0 to 1023)
So you are getting integers anyway.
Usually I create a temporary structure will all the different data and context I need to iterate through. Then a simple loop is sufficient.
struct data_s {
const char *desc;
int pin;
enum data_type_s {
DATA_INT,
DATA_BOOL,
} data_type;
} const datas = {
{ SOUND_SENSOR, "sound", DATA_INT },
{ DISTANCE_SENSOR, "distance", DATA_INT },
{ SOMEBOOLEAN_SENSOR, "someboolean", DATA_BOOL },
... and so on ...
};
for (size_t i = 0; i < sizeof(datas)/sizeof(*datas); ++i) {
Serial.print(data[i].desc);
Serial.print(";");
const int readed_value = analogRead(data[i].pin);
switch (data[i].type) {
case DATA_BOOL:
// custom int->bool conversion
Serial.print(value ? "true" : "false");
break;
case DATA_INT:
Serial.print(value);
break;
}
Serial.print("n");
}
answered Nov 21 '18 at 14:24
Kamil CukKamil Cuk
12.5k1529
12.5k1529
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413090%2fmost-efficient-way-of-printing-different-types-from-the-same-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
isn't efficient
. This is the most efficient method, you can only get slower.– Kamil Cuk
Nov 21 '18 at 14:17
Does the order of the output matter? Is it okay to output
0
for false and1
for true for bool sensors?– Nominal Animal
Nov 21 '18 at 14:17
1
"This isn't possible as I'm using both
int
andbool
variables." --bool
is an integer type that can be stored in an integer array. What is the problem; do you need to store the value as abool
? Do you need to print the value as"true"
or"false"
forbool
values? More detail needed, and the question needs narrowing with "most efficient possible way". Most efficient in what way? Speed of execution? Lines of code? Memory usage?– David Bowling
Nov 21 '18 at 14:17
2
What isn't efficient? Even a slowpoke AVR executes thousands of times faster than the RS-232. The real bottleneck here is the blocking Ardunio API. If it's too slow for your purposes, then maybe pick a MCU from this millennium, maybe even one with DMA, then write the code yourself.
– Lundin
Nov 21 '18 at 15:01