Jump to content

c++ Array handling

hambobolubia

So i made a post yesterday got a little feedback but it didnt solve my problem. im trying to write a sketch for an arduino in witch i have an array of analog inputs witch i want to read all at once and write them to a array of arrays witch i should be able to do in one line to conserve space on the micro heres my code witch i believe works but id like to know how to do it more efficiently

const int SliderPin[3] = {14,15,16};

int CurrentProfile = 0;

int Profile[4][3]={
  {0,0,0},
  {0,0,0},
  {0,0,0},
  {0,0,0}
  };

 


  if (CurrentProfile > 3) {
    CurrentProfile = 0;
    };

  Profile[CurrentProfile][0] = analogRead(SliderPin[0]);
  Profile[CurrentProfile][1] = analogRead(SliderPin[1]);
  Profile[CurrentProfile][2] = analogRead(SliderPin[2]);

Link to comment
Share on other sites

Link to post
Share on other sites

putting things in one line does not make them more efficient, the compiler will extract, inflate, unroll and change everything; to make it more efficient you would need to dive into assembly/op code. You do not have much logic or arithmetic, just basic variable assigning.

 

you can save some overhead by using 3 variables instead of an array for SliderPin. A standard practice is to declare constants in capitals e.g. const int INPUT_SEXTOY = 14; const int INPUT_BUTTPLUG = 15; you won't save much overhead just the overhead of the array index.

 

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

as i suspected the constants for the inputs are already defined, so you do not need to define them: http://electronics.stackexchange.com/questions/67087/analogread0-or-analogreada0

Quote

static const uint8_t A0 = 14;
static const uint8_t A1 = 15;
static const uint8_t A2 = 16;

 

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, hambobolubia said:

i should be able to do in one line to conserve space on the micro

The uncompiled code does not get stored on the device I don't really know why you would think that it would be be case... All you will be doing by trying to compactify it is making things incredibly difficult for anyone to read and understand - including yourself when you come back to it in 6 months time. In short don't do this, it is bad.

3 hours ago, hambobolubia said:

i want to define them because the bigger chips have more analog pins and i may want to change them

I don't really understand what you mean by this. Does the configuration actually change? Are you trying to make some kind of generic layer that is agnostic to the device?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Nuluvius said:

I don't really understand what you mean by this. Does the configuration actually change? Are you trying to make some kind of generic layer that is agnostic to the device?

basically yes and i want it to be easily scaled so in this case im only reading 3 inputs but i would like to simply change a few variables and then read from say 6. even though the uncompiled code doesnt get saved id rather do it once in a few lines of code then have to do it 3 times as a big block each time. 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, hambobolubia said:

So i made a post yesterday got a little feedback but it didnt solve my problem. im trying to write a sketch for an arduino in witch i have an array of analog inputs witch i want to read all at once and write them to a array of arrays witch i should be able to do in one line to conserve space on the micro heres my code witch i believe works but id like to know how to do it more efficiently

 


const int SliderPin[3] = {14,15,16};

int CurrentProfile = 0;

int Profile[4][3]={
  {0,0,0},
  {0,0,0},
  {0,0,0},
  {0,0,0}
  };

 

 


  if (CurrentProfile > 3) {
    CurrentProfile = 0;
    };

  Profile[CurrentProfile][0] = analogRead(SliderPin[0]);
  Profile[CurrentProfile][1] = analogRead(SliderPin[1]);
  Profile[CurrentProfile][2] = analogRead(SliderPin[2]);

 

What you're trying to do is a vector operation which is probably impossible to do on an Arduino simply because it doesn't support doing something like this. Besides, all you're saving here is a handful of bytes of executable code. Unless you are literally strapped for bytes, this isn't something to worry about.

 

If you really want to save lines, even though it really doesn't matter because it compiles into the same code, you could do

CurrentProfile = (CurrentProfile > 3) ? 0 : CurrentProfile;

However, you cannot compress...

  Profile[CurrentProfile][0] = analogRead(SliderPin[0]);
  Profile[CurrentProfile][1] = analogRead(SliderPin[1]);
  Profile[CurrentProfile][2] = analogRead(SliderPin[2]);

...Into a one line operation because doing so is a vector operation.

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/10/2017 at 11:12 AM, M.Yurizaki said:

What you're trying to do is a vector operation which is probably impossible to do on an Arduino simply because it doesn't support doing something like this. Besides, all you're saving here is a handful of bytes of executable code. Unless you are literally strapped for bytes, this isn't something to worry about.

 

If you really want to save lines, even though it really doesn't matter because it compiles into the same code, you could do


CurrentProfile = (CurrentProfile > 3) ? 0 : CurrentProfile;

However, you cannot compress...


  Profile[CurrentProfile][0] = analogRead(SliderPin[0]);
  Profile[CurrentProfile][1] = analogRead(SliderPin[1]);
  Profile[CurrentProfile][2] = analogRead(SliderPin[2]);

...Into a one line operation because doing so is a vector operation.

alright thanks for the info i guess this was more for me to learn if there weas a better way to do things and such thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×