Arduino Programming Reference

STRUCTURE

  • void setup()   void loop()

CONTROL STRUCTURES
  • if (x<5){ } else { }
  • for (int i=0 ; i<=255 ; i++) { }
  • while (x<5){ }
  • do { } while (x<5);
  • continue; // go to next in do/ for/ while loop
  • return x; // or 'return;' for voids
  • goto
FURTHER SYNTAX
  • // (single line comment)
  • /* (multi line comment) */
  • #define ledPin 3;
  • #include
GENERAL OPERATORS
  • = (assignment operator)
  • + (addition)
  • - (substraction)
  • * (multiplication)
  • / (division)
  • % (modulo)
  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • && (and)
  • || (or)
  • ! (not) 

COMPOUND OPERATORS
  • ++ (increment)
  • -- (decrement)
  • += (compound addition)
  • -= (compound substraction)
  • *= (compound multiplication)
  • /= (compound division)
  • &= (compound bitwise and)
  • |= (compound bitwise or)
POINTER ACCESS
    • & (reference operator)
    • * (dereference operator)
    CONSTANTS
    • HIGH   LOW
    • INPUT   OUPUT
    • TRUE   FALSE
    • 143 (decimal number)
    • 0173 (octal number)
    • B11011111 (binary 8-bit)
    • 0x7 (hex number)
    • 7U (force unsigned)
    • 10L (force long)
    • 15UL (force long unsigned)
    • 10.0 (force floating point)
    • 2.4e5 (245000)
    DATA TYPES
    • void
    • boolean (0, 1, false, true)
    • char (e.g. 'a' -128 to127 =256 chars)
    • unsigned char (0 to 255)
    • byte (0 to 255)
    • int (-32,768 to 32,767)
    • unsigned int (0 to 65535)
    • word (0 to 65535)
    • long (-2,147,483,648 to 2,147,483,647)
    • unsigned long (0 to 4,294,967,295)
    • float (-3,4028235E+38 to 3,4028235E+38)
    • double (same as float)
    • sizeof(variable) (variable= e.g. int, float, byte) 
    STRINGS
      • char S1[15];
      • char S2[8]={'a','r','d','u','i','n', 'o'};
      • char S3[8]={'a','r','d','u','i','n', 'o','\0'}; (0 null termination)
      • char S4[]="arduino";
      • char S5[8]="arduino";
      • char S6[15]="arduino";
      ARRAYS
      • int myInts[6];
      • int myPins[]={2, 4, 8, 3, 6};
      • int mySensVals[6]={2, 4, -8, 3, 2};
      CONVERSION
      • char()
      • int()
      • long()
      • byte()
      • word()
      • float()
      QUALIFIERS
      • static (perisists between calls)
      • volatile (load the variable from RAM)
      • const (make read-only)
      • progmem (use flash)
      DIGITAL I/O
      • pinMode (pin,INPUT/OUTPUT);
      • digitalWrite (pin, value);
      • int digitalRead (pin);
      ANALOG I/O
      • analogReference (DEFAULT / INTERNAL / EXTERNAL);
      • int analogRead (pin);
      • int analogWrite (pin, value);  (PWM)
      ADVANCED I/O
      • tone (pin, freq);
      • tone (pin, freq, duration);
      • noTone (pin);
      • shiftOut (dataPin, clockPin, MSBFIRST/ LSBFIRST, value); 
      • unsigned long pulseIn (pin, HIGH/ LOW); 
      TIME
      • unsigned long millis();
      • unsigned long micros();
      • delay ();
      MATH

      • min (x, y)  Calculates the minimum of two numbers
      • max (x, y)  Calculates the maximum of two numbers
      • abs (x);  Computes the absolute value of a number
      • constrain (x, minval, maxval)  Constrains a number to be within a range
      • map (val, fromL, fromH, toL, toH)
      • pow (base, exponent)
      • sqrt (x)
      • sin (rad)
      • cos (rad)
      • tan (rad)
      RANDOM NUMBERS

      • randomSeed (seed)
      • long random (max)
      • long random (min, max)
      BITS AND BYTES

      • lowByte()
      • highByte()
      • bitRead (x, bitn)
      • bitWrite (x, bitn, bit)
      • bitSet (x, bitn)
      • bitClear (x, bitn)
      • bit (bitn) (0=LSB & 7=MSB)
      EXTERNAL INTERRUPTS

      • attachInterrupt (interrupt, function, LOW/ CHANGE/ RISING/ FALLING)
      • detachInterrupt (interrupt)
      • interrupts()
      • noInterrupts
      LIBRARIES

      EEPROM
      • EEPROM(#include )
      • byte read (intADDR)
      • byte write (int, ADDR, myByte)
      SERVO

      SOFTWAR SERIAL

      WIRE