[last updated: 2025-05-19]
Arduino home page
(link to:) Arduino Programming Reference
-----
On This Page:
- Programming I/O
- Data Types:
- char
- strings (lower case 's')
- String (upper case 'S')
- Arrays
- int
- short
- long
- unsigned long
- Struct
- Conversion between data types:
- Functions
- Flow Control statements:
- Text & Character manipulations:
- Logic statements:
- Math Operations:
- Capture program code for websites/forums:
- Misc programming concepts:
- eeprom read/write
- wait for keyboard input
- float/int data-type weirdness:
- PWM programming
- switch input - touch sense and debounce
- pointers
-----------------------------------------------------------------------
- Programming I/O:
- Best procedure is to first define a variable for the pin number you're using (in globals):
- Set the pin as input or output (in setup):
pinMode(pinNum, OUTPUT); (or "INPUT", or "INPUT_PULLUP")
-----------------------------------------------------------------------
- Data Types:
- char:
A single byte, representing a character.
Type defined as:
char chr1 = 'a';
A variable defined as char is actually stored as an integer, that is the ASCII value of the character.
In fact, you can define a variable with char data type like this:
char chr1 = 65; In fact this is equivalent to the definition using 'a'.
- strings (lower case 's'):
There is no such thing (in Arduino IDE) as a data type for a "string" (lower case 's').
(c++ does have a "string" data type, which is functionally equivalent to Arduino "String" data type.)
The term "string" is type defined in Arduino IDE as a: char array.
- char array:
char chrAry1[4] = "adf";
Note the "4" specified in the line above is the minimum length for defining the "adf" string,
which contains 3 chars, plus the null char, for 4 total.
-or:
char chrAry1[6] = "adf";
Note the type def length can be greater than the number of chars + null.
-or:
char chrAry1[] = "adf";
-or:
char chrAry1[] = ('a', 'd', 'f');
length of char array:
int i = 0;
while(someArray[i]) {
i++;
}
// At this point, i is the number of items in the array.
...
This is clever (from some forum post), and it works,
but I thinbk it means you cannot use the value 0 as an intended element in the array,
because 0 is treated as the null termination to define the end of the array.
Not completely sure about this, because the char '\0' is what should be used as null termination,
and I have not tested it in depth, but I suspect either '0' or 0, or '\0' will indicate end of array in above code
- String (upper case 'S');
Type defined as:
String str1 = "adf";
- Arrays
eg. an array of Strings:
String paramList[3] = {"lat", "lng", "alt"};
https://forum.arduino.cc/t/a-true-array-of-strings-heres-how-i-did-it-us...
- int (integer):
2 bytes (16 bits)
"signed" values ranging from -32,768 - +32767, stored as "2's complement"
Due and SAMD boards use 4 bytes for int type, with corresponding greater range.
- short:
short is a 16-bit (2 byte) data type
- long:
4 bytes (32 bits)
stores integers in range from -2,147,483,648 to +2,147,483,647
- unsigned long:
32 bits (4 bytes)
"unsigned" = no negative value flag, so all of 32 bits used for data, so
max size = 232 - 1 = 4,294,967,295
(if storing ms from millis(), this is about 50 days)
- Struct - Custom data types:
- Allows you to create a data type that contains multiple parameters, each with its own data type
- Put the definition/configuration into globals like this:
Struct person {
String firstName;
String lastName;
int age;
}
In this example "person" is the name of the struct data type, while firstName, lastName, and age are the elements of person, and each of them is defined with their respective data types (ie. String and int).
- In globals, after you've created the person struct, you can create instances of it like this:
person ID1 (
"John",
"Smith",
47,
}
Or: person ID1 = {"John", "Smith", 47};
- Now you can refer to a given element like this: ID1.firstName or ID1.age.
You can set or retrieve given data elements using that syntax.
-
-
-
- Conversion between data types:
- Convert int to String:
String newString = String(oldInt);
-
----------------------
-----------------------------------------------------------------------
- Functions:
- Flow Control statements:
- Text & Character manipulations:
- Logic statements:
- Math Operations:
- tone() function:
- syntax:
tone(pin, frequency, duration)
- where: pin = int of output pin
freq = Hz as unsigned int (31 Hz minimum)
duration = in ms as unsigned long
- Capture program code for website:
- In IDE, Edit -> Copy for Forum
- Open text doc and paste copied program text into it
- Save text file and upload to server (BlueHost at this time)
- Make link on desired page to open the saved file, with code eg:
<a target="_blank" href="/sites/default/files/files/OLED-UNO-06.ino.txt">OLED-UNO-06.ino</a>
-----------------------------------------------------------------------
- Misc programming concepts:
- eeprom read/write
- wait for keyboard input
- float/int data-type weirdness:
- PWM programming
- switch input - touch sense and debounce
- pointers:
----------------------
.
.
.
eof