Download presentation
Presentation is loading. Please wait.
Published byBrook Lewis Modified over 8 years ago
1
Introduction to Programming the Arduino Dr Gaw 3/21/14
2
What is an Arduino? Arduino is a kind of like Lego for the computer and science crowd. – It has a microcomputer on a semiconductor board to which you attach sensors or switches The sensors include: an accelerometer, a joystick, a gyroscope, … – You then write computer code to control the sensors and do something with the information from the sensor/switch Turn on lights, motors, speakers, … “Arduino is a tool for making computers that can sense and control more of the physical world than your laptop computer”
3
Check it out … Fire up your browser and go to http://playground.arduino.cc/Projects/ArduinoU sers Or … go to Google and do a search for Arduino projects …..
4
C Computer Language The Arduino uses a variant of the C computer language. – The computer language C has been around since the 1970s Developed at Bell Laboratories (AT&T) – Interestingly, the first version of C was called B – Unix was rewritten in C in the 1970s There’s a C shell environment in Unix – Languages like C++ and Java were developed from C
5
More on C C is a complied language – You run your code through a computer program called a complier to make a machine executable version of your code – The complier will let you know if you have made any mistakes in your programing syntax The complier can not check your logic. It just checks the validity of your coding (parameters, spelling, etc) Remember, the computer executes each line of your code sequentially – so we’ll need to think like a computer
6
Arduino Programming Arduino code is called a “sketch” and is written in the supplied IDE (integrated development environment) Some syntax – anything after // on a line is ignored as a comment – braces { } encapsulate blocks – semicolons ; must appear after every command exceptions are conditionals, loop invocations, subroutine titles – every variable used in the program needs to be declared common options are int, float, char, long, unsigned long, void conventionally happens at the top of the program, or within subroutine if confined to { } block – Formatting (spaces, indentation) are irrelevant in C but it is to your great benefit to adopt a rigid, readable format much easier to read if indentation follows consistent rules the IDE will force some structure on your coding
7
Arduino Gotchas Always start your code with a call of the function setup() – This is run once and initializes variables or defines input/output pins on the Arduino – We actually type in void setup() because nothing is returned from the call We then follow with a call of the function loop() – This runs indefinitely and since it returns nothing we type in void loop() The standard arithmetic operators are available – These are: + - / * (plus, minus, divide, multiply) – The = (a single equal sign) is used to assign the value to the right of the equal sign to the variable on the left hand side of the equal sign Comparison operators (used in if statements) > Greater than>= Greater than or equal to < Less than<= Less than or equal to == (two equal signs) equal to!=not equal to Two characters are used for Boolean operators “and” + “or” &&and ||or !(called bang) not
8
Example Sketch in Arduino IDE
9
A Real Bit of Code
10
Arduino Commands Command reference: http://arduino.cc/en/Reference/HomePage http://arduino.cc/en/Reference/HomePage Here is a list of commands: –pinMode (pin, [INPUT | OUTPUT]) –digitalWrite (pin, [LOW | HIGH]) –digitalRead (pin) int –analogWrite (pin, [0…255]) –analogRead (pin) int in range [0..1023] –delay (integer milliseconds) –millis() unsigned long (ms elapsed since reset)
11
And some more … –Serial.begin (baud): in setup ; 9600 is common choice –Serial.print (string): string “example text “ –Serial.print (data): prints data value (default encoding) –Serial.print (data,encoding) encoding is DEC, HEX, OCT, BIN, BYTE for format –Serial.println() : just like print, but CR & LF ( \r\n ) appended –Serial.available() int (how many bytes waiting) –Serial.read() char (one byte of serial buffer) –Serial.flush() : empty out pending serial buffer
12
Defining Variables You must define your variables before you use them in your code – Generally done in the setup()
13
If statements (Conditionals) Used a lot to test whether a condition has been met – Put what you want to happen if the condition is met inside of {} If … else (very powerful)
14
Boolean Logic When using Boolean operators in a conditional statement, you need to know the following truth table (&& is “and” and || is “or”) – Think of A or B as the logical result (true or false) of testing the condition in an if statement, like … if(i>=3) “and” is only true when both conditions are true. “or” is only false when both conditions are false. AB(A && B)(A || B) FFFF FTFT TFFT TTTT
15
Compound Operators Programmers like to keep things simple, so C supports simple ways to combine commonly used operators
16
Functions Sometimes your program repeatedly performs an operation – Instead of writing and copying the code throughout your program, you can create a function and call it – You write the code once and put it in the function (sometimes called a subroutine)
17
Integer Math When working with integers, we often perform mathematical operations. Division can be tricky … Suppose you cast (declare) i and j as integers and set their values to 1 and 8 respectively. What is i/j ?? – We all know it is: 0.125 – But with integer math the result will be zero – If you want it to be 0.125 then you need to float the numbers before the division and to put the result into a variable that has been declared a float
18
Sketch Showing Integer Math
19
Loops Arduinos are a little strange … we actually run the microcomputer in an infinite loop that only stops when you disconnect the power – Infinite loops are generally the worse kind of coding that one can do Sometimes you might want to create a loop within your code, we do this most often with a for loop – But C also has while and do …. while loops
20
For Loop with Integer Math
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.