//x equal to yx == y//x not equal to yx != y//x less than y x < y //x greater than yx > y//x less than or equal to yx <= y//x greater than or equal to yx >= y
//integer, 16 bits, from -32,768 to 32,767int var = 100;//integer, 16 bits, from 0 to 65535 (except in Due, where they are 32 bits)unsigned int var = 100;//integer, 16 bits, from 0 to 65535short var = 100;//integer, 32 bits, from -2,147,483,648 to 2,147,483,647long var = 100000L;//integer, 32bits, from 0 to 4,294,967,295unsigned long var = 100000L;
Floating Point
//floating point, 32 bits, from -3.4028235E+38 to 3.4028235E+38. Precision 6 digitsfloat var = 1.117;//identical to float, except on Arduino Due where it is a 64-bit floatdouble var = 1.117;
Bytes
//8 bits, from 0 to 255byte var = B10010;//16 bits, unsigned, from 0 to 65535word var = 10000;
Characters
//8 bits, from -128 to 127char var = 'A';//8 bits, from 0 to 255unsigned char var = 240;
Conversion between variables
//convert to charchar(variable);//convert to bytebyte(variable);//convert to intint(variable);//convert to wordword(variable);//convert to longlong(variable);//convert to floatfloat(variable);
Variable Qualifiers
//STATIC//Variables visible only inside a function,//and whose value is maintained between calls to it.static int variable;//CONST//Variables whose value cannot be redefined after initializationconst float pi = 3.14;//VOLATILE//Variables in which the compiler is told not to store in the microprocessor's registers,//but to force the update in memory. This is done when there is a possibility that the value of the variable may be//modified by another process that runs concurrently with the current one//(for example when using threads or interrupts)volatile int variable = LOW;
//text string literalString txtMsg = "Hello";//converting a char to StringString txtMsg = String('a');//converting a literal to StringString txtMsg = String("Text");//concatenating two literals to StringString txtMsg = String("text1" + "text2");
do{ // action variable++;} while (variable < 100);
Mathematical Functions
Range Functions
//returns minimum between a and bmin(a,b);//returns maximum between a and bmax(a,b);//returns absolute value of aabs(a);//returns x restricted to (a,b)constrain(x, a, b);//linearly interpolates y between x1,y1 x2,y2map(x, x1, x2, y1, y2);
Exponentiation
//returns a^b (both float type)pow(a,b);//returns square root of asqrt(a);
Random Numbers
//initializes the seed of the pseudo-random number generatorrandomSeed(seed);//returns a random number between a and b (both long type)random(a, b);
Trigonometry
//returns the sine of a (a float type and in radians)sin(a);//returns the cosine of a (a float type and in radians)cos(a);//returns the tangent of a (a float type and in radians)tan(a);
Bit and Byte Functions
//returns the least significant byte of a word or variable.lowByte(variable);//returns the most significant byte of a word//(or the second least significant byte in larger variables)highByte(variable);//returns the nth bit of a variable x //(with bit 0 being the least significant)bitRead(x, n);//writes the nth bit of variable x with value b//(with bit 0 being the least significant)bitWrite(x, n,b );//sets bit n of variable x to 1bitSet(x, n);//clears bit n of variable x to 0bitClear(x, n);//gets the value of bit n (identical to 2^n)bit(n);
Text Functions
//returns the character at position 3 (identical to txtMsg[3];)txtMsg.charAt(3);//replaces the character at position 3 with "A" (identical to txtMsg[3]="A";)txtMsg.setCharAt("A", 3);//concatenates text 1 and text 2 (identical to text1=text1+text2;)text1.concat("text2");//returns the length of the stringtxtMsg.length();//returns the string converted to lowercasetxtMsg.toLowerCase();//returns the string converted to uppercasetxtMsg.toUpperCase();//removes spaces and incorrect characterstxtMsg.trim();//returns the text string as an integertxtMsg.toInt();
Comparison
//compares two strings. Returns 1 if text1 is greater than text2,//0 if they are equal, and -1 otherwise text1.compareTo(text2);//compares if two strings are equal (identical to text1==text2)text1.equals(text2);//compares if two strings are equal, ignoring casetext1.equalsIgnoreCase(text2);
Substrings
//returns a substring from position 3 to 10txtMsg.substring(3, 10);//checks if the string starts with "text", with offset 3txtMsg.startsWith("text", 3);//checks if the string ends with "text"txtMsg.endsWith("text");
Search and Replace
//returns the index of the first occurrence of 'A',//starting from the offset positiontxtMsg.indexOf('A', offset);//returns the index of the last occurrence of 'A'//prior to the offset positiontxtMsg.lastIndexOf('A', offset);//replaces occurrences of "text1" with "text2"txtMsg.replace("text1", "text2");