Cheatography
https://cheatography.com
Basic Arduino functions and their use cases.
Variables
Strings |
String variableName = "Text in here";
|
Integers |
|
Doubles |
double variableName = 0.1;
|
Longs |
long variableName = 999999999;
|
Booleans |
boolean variableName = false;
|
You can also create empty variables by default by omitting the equals sign.
For example, String variableName;
will create a new empty String variable.
Arrays
Array that can hold 4 Strings |
|
Array with pre-set content |
string[3] array = {"Banana", "Orange", "Apple"}
|
Access the nth value of an array |
|
Assign a value to an array |
|
Changing Values
Create a new String variable |
|
Assign "Pear" to the variable |
variableName = "Pear";
|
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 "Pear"
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 (condition) {
// code here
}
Functions
Declare a function |
void myFunc() // code here }
|
Declare a function that returns an int
|
|
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 variableName = 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. |
|
Set digital pin n
to OUTPUT. |
|
Read digital pin n
(returns boolean) |
|
Write to digital pin n
(LOW or HIGH) |
|
Read value of analog pin A0 (returns int) |
|
Motor
Create a new motor plugged into port M1 |
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
|
Set speed of a motor (0-255) |
motor->setSpeed(100);
|
Change motor direction (FORWARD, BACKWARD, RELEASE) |
|
Encoder
Create a new encoder plugged in pins 2 and 3 |
Encoder myEncoder(2, 3);
|
Read encoder (returns long) |
|
Write to encoder |
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets