Programming, C , And Arduino - WordPress

Transcription

Programming,C , and Arduino

Programming skills basic social skills

Programming

Arduino/C Programming andSoftware Development In GeneralThere’s two things you have to learn:1) The basic idea of how programswork.2) Where the semicolons, parentheses,brackets and stuff go*.*Often called syntax.

Arduino/C Programming andSoftware Development In GeneralThere’s two things you have to learn:1) The basic idea of how programswork.

Arduino/C Programming andSoftware Development In GeneralThere’s two things you have to learn:2) Where the semicolons, parentheses,brackets and stuff go (a.k.a syntax).

Arduino/C Programming andSoftware Development In GeneralThere’s two things you have to learn:2) Where the semicolons, parentheses,brackets and stuff go (a.k.a syntax).your program brokeand won’t uploadbecause you forgotto put a semicolonsomewhere.

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 Cprogramming language, created in the early 70salso at Bell Labs.Bell Labs, Murray Hill NJ, 1961The Idea Factory

What is C all about-It’s built on top of C and was originally called “CWith Classes”. C didn’t have classes.

What is C all about-It’s built on top of C and was originally called “CWith Classes”. C didn’t have classes.-Classes are a way of organizing code so thatyou can easily reuse complex and powerful codewith little effort.-“Object Oriented Programming” means usingand designing classes.

What is C all about-Classes are a way of organizing code so thatyou can use other people’s complex andpowerful code without having to understand orsee all of it.

What is C all about-Classes are a way of organizing code so thatyou can use other people’s complex andpowerful code without having to understand orsee all of it.Any time you use any code that lookslike 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 thatyou can use other people’s complex andpowerful code without having to understand orsee all of it.Any time you use any code that lookslike CapitalizedWords followed by a dot,you’re using a class.You’re already using the Serial class.The code that allows you to connect tothe Serial Monitor and print out text isweird and complicated. So the Arduinoteam wrote a Serial class that lets youstart the Serial Monitor withSerial.begin(), and write stuff to it withSerial.println() even if you have nounderstanding of the nuts and bolts ofhow serial communication works.

What is C all about-Classes are a way of organizing code so thatyou can use other people’s complex andpowerful code without having to understand orsee all of it.

Back to Arduino

Official Arduinoreference o to www.arduino.cc Learning Reference

How To Make AProgram

How to make programs that dowhatever you wantThere’s mostly just these fewbuilding blocks (besides obviousthings like numbers) in computerprograms*:1) Variables2) Functions3) Conditionals

How to make programs that dowhatever you wantProgramming is giving instructions to acomputer 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 dowhatever you want1) Variables2) Functions3) ConditionalsEvery programming conceptbeyond these 3 things is just a wayof making it easier to organize andmanage these 3 things.

Syntax

Syntax

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

SyntaxQ: What can you say about the lastcharacter of every line in this code,as with all the other code you’ve seenso far in class?A: Every line ends with either asemicolon, or an open curlybracket, or a close curly bracket.**later on some won’t,and there are other ways toarrange the curly brackets,but for now your code shouldalways follow this rule.

SyntaxQ: What can you say about the curlybrackets?

SyntaxQ: What can you say about the curlybrackets?A: There’s always acorresponding close bracket forevery 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

VariablesA variable is a bowl full of marbles that you program cansee and use and add or remove marbles from at will.

VariablesYou can make your own variables, however many youwant*.*You’ll eventually run out of room on your Teensy if you make many hundreds orthousands of variables. But you probably won’t do that.

VariablesA variable is a bowl full of marbles that your programcan see and use and manipulate at will.You create one like this:datatypenameequals value semicolonDatatype is the number of marbles that fit in thebowl.Name is whatever name you give it.It’s value is the number of marbles in the bowl.

datatypenameequals valueDatatype is the size of the bowl and thus the numberof marbles that fit in the bowl.A list of datatypes that you can use with Arduino andtheir size here: https://www.arduino.cc/en/Reference/HomePage

Datatype is the size of the bowl and thus howmany marbles fit into itA list of datatypes that you can use with Arduino andtheir size here: https://www.arduino.cc/en/Reference/HomePageSome - byte, int, word, long - store integers (nodecimals).Some - double, float - can store numbers withdecimals.Some - array, string - store a list of numbers in a row.Some -string, char - store but they can be writtenand used as strings of text.

DatatypesThere’s only two datatypes you’ll need to know aboutfor now:int and bool1) 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 numbersthan that for a while.

DatatypesThere’s only two datatypes you’ll need to know aboutfor now:int and bool1) 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 NamesThey can’t have spaces.The can have letters, numbers, andunderscores.camelCase: the popular conventionof having first word lowercase, allothers uppercase, if the variablename is more than one word.

Variable NamesThey can’t have spaces.The can have letters, numbers, andunderscores.camelCase: the popular conventionof having first word lowercase, allothers uppercase, if the variablename is more than one word.Always use camelCase.

Variable Names camelCase, standard and easy to readclass names start with capitals so thiswill confuse peopleall lowercase is slightly harder to readthan camelCaseI don’t know why anyone does this

Creating vs using variablesYou can only do 3 things with avariable: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 valueand use it however you want however many times you want.

Creating vs using variables, example 1creatingledPin

Creating vs using variables, example 1creatingledPinusing orreferring toledPin

Creating vs using variables, example 2creatingblinkTime

Creating vs using variables, example 2creatingblinkTimeusing orreferring toor changingthe value ofblinkTime

Creating vs using variablesWhere were the variables created in the previous2 examples?

Creating vs using variablesWhere were the variables created in the previous2 examples?at the very topof the program

Creating vs using variablesWhere were the variables created in the previous2 examples?NOT inside a function

Creating vs using variablesWhere were the variables created in the previous2 examples?NOT inside a functionThis will break yourprogram.Unless you alreadyknow all about why,put all variables at thetop of your code, notinside a function.

Functions

FunctionsThere’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 manyyou want, named whatever you want,however long or short you want*.

Functionsvoid is called the “return type”. If this is new toyou don’t worry about it and just be sure to alwaysinclude it for now.

Functionsprogram Aprogram BWill these do exactly the same thing?

Functionsprogram Aprogram BWill these do exactly the same thing?Yes.

program Aprogram BWill these do exactly the same thing?Yes.What’s the point of making a function?

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

Functions

Conditionals

ConditionalsIf the button’s pressed, do the blinkLed() functionwhich presumably blinks the LED.If it’s not pressed, skip whatever’s in the brackets.

ConditionalsIf the button’s pressed, do whatever’s in the bracketsright after the if statement.So if the button’s pressed, it will presumably blink theLED.And if it’s not pressed, it will skip whatever’s in thebrackets.

ConditionalsDoes doOtherStuff() happen every loop,regardless of whether the button is pressed ornot?

ConditionalsDoes doOtherStuff() happen every loop,regardless of whether the button is pressed ornot?Yes, since it’s not in the conditional part.

ConditionalsIf the button’s pressed, blink the LED.Do the doOtherStuff() function, regardless ofwhether the button’s pressed.

ConditionalsIf the button’s pressed, do the blinkLed() functionwhich presumably blinks the LED.Otherwise, run the turnOffLed() function.

ConditionalsIf 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.

ConditionalsIf the buttonPin is pressed, do the blinkLed() function, and skip any elsesand 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, doblinkLed3(), then skip any else ifs and elses immediately after it.

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

age

End

-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 .