Use of Structures i...
 
Notifications
Clear all

Use of Structures in the Arduino IDE

5 Posts
5 Users
4 Reactions
909 Views
(@ljbeng)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

It might be useful to educate Arduino programmers on the use of a Structure inside the Arduino IDE especially if they are saving important data to the EEPROM.  You could highlight the use of .PUT and .GET to save and retrieve those structures from EEPROM to RAM.

Here is one I defined recently.

typedef struct gps {
float rlatf;
float rlonf;
float guideHeading;
int baseRox;
int roverGGA;
int roverGST;
int roverGSV;
int roverZDA;
int dirB;
};

gps gpsVar{0.0f, 0.0f, 90.0f,0,0,0,0,0,1};//declare a variable as the gps structure and preset values


gpsVar.dirB = 1;  //simple use of a Structure Variable


EEPROM.put(0, gpsVar); //save the variable to EEPROM


EEPROM.get( 0, gpsVar );//retrieve the variable from EEPROM

   
Quote
(@anibal)
Member
Joined: 5 years ago
Posts: 39
 

Thank you Ljbeng:

Im always interested in coding and structures I know nothing about. I definitely will explore further. So much to learn and apply. 

Best,

Anibal 


   
LJBENG reacted
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2185
 

As a hobby programmer I always found it easier to learn something new by using it in applications like I guess the EEPROM example above.  Before using structures I used to rely on parallel arrays even though I had used structures (which I called records) in Assembler.  In BASIC a structure is called a TYPE.  It is just a way of grouping a block of different data types under a single label where an item in that group is then accessed via the dot notation.

http://www.cplusplus.com/doc/tutorial/structures/

 


   
LJBENG reacted
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 131
 

Also you can have a structure as a member of a structure also. I do stay away from unions though.

I never got comfortable with pointers until I read "C pointers and dynamic memory management"

So once you get a good grasp on C grab on of the more advanced books.


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @ljbeng

It might be useful to educate Arduino programmers on the use of a Structure inside the Arduino IDE especially if they are saving important data to the EEPROM.  You could highlight the use of .PUT and .GET to save and retrieve those structures from EEPROM to RAM.

Here is one I defined recently.

[snip]

Structures, which are POD (Plain Old Data) types in C, are actually considered classes in C++.

These objects are very useful in programming, and we have actually discussed them being used to write to EEPROM (and other things), here in the past.  IMHO, learning how to create and use ones own objects (in any language), opens up an entire new world in ones programming skill-set.  It's very difficult to get people to understand the benefits, but once you know how, you'll never look back.

Cheers.


   
ReplyQuote