Thursday, 18 May 2017

Data Types - Constants vs Variables

Data types are closely associated with memory
All data occupies memory 
Below is a list of many of the data types commonly seen in Arduino. 
The memory size of each is listed in parentheses after the type name.
 

 Constants

The value can't be changed. It's read only. 
 
+ Defining pin levels: HIGH  vs LOW 
   High: voltage greater than 3.0V is present at the pin (5V boards)
   LOW:  a voltage less than 1.5V is present at the pin (5V boards)
 
+ Defining pinModes:
   INPUT vs OUTPUT , INPUT_PULLUP

+ boolean Constants (8 bit) - simple logical true/false.
   false is defined as 0 (zero).
   Any integer which is non-zero is true, in a Boolean sense. 
   So -1, 2 and -200 are all defined as true, too, in a Boolean sense. 
 
+ Floating Point Constants
   eg : n = 0.007; // 0.007 is a floating point constant
          You can also express them in scientific notation.
          thus 2.34E5 = 234000
 
+ Integer Constants 
    eg   n = 345;
           Normally, we use decimal numbers, but other bases can be used like binary or hexadecimal.
 

VARIABLES

These are memory locations which can be changed as many times as you like.
Memory is measured in bits and bytes.
8 bit = 1 byte
Note: signed variables allow both positive and negative numbers, while unsigned variables allow only positive values. 

 
+ boolean (8 bit/1 byte) - simple logical true/false. 
+ byte (8 bit or 1 byte) - unsigned number from 0-255 
+ char (8 bit) - signed number from -128 to 127. 
    The compiler will attempt to interpret this data type as a character (like ‘a’ or ‘!’) 
    in some circumstances.
+  unsigned char (8 bit) - same as the 'byte' data type.
    The unsigned char datatype encodes numbers from 0 to 255. The byte data type is preferred.
+  word (16 bit or 2 bytes) - unsigned number from 0-65535
+  int (16 bit) - signed number from -32768 to 32767. 
    This is most commonly what you see used for general purpose variables in 
     Arduino example code provided with the IDE
 +  unsigned int (16 bit or 2 bytes)- They only store positive values.
    They thus yield a range of 0 to 65,535 ((2^16) - 1)
+  long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
+  unsigned long (32 bit or 4 bytes) - store positive numbers from 0 to 4,294,967,295. 
    The most common usage of this is to store the result of the millis() function, 
    which returns the number of milliseconds the current code has been running 
+  short (16 bit) - range of -32,768 to 32,767
+  float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. 
     Floating point on the Arduino is not native & the compiler has to do complicated math to make it work.
     Avoid if you can.
double (32 bit) - double-precision floating point number.
     can hold very large (or small) numbers. Some of the bits are used to tell 
     where the decimal place goes.  This leaves 6 to 7 digits of precision.
+ string - holds ASCII characters.. that is, it holds text .. 
      you might use this to send a message via a serial port. 
 

Arrays

 
+ Array - This is a list or collection of variables that can be accessed via an index number. 
      The word "array" isnt actually used. The symbols [] and {} do the job.
      eg: int myArray[] = {6,21,34,2,1,0,152}; 
      Here we declared an array with 7 values.  Arduino creates 7 places in memory for these values.
 
     We can also just tell the arduino to create 7 memory spots, and then enter the values later :
      int myArray[7];
 
To assign a value to the second spot we use a command like this:
myArray[1] = 21; 
This is the index number.
The first spot always has an index value of 0 (they are zero indexed).
 
 
 
 
Links:
 

 

 ---------------------------------
-------------------------------------

No comments:

Post a Comment