Cheatography
https://cheatography.com
Basic Arduino functions and their use cases.
Variables
Strings |
String variableName = "Text in here";
|
Integers |
int variableName = 0;
|
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 |
string[4] array;
|
Array with pre-set content |
string[3] array = {"Banana", "Orange", "Apple"}
|
Access the nth value of an array |
array[n-1];
|
Assign a value to an array |
array[n-1] = "Pear";
|
Changing Values
Create a new String variable |
String variableName;
|
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 |
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 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. |
pinMode(n, INPUT);
|
Set digital pin n to OUTPUT. |
pinMode(n, OUTPUT);
|
Read digital pin n (returns boolean) |
digitalRead(n);
|
Write to digital pin n (LOW or HIGH) |
digitalWrite(n, LOW);
|
Read value of analog pin A0 (returns int) |
analogRead(A0);
|
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) |
motor->run(FORWARD);
|
Encoder
Create a new encoder plugged in pins 2 and 3 |
Encoder myEncoder(2, 3);
|
Read encoder (returns long) |
Encoder.read();
|
Write to encoder |
Encoder.write(0);
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets