+ All Categories
Home > Documents > Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General...

Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General...

Date post: 16-May-2020
Category:
Upload: others
View: 10 times
Download: 0 times
Share this document with a friend
74
Programming, C++, and Arduino
Transcript
Page 1: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Programming, C++, and Arduino

Page 2: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Programming skills + basic social skills =

$$$$$$$

Page 3: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Programming

Page 4: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Arduino/C++ Programming and Software Development In General

There’s two things you have to learn:

1) The basic idea of how programs work.

2) Where the semicolons, parentheses, brackets and stuff go*. *Often called syntax.

Page 5: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Arduino/C++ Programming and Software Development In General

There’s two things you have to learn:

1) The basic idea of how programs work.

Page 6: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Arduino/C++ Programming and Software Development In General

There’s two things you have to learn:

2) Where the semicolons, parentheses, brackets and stuff go (a.k.a syntax).

Page 7: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Arduino/C++ Programming and Software Development In General

There’s two things you have to learn:

2) Where the semicolons, parentheses, brackets and stuff go (a.k.a syntax).

your program broke and won’t upload because you forgot to put a semicolon somewhere.

Page 8: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

C++

Page 9: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

What is C++ all about

-Programming language created in the late 70s/early 80s at Bell Labs. -Created as an extension of the popular C programming language, created in the early 70s also at Bell Labs.

Bell Labs, Murray Hill NJ, 1961 The Idea Factory

Page 10: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

What is C++ all about

-It’s built on top of C and was originally called “C With Classes”. C didn’t have classes.

Page 11: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

What is C++ all about

-It’s built on top of C and was originally called “C With Classes”. C didn’t have classes.

-Classes are a way of organizing code so that you can easily reuse complex and powerful code with little effort.

-“Object Oriented Programming” means using and designing classes.

Page 12: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

What is C++ all about

-Classes are a way of organizing code so that you can use other people’s complex and powerful code without having to understand or see all of it.

Page 13: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

What is C++ all about

-Classes are a way of organizing code so that you can use other people’s complex and powerful code without having to understand or see all of it. Any time you use any code that looks

like CapitalizedWords followed by a dot, you’re using a class.

You’re already using the Serial class.

Page 14: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

What is C++ all about

-Classes are a way of organizing code so that you can use other people’s complex and powerful code without having to understand or see all of it. Any time you use any code that looks

like CapitalizedWords followed by a dot, you’re using a class.

You’re already using the Serial class.

The code that allows you to connect to the Serial Monitor and print out text is weird and complicated. So the Arduino team wrote a Serial class that lets you start the Serial Monitor with Serial.begin(), and write stuff to it with Serial.println() even if you have no understanding of the nuts and bolts of how serial communication works.

Page 15: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

What is C++ all about

-Classes are a way of organizing code so that you can use other people’s complex and powerful code without having to understand or see all of it.

Page 16: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Back to Arduino…

Page 17: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Official Arduino reference page:

https://www.arduino.cc/en/Reference/HomePage

Go to www.arduino.cc > Learning

> Reference

Page 18: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

How To Make A Program

Page 19: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where
Page 20: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

How to make programs that do whatever you want

There’s mostly just these few building blocks (besides obvious things like numbers) in computer programs*:

1) Variables 2) Functions 3) Conditionals

Page 21: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

How to make programs that do whatever you want

Programming is giving instructions to a computer telling it what to do.

In those instructions,

variables are the nouns and adjectives,

functions are the verbs,

conditionals are conjunctions (words like if, when, while).

Page 22: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

How to make programs that do whatever you want

1) Variables 2) Functions 3) Conditionals

Every programming concept beyond these 3 things is just a way of making it easier to organize and manage these 3 things.

Page 23: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Syntax

Page 24: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Syntax

Page 25: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

SyntaxQ: What can you say about the last character of every line in this code, as with all the other code you’ve seen so far in class?

Page 26: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

SyntaxQ: What can you say about the last character of every line in this code, as with all the other code you’ve seen so far in class?

A: Every line ends with either a semicolon, or an open curly bracket, or a close curly bracket.*

*later on some won’t,

and there are other ways to arrange the curly brackets,

but for now your code should always follow this rule.

Page 27: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

SyntaxQ: What can you say about the curly brackets?

Page 28: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

SyntaxQ: What can you say about the curly brackets?

A: There’s always a corresponding close bracket for every open bracket.

Page 29: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

SyntaxIn Python you have to indent your code the right way or it fails.

Page 30: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

SyntaxIn Python you have to indent your code the right way or it fails.

In C++ you don’t have to, this mess will actually work:

Page 31: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

SyntaxIn Python you have to indent your code the right way or it fails.

In C++ you don’t have to, this mess will actually work:

But you should always make everything easy to read anyway:

Page 32: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Variables

Page 33: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Variables

A variable is a bowl full of marbles that you program can see and use and add or remove marbles from at will.

Page 34: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

VariablesYou can make your own variables, however many you want*.

*You’ll eventually run out of room on your Teensy if you make many hundreds or thousands of variables. But you probably won’t do that.

Page 35: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Variables

A variable is a bowl full of marbles that your program can see and use and manipulate at will.

You create one like this:

datatype name valueequals

Datatype is the number of marbles that fit in the bowl. Name is whatever name you give it. It’s value is the number of marbles in the bowl.

