Arduino Class Resources

Tutorials

Cheat Sheet

  • Variables: Variables are placeholders that hold data. You've seen them in algebra (x =10).

  • Declaring a new variable
    • int myAge = 16; # Declares a new variable of type integer
    • bool amIHappy = true # Declares a new variable of type boolean.
    • unsigned long timeFromStart = 4,294,967,067 # Declares a variable of type long (useful for time measurement, and really long numbers)
      
  • Constants: these are variables that do not change throughout your sketch. For example, if you want to assign a pin number to a variable, this would best be done using a constant. For example
    • const int ledPin = 6
  • void setup(){} # This is the function that gets run only once when the arduino is started or reset.
    • What goes inside setup? Things you only want to happen once! Generally this is pin setup (using pinMode) and getting the Serial port going by setting the data rate.
  • void loop(){} # This code is run over and over and over after setup.
    • The main code for your sketch goes here - this is where your are reading and writing to your pins, and are writing the ongoing logic that is powering your arduino.
    • digitalRead(pin) # reads the pin in the argument - will only return HIGH or LOW
    • analogRead(pin) # reads the pin in analog format - Will return a number from 0-1023
    • digitalwrite(pin, HIGH) # Write to a digital pin. Will only accept HIGH or LOW as it's second argument.
    • analogWrite(pin, 124) # Write to a pwm (~) pin. Will accept a number between 0-255.
  • If statements allow you to have multiple branches of logic. Here's an example:
int x = 15;
if(x > 20){
    digitalWrite(2, HIGH);
} else {
    digitalWrite(2, LOW);
}