You are on page 1of 14

Programming

Why do we program?
● Computers are very fast, but not very smart -- you have to tell them
exactly what to do
● It’s a good way to practice problem solving
● We want to build interesting things
Programming Languages
There are a ton of programming languages! Each of them has their own
benefits and capabilities.

Scratch - Easy to learn and good for education; very visual.

Python - Looks beautiful, good for data science and scripting.

Java - Very fast, good for building websites and applications.

C++ - Even faster! Can be complicated but can run on very small devices. This
is what we use for Arduino!
Programming for Arduino
The Arduino can take in voltage values from your circuit and process them in a
program. You might build a circuit that outputs a high voltage when an object
is close, or when you hit a button. You can then output a voltage based on the
inputs that you see.

For example, you might be building a remote-controlled car with a distance


sensor. When your Arduino gets too close to a wall, your program can tell it to
stop.
Programming Guide
tinyurl.com/invent-2019-code

This leads to a Google Drive folder containing


Variables
Declaring variables: tell the computer the name of your variable and what
type it is. You can only declare a variable once!

Examples:

int x; // declares that the name x will hold an integer

char ch; // declares that the name ch will hold a character

Notice that these lines end in a semicolon. This is true for almost every line in
C++, so don’t forget to put “;” at the end of each of your lines! Everything after
the “//” on each line is a comment, so the computer won’t try to read it.
Variables
Assigning variables gives a variable name an actual value with a single equals
sign “=”. You have to declare a variable before you can assign it. For example:

int x;
x = 3;
x = 4; // Variables can change values.
int y = 24; // You can declare and assign in the same line
Primitive Types
Name Description

Boolean Booleans can either be true or false; no other value.

Character Represents a single letter, number or symbol.

Integer Holds a positive or negative number, but can’t have decimal points.

Float Just like an integer, but it can use decimal points too. It takes more memory.

Double Just like an integer, but it has the greatest range of values and can do math with
decimal points just like a float. It takes a lot of memory!

Short Just like an integer, but with a restricted range of values. It takes less memory.

Byte Just like an integer, but with a very restricted range of values. It takes very little
memory.

Long Just like an integer, but with a greater range of values. It takes more memory.
Primitive Types
Name Keyword Example Usage

Boolean bool bool x = true;

Character char char x = ‘a’;


char y = ‘?’;

Integer int int x = -234;


int y = 12345678;

Float float float x = 44.23457;


float y = 10 / 3; // Gives about 3.333333334234
because of rounding errors

Double double double x = 123498709814250198273401928473;


Non-Primitive Types
There is also some data that doesn’t fall into these categories. While there is a
whole branch of computer science dealing with this data,* we will only worry
about two special types of data.

Arrays are like lists of one type of data. For example, you could have an array
that holds the integers [2, -4, 124]. You can’t mix data types, so the array
[2, false, ‘c’] would not be possible.

Strings are arrays of characters; you can use this to spell out words. For an
example, the string “San Pedro” would be represented by the array

[‘S’, ‘a’, ‘n’, ‘ ‘, ‘P’, ‘e’, ‘d’, ‘r’, ‘o’]


*It is called “object-oriented programming” if you’re interested.
Non-Primitive Types
How do we create non-primitive types?

Data Type Example of Declaration

String String s = “hello”;

Array int myInts[] = {2, 3, 4}; // one way


int otherInts[3]; // another way
otherInts[0] = 2;
otherInts[1] = 3;
otherInts[2] = 4;
Math
You can do math just like Serial.println(3 + 4); // prints 7

you would with a Serial.println(3 - 4); // prints -1


calculator. Here is some
Serial.println(3 * 4); // prints 12
example code to help you
understand how this Serial.println(3 / 4); // prints 0, by default
numbers without decimals are integers
works.
Serial.println(3.0 / 4.0); // prints 0.75 (you
can do decimal math with doubles and floats)

Serial.println(4 % 3); // prints 1, this gives


the remainder from division

Serial.println((1 + 2) * 3); // prints 9


Equality and Logic bool t = true;
bool f = false;
You can test to see if two
values are equal, getting a Serial.println(t && f); // prints false
Serial.println(t || f); // prints true
boolean value (true or Serial.println(t && (t || f)); // prints true
false) as a result. You can Serial.println(3 == 3); // prints true
Serial.println('a' != 'A'); // prints true
also chain together Serial.println(3 < 4); // prints true
booleans: Serial.println(!(3 < 4)); // prints false
Serial.println(3 > 4); // prints false
Serial.println(3 >= 3); // prints true
A && B is true if both A
and B are true, and false For explanations, see example code linked before. Here are descriptions as well:

otherwise. == is used to check if two items are equal


!= is true if the two items are not equal
! returns true if an item is false, and false if an item is true (it switches them)
A || B is true if either A, B > is true if the first item is greater than the second item
>= is true if the first item is greater than or equal to the second item
or both of them are true. < is true if the first item is less than the second item
<= is true if the first item is less than or equal to the second item
Special Structures
if statements

You might also like