Pages: [1]   Go Down
Author Topic: Good Use of *Pointers  (Read 614 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to be aware of memory consumption with variables as the project I'm working on might get quite large. Would anyone like to review a bit of code I've written and comment on whether I'm using pointers correctly, the use of pointers is useful in this situation or a waste of time. To me it seemed more efficient.

Below is the function I'm calling on to get data from certain registers from an I2C device.

Code:
void I2C_ZMDTxRx(int CMD){// I2C COM "CMD" selects operation.
  //local vars
  byte msb, lsb;
  int data;
  Wire.beginTransmission(I2C_TxRxAddr);
  Wire.send(CMD);
  Wire.endTransmission();
  delay(inttime); //I2C Process Time Delay
  Wire.requestFrom(I2C_TxRxAddr,2);
  if (Wire.available() <= 2){  // if two bytes were received
    msb = Wire.receive();  // receive high byte
    lsb = Wire.receive();  // receive low byte
    //combine data
    data = (msb << 8);  // MSB
    data |= (lsb);   // LSB
    //serial display -- Choose what we are printing for serial print line "data" = batch ,serial number, etc..
    if (CMD == BATCH){
      Serial.print("Batch: ");
      Serial.println(data,DEC);
    }
    else if (CMD == SN){
      Serial.print("Serial Number: ");
      Serial.println(data,DEC);
    }
    else if (CMD == MEAS){
      Serial.println(data,DEC);
    }
    else {
      Serial.print("Error: No I2C Communications, Reconnect Sensor Device");
    }
  }
}//END I2C COM

Code with Pointers, does it make sense to use them versus the above code ??

Code:
void I2C_TxRx(int CMD){// I2C COM  "CMD" selects operation.
  //local vars
  byte msb, lsb, *pbyte;
  int data, *pdata = &data;
  Wire.beginTransmission(I2C_TxRxAddr);
  Wire.send(CMD);
  Wire.endTransmission();
  delay(inttime); //Mandatory I2C Process Time Delay
  Wire.requestFrom(I2C_TxRxAddr,2);
  if (Wire.available() <= 2){  // if two bytes were received
     pbyte = &msb;
    *pbyte = Wire.receive();  // receive high byte
     pbyte = &lsb;
    *pbyte = Wire.receive();  // receive low byte
    //combine data
    *pdata = (msb << 8);  // MSB
    *pdata |= (lsb);   // LSB
    //serial display -- Choose what we are printing for serial print line "data" = batch ,serial number, etc..
    if (CMD == BATCH){
      Serial.print("Batch: ");
      Serial.println(data,DEC);
    }
    else if (CMD == SN){
      Serial.print("Serial Number: ");
      Serial.println(data,DEC);
    }
    else if (CMD == MEAS){
      Serial.println(data,DEC);
    }
    else { //should we check for valid data data != 0
      Serial.print("Error: No I2C Communications, Reconnect Sensor Device");
    }
  }
}//END I2C COM

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24320
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're worried about memory consumption, get rid of all the serial print strings and put them in PROGMEM.
Your pointer version does, of course, use more memory.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3402
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your use of pointers looks technically correct in that it would work, but it conveys no advantage in this situation. Indeed, in fact you've consumed extra memory to store the pointer and extra code to point it to the target variable. If you were using the pointer to move through a buffer it might make more sense, but here, it's not doing you any good.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8437
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No reason to use pointers there, you've just added a layer of complexity for no reason, and used more memory.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the input. The good news is I know understand pointers. Now I just need to find a good use for them.
As far as the serial.print calls it's for Debugging  at the moment.
Looks like I'll look into PROGMEM for the future if I'm going to need to output serial data to interface with most likely LABView
« Last Edit: March 11, 2011, 12:12:17 pm by dennisa » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8437
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now I just need to find a good use for them.
You will smiley, most of the problem is in the understanding.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Espoo, Finland
Offline Offline
God Member
*****
Karma: 6
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the topic subject is too good to let this conversations end too quick. For the beginners it is very valuable. I think!?

Last TOO MANY years I have spend my time programming with VB6, because of the need to have our housekeeper-project keep developing. Now it is ready, took only 20 years, started in 1991, with broken refrigerator and QB45, swithed to VisualBasic 6 in 1999.

All that time I didn't need to worried about the usage of the memory, arrays for 72 different objects, resolution of 10s/day was too easy, and no optimization was needed, just working, bugfree code.

Now, finally, with Arduino's environment I see the other side of the world of programming and electronics; the memory space and the language, completely new to me. We design and built all our electronic by ourself, but that was mostly circuits without any internal intelligent. Only microconroller was 8255, which we have two of them in three separate unit to have complete 72 in and out for the application to read and control, and 32 AD channels with only 8-bit resolution. Wow, now that I look at the mess, huge PCB's with the PC to talk and think for them. But it was quite a job for two guys who learn everything by the errors, nothing burned and that was quite nice too.

Back to this day.

Finally the meaning of the pointer is about open to me (sloowlyyyy), it is just an another way to handle directly arrays. And now I'm looking for a source for a simple guy, to learn how to directly write information to the memory, to optimize the code instead of making it more complicated, minimal efforts for CPU, but still stay on C-language. Assembler is still a nightmare, even that our circuits were on the lowest possible level. My brains just don't get it anymore, I don't even understand, how we manage to built that all...

Yes, that writing is hard to read, I can see, but it is morning here, I just woke up, trying to open my eyes and make sure I don't mess with my coffee... damn, it's almost middey already????!!!!

As I said (nobody probably read this far), I have found few links like this: http://duramecho.com/ComputerInformation/WhyCPointers.html , but any good source for dummie is welcome.
Thanks!

Cheers,
Kari
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Pages: [1]   Go Up
Jump to: