1 of 21

Arduino Programming

2 of 21

Arduino IDE

(Integrated Developer Environment)

Download from www.Arduino.cc

Allows you to:

  • Edit code
  • Compile Code
  • Run/upload Code
  • Download Libraries
  • Run example Code

3 of 21

Variables

  • Store Info in Program memory

Problem:

How big is the information?

How much memory must be allocated?

What type of variable?

Data is Stored in memory with Bits

  • Flip-Flops(like a switch)
  • Two states: 1/0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

^ One Byte ^

Reading Binary Numbers:

  • Every bit represents one 2^n

N = 0 1 2 3 4 5 6 7

  • A bit’s state defines 2^n for that bit
  • All bits with 1 state are added
  • All bits with 0 state are ignored

1 + 8 + 16 + 32 + 128

= 185

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255

N = 0 1 2 3 4 5 6 +/-

1 + 2 + 4 + 8 + 16 + 32 + 64 x +/-

= [-128, 127]

Unsigned Bytes

  • All 8 bits can contribute to byte’s magnitude
  • Range [0, 255]

Signed Bytes

  • 7 bits can contribute to byte’s magnitude
  • Range [-128, 127]
  • Default for all data types

4 of 21

The Arduino must know the data type!

Some examples:

  • Byte: [-127, 127] 1 byte
  • Unsigned byte: [0, 255] 1 byte
  • int: [-32768, 32767] 2 byte
  • Unsigned int: [0, 64535] 2 byte
  • (There are sometimes 32 bit ints)
  • float: some gigantic number(this also allows you to store decimals)
  • long: >10x10^20 8 byte

^often depends on processor^

5 of 21

Arrays

  • Grouping of related values (like a list of numbers)
  • Variable declared with number of values (to specify memory usage)
  • Array denoted with {}
  • Values can be accessed with:

value = variable[n];

n = 0 1 2 3 4

Buffer overflow:

  • Writing to values that don’t exist:

number = numbers[100];

  • This will kill your processor or crash your code

6 of 21

Char:

  • One byte
  • Holds a number, but ‘char’ says that it is defined by ASCII Table

ASCII Table:

  • Number given to characters on keyboard
  • 0-255 values

String:

  • Array of chars; each value = char variable
  • Terminated with a null ‘\0’ value

7 of 21

Booleans

  • ‘bool’
  • Can be true or false

8 of 21

The Compiler:

  • Precompiler
    • Compiles the basic stuff
      • #define
  • Compiler
    • Converts code to machine language

Great for checking mistakes!

Declaring v.s. Defining

Declaration

  • Make a variable exist and allocate memory

Definition

  • Give a variable a value

Initialize

  • Declaration + Definition
  • Some fancy stuff idk about (checkout stackoverflow for some try-hards on this)

9 of 21

void setup()

  • Runs once
  • Used to initialize variables + libraries
  • “void” because it doesn’t return anything

void loop()

  • Loops as fast as possible(hardware dependency)

Built-in functions

  • Don’t have to be defined because they are built into arduino init library
  • Usually are very simple functions

10 of 21

If - else if - Else statement

  • Conditional-relies on the state of a variable
  • Condition is defined by operators
  • Condition is in (), function code in {}
  • If code is one line, {} isn’t necessary

While loop

  • Conditional-relies on the state of a variable
  • Condition is defined by operators
  • Loops infinitely until condition = false

For loop

  • Loops for a set amount depending on a variable
  • Will likely require an operator

Break & continue

  • Allow you to control flow of cond. + loops

Switch… case

  • Like an if statement, but neater
  • Allows you to set a default
  • Doesn’t break between cases!!!!!!

11 of 21

Functions

  • Return type must be specified
    • void - returns nothing
    • int -returns an int
    • bool, etc.
  • Is run by calling it
  • Used to simplify complex code-stop handle repetitive tasks

Global Variables

  • Declared at the top of program
  • Accessible over all code (no scope)
  • Can become very confusing, and it’s bad habit to use them in functions!

Local Variables

  • Inside functions
  • Smaller scope (referencing locals outside their scope doesn’t work
  • You can use “static” variable type

12 of 21

Analog

  • Continuously varying signal
  • Life is analog;
  • Computers are digital
    • Bits
  • ADC: analog to digital converter
  • UNO: 10-bit ADC
  • 0-1023 values
  • Use this to read potentiometers!

13 of 21

Libraries

  • A lot of code used to perform specific functions on objects
  • Uses object-oriented programming
  • Use examples! Be lazy!

14 of 21

TinkerCad challenge:

15 of 21

GOAL:

Read 2 potentiometers as analog in values (A0-A5 pins) and print their mapped data (from a value 0-1023 to 0-180) to the lcd display every x milliseconds.

Use functions!

Useful built-in functions:

16 of 21

TinkerCad Challenge Answer:

17 of 21

Structures

  • Allow you to create variables that hold other variables
  • Is a “type” like int/bool/byte/float
  • Arrays of structures allow you to group similar struct variables

Cars

Brand

Price

Color

Car: Roadster

Car: Prius

18 of 21

Pointers

  • All variables have an address in memory - where they are
  • Setting variables equal to others’ addresses makes them “point” to the other’s value
  • Allow you to modify global values with local variables!

19 of 21

I2C (I2C) Protocol

  • Up to 127 devices
  • Master/Slave, Primary/Secondary
  • Devices have identification addresses
  • 2 wires: SDA (data) and SCL (clock)
    • These pins are hardware dependent
  • Clock allows devices to read/write at the same rate
  • Wire.h library in arduino
  • Needs pull-up resistors

PWM protocol

  • Pulse-Width-Modulation (square wave) -not exactly a protocol because all protocols use this
  • Used to interface with motors (most of the time)
  • Requires a PWM pin

SPI protocol

  • Max 4 devices
  • Master/Slave, Primary/Secondary
  • Devices have identification addresses
  • 4 wires: MOSI, MISO, SS/CS, SCK
    • MOSI: Master-out, slave-in
    • MISO: Master-in, slave-out
    • SS/CS: Slave/Chip select
    • SCK: clock

20 of 21

PWM protocol

  • Pulse-Width-Modulation (square wave) -not exactly a protocol because all protocols use this
  • Used to interface with motors (most of the time)
  • Requires a PWM pin

21 of 21

SPI protocol

  • Max 4 devices
  • Master/Slave, Primary/Secondary
  • Devices have identification addresses
  • 4 wires: MOSI, MISO, SS/CS, SCK
    • MOSI: Master-out, slave-in
    • MISO: Master-in, slave-out
    • SS/CS: Slave/Chip select
    • SCK: clock