Arduino Coding

From Network Security Wiki
Jump to navigation Jump to search


Data Types[edit]

Void
  • The void keyword is used only in function declarations.
  • It indicates that the function is expected to return no information to the function from which it was called.
Void Loop ( ) {
   // rest of the code
}
Boolean
  • Boolean holds one of two values, true or false.
  • Each Boolean variable occupies one byte of memory.
boolean val = false;
boolean state = true;


Char
  • A data type that takes up one byte of memory that stores a character value.
  • Character literals are written in single quotes like: 'A'
  • For multiple characters, strings use double quotes: "ABC".
  • However, characters are stored as numbers.
  • You can see the specific encoding in the ASCII chart.
  • This means that it is possible to do arithmetic operations on characters, in which the ASCII value of the character is used.
  • For example, 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65.
Char chr_a = ‘a’;
Char chr_a = 97;
Unsigned char
  • Unsigned char is an unsigned data type that occupies one byte of memory.
  • The unsigned char data type encodes numbers from 0 to 255.
unsigned char chr_y = 121;
Byte
  • A byte stores an 8-bit unsigned number, from 0 to 255.
byte m = 25;
Int
  • Integers are the primary data-type for number storage.
  • Int stores a 16-bit (2-byte) value.
  • This yields a range of -32,768 to 32,767.
  • Minimum value of -2^15 and a maximum value of (2^15)-1.
  • The int size varies from board to board.
int counter = 32;
Unsigned int
  • Unsigned integers are the same as int in the way that they store a 2 byte value.
  • Instead of storing negative numbers they only store positive values
  • Yielding a useful range of 0 to 65,535 (2^16) - 1).
unsigned int counter = 60;
Word
  • A word stores a 16-bit unsigned number.
word w = 1000;
Long
  • Long variables are extended size variables for number storage, and store 32 bits (4 bytes).
  • From -2,147,483,648 to 2,147,483,647.
long velocity = 102346;


Unsigned long
  • Unsigned long variables are extended size variables for number storage and store 32 bits (4 bytes).
  • They will not store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).
unsigned long velocity = 101006;
Short
  • A short is a 16-bit data-type.
  • This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).
short val = 13;
Float
  • Data type for floating-point number is a number that has a decimal point.
  • Floating-point numbers are often used to approximate the analog and continuous values because they have greater resolution than integers.
  • Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38.
  • They are stored as 32 bits (4 bytes) of information.
float num = 1.352;
Double
  • Double precision floating-point number occupies four bytes.

That is, the double implementation is exactly the same as the float, with no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision.

double num = 45.352;
Array
String-char array
String-object

Variables & Constants[edit]

   Paste.png     This section is under construction.

Operators[edit]

   Paste.png     This section is under construction.

Control Statements[edit]

If Statements[edit]

Form 1
if (expression)
   statement;
Form 2

You can use the if statement without braces { } if you have one statement.

if (expression) {
   Block of statements;
}

If Else Statement[edit]

if (expression) {
   Block of statements;
}
else {
   Block of statements;
}

If Else-If Else Statements[edit]

if (expression_1) {
   Block of statements;
}

else if(expression_2) {
   Block of statements;
}
.
.
.

else {
   Block of statements;
}

Switch Case Statement[edit]

switch (variable) { 
   case label:
   // statements
   break;
}

case label: { 
   // statements
   break;
}

default: { 
   // statements
   break;
}

Loops[edit]

   Paste.png     This section is under construction.

Functions[edit]

   Paste.png     This section is under construction.

Strings[edit]

   Paste.png     This section is under construction.

String Objects[edit]

   Paste.png     This section is under construction.


References





blog comments powered by Disqus