semicolon

Page 36: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

datatype name valueequals

Datatype is the size of the bowl and thus the number of marbles that fit in the bowl.

A list of datatypes that you can use with Arduino and their size here: https://www.arduino.cc/en/Reference/HomePage

Page 37: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Datatype is the size of the bowl and thus how many marbles fit into it

A list of datatypes that you can use with Arduino and their size here: https://www.arduino.cc/en/Reference/HomePage

Some - byte, int, word, long - store integers (no decimals).

Some - double, float - can store numbers with decimals.

Some - array, string - store a list of numbers in a row.

Some -string, char - store but they can be written and used as strings of text.

Page 38: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

DatatypesThere’s only two datatypes you’ll need to know about for now:

int and bool 1) An int can be set to a positive or negative integer. No decimals. An int can be anywhere from about -32,000 to 32,000. You won’t need bigger numbers than that for a while.

Page 39: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

DatatypesThere’s only two datatypes you’ll need to know about for now:

int and bool 1) A bool can only be set to 0 or 1.

true and HIGH are built in variables that equal 1. false and LOW are built in variables that equal 0. So you can use whichever makes the most sense.

Page 40: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Datatypes

Page 41: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Variable Names

They can’t have spaces. The can have letters, numbers, and underscores.

camelCase: the popular convention of having first word lowercase, all others uppercase, if the variable name is more than one word.

Page 42: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Variable Names

They can’t have spaces. The can have letters, numbers, and underscores.

camelCase: the popular convention of having first word lowercase, all others uppercase, if the variable name is more than one word. Always use camelCase.

Page 43: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Variable Names

camelCase, standard and easy to read

class names start with capitals so this will confuse people

all lowercase is slightly harder to read than camelCase

I don’t know why anyone does this

Page 44: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variables

You can only do 3 things with a variable:1) Create one.

2) Change its value.

3) Stick it somewhere it will do something useful.

Once you create the variable you can change it’s value and use it however you want however many times you want.

Page 45: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variables, example 1creating ledPin

Page 46: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variables, example 1

using or referring to

ledPin

creating ledPin

Page 47: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variables, example 2creating

blinkTime

Page 48: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variables, example 2creating

blinkTime

using or referring to or changing the value of blinkTime

Page 49: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variablesWhere were the variables created in the previous 2 examples?

Page 50: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variablesWhere were the variables created in the previous 2 examples?

at the very top of the program

Page 51: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variablesWhere were the variables created in the previous 2 examples?

NOT inside a function

Page 52: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Creating vs using variablesWhere were the variables created in the previous 2 examples?

NOT inside a function

This will break your program.

Unless you already know all about why, put all variables at the top of your code, not inside a function..

Page 53: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Functions

Page 54: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Functions

There’s built in ones that run automatically - setup() and loop().

There’s built in ones you can use - pinMode(), digitalRead(), digitalWrite(), etc.

You can create your own. However many you want, named whatever you want, however long or short you want*.

Page 55: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Functions

void is called the “return type”. If this is new to you don’t worry about it and just be sure to always include it for now.

Page 56: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

program A program B

Will these do exactly the same thing?

Functions

Page 57: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

program A program B

Will these do exactly the same thing? Yes.

Functions

Page 58: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

program A program B

Will these do exactly the same thing? Yes.

What’s the point of making a function?

Page 59: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where
Page 60: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where
Page 61: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Functions

milliseconds is called an argument. It’s a variable you can put into the function to make it more flexible and useful.

Page 62: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Functions

Page 63: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

Page 64: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

If the button’s pressed, do the blinkLed() function which presumably blinks the LED.

If it’s not pressed, skip whatever’s in the brackets.

Page 65: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

If the button’s pressed, do whatever’s in the brackets right after the if statement.

So if the button’s pressed, it will presumably blink the LED.

And if it’s not pressed, it will skip whatever’s in the brackets.

Page 66: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

Does doOtherStuff() happen every loop, regardless of whether the button is pressed or not?

Page 67: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

Does doOtherStuff() happen every loop, regardless of whether the button is pressed or not?

Yes, since it’s not in the conditional part.

Page 68: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

If the button’s pressed, blink the LED.

Do the doOtherStuff() function, regardless of whether the button’s pressed.

Page 69: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

If the button’s pressed, do the blinkLed() function which presumably blinks the LED.

Otherwise, run the turnOffLed() function.

Page 70: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

If the buttonPin is pressed, do the blinkLed() function, and skip any elses and else ifs that are right after it.

If the buttonPin2 is pressed, do the blinkLed2() function.

Page 71: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

If the buttonPin is pressed, do the blinkLed() function, and skip any elses and else ifs that are right after it.

If buttonPin isn’t pressed, see if buttonPin2 is pressed. If it is, do blinkLed2(), then skip any else ifs and elses immediately after it.

If buttonPin2 isn’t pressed, see if buttonPin3 is pressed. If it is, do blinkLed3(), then skip any else ifs and elses immediately after it.

Page 72: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Conditionals

Same as the previous example, but if the first if statement and none of the following else ifs are true, do whats in the else - turn off all LEDs.

Page 73: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

Operators

https://www.arduino.cc/en/Reference/HomePage

Page 74: Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General There’s two things you have to learn: 1) The basic idea of how programs work. 2) Where

End


Recommended