Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General...
Transcript of Programming, C++, and Arduino · Arduino/C++ Programming and Software Development In General...
Programming, C++, and Arduino
Programming skills + basic social skills =
$$$$$$$
Programming
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.
Arduino/C++ Programming and Software Development In General
There’s two things you have to learn:
1) The basic idea of how programs work.
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).
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.
C++
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
What is C++ all about
-It’s built on top of C and was originally called “C With Classes”. C didn’t have classes.
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.
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.
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.
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.
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.
Back to Arduino…
Official Arduino reference page:
https://www.arduino.cc/en/Reference/HomePage
Go to www.arduino.cc > Learning
> Reference
How To Make A Program
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
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).
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.
Syntax
Syntax
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?
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.
SyntaxQ: What can you say about the curly brackets?
SyntaxQ: What can you say about the curly brackets?
A: There’s always a corresponding close bracket for every open bracket.
SyntaxIn Python you have to indent your code the right way or it fails.
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:
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:
Variables
Variables
A variable is a bowl full of marbles that you program can see and use and add or remove marbles from at will.
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.
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
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
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.
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.
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.
Datatypes
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.
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.
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
✔
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.
Creating vs using variables, example 1creating ledPin
Creating vs using variables, example 1
using or referring to
ledPin
creating ledPin
Creating vs using variables, example 2creating
blinkTime
Creating vs using variables, example 2creating
blinkTime
using or referring to or changing the value of blinkTime
Creating vs using variablesWhere were the variables created in the previous 2 examples?
Creating vs using variablesWhere were the variables created in the previous 2 examples?
at the very top of the program
Creating vs using variablesWhere were the variables created in the previous 2 examples?
NOT inside a function
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..
Functions
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*.
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.
program A program B
Will these do exactly the same thing?
Functions
program A program B
Will these do exactly the same thing? Yes.
Functions
program A program B
Will these do exactly the same thing? Yes.
What’s the point of making a function?
Functions
milliseconds is called an argument. It’s a variable you can put into the function to make it more flexible and useful.
Functions
Conditionals
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.
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.
Conditionals
Does doOtherStuff() happen every loop, regardless of whether the button is pressed or not?
Conditionals
Does doOtherStuff() happen every loop, regardless of whether the button is pressed or not?
Yes, since it’s not in the conditional part.
Conditionals
If the button’s pressed, blink the LED.
Do the doOtherStuff() function, regardless of whether the button’s pressed.
Conditionals
If the button’s pressed, do the blinkLed() function which presumably blinks the LED.
Otherwise, run the turnOffLed() function.
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.
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.
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.
End