Show Menu
Cheatography

Arduino Cheat Sheet by

Basic Arduino functions and their use cases.

Variables

Strings
String variab­leName = "Text in here";
Integers
int variab­leName = 0;
Doubles
double variab­leName = 0.1;
Longs
long variab­leName = 999999999;
Booleans
boolean variab­leName = false;
You can also create empty variables by default by omitting the equals sign.
For example, String variab­leName; will create a new empty String variable.

Arrays

Array that can hold 4 Strings
string[4] array;
Array with pre-set content
string[3] array = {"Ba­nan­a", "­Ora­nge­", "­App­le"}
Access the nth value of an array
array[­n-1];
Assign a value to an array
array[n-1] = "­Pea­r";

Changing Values

Create a new String variable
String variab­leName;
Assign "­Pea­r" to the variable
variab­leName = "­Pea­r";
This method works for other variable types, too. Keep in mind of the data type that you are assigning to a variable, though. You cannot assign "­Pea­r" to an int, for example.
 

While Loops

While loops will repeatedly run the code inside it until the condition is false, in which the loop will stop and the code after it will continue.
while (condi­tion) {
    // code here
}

Functions

Declare a function
void myFunc()
   // code here
}
Declare a function that returns an int
int myFunc(){
   return 1;
}
Declare a function that takes a String parameter
void myFunc­(String param) {
   // code here
}
Store the return value of a function in a variable
int variab­leName = myFunc();
Combine these concepts to create different types of functions that suit your needs. You can have a function that returns an int value with a String parameter, for example. Use void if your function doesn't return anything.
 

Arduino

Set digital pin n to INPUT.
pinMode(n, INPUT);
Set digital pin n to OUTPUT.
pinMode(n, OUTPUT);
Read digital pin n (returns boolean)
digita­lRe­ad(n);
Write to digital pin n (LOW or HIGH)
digita­lWr­ite(n, LOW);
Read value of analog pin A0 (returns int)
analog­Rea­d(A0);

Motor

Create a new motor plugged into port M1
Adafru­it_­DCMotor *myMotor = AFMS.g­etM­oto­r(1);
Set speed of a motor (0-255)
motor-­>se­tSp­eed­(100);
Change motor direction (FORWARD, BACKWARD, RELEASE)
motor-­>ru­n(F­ORW­ARD);

Encoder

Create a new encoder plugged in pins 2 and 3
Encoder myEnco­der(2, 3);
Read encoder (returns long)
Encode­r.r­ead();
Write to encoder
Encode­r.w­rit­e(0);
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Programación en Arduino Cheat Sheet
          arduino Cheat Sheet