Introduction to programming using Arduino
Jump to navigation
Jump to search
Contents
- 1 Introduction
- 2 How Computers Work
- 3 Arduinos
- 4 Intro to Arduino GUI Software
- 5 The Bare Minimum Arduino Program
- 6 Your First Output: PRINTLN
- 7 Your First Subroutine
- 8 Your First Variable
- 9 Your First Condition: IF THEN
- 10 Your First Else Condition: IF THEN ELSE
- 11 Your First Interactive Input Program
- 12 Your First Interactive (Input/Output) Program
- 13 Debugging Compile Errors
- 14 See Also
Introduction
Requirements
- Laptop computer (Mac OS X, Windows, Linux)
- Arduino microcontroller and USB cable
- Arduino environment installed - Go here for download links Arduino IDE
Overview of this guide
- How a computer (or microcontroller like the Arduino) work
- Human languages versus computer languages
- Input/Output
- Arduino basic program layout
- Compilation and Deployment
- Comments
- Boolean logic
- Logic structure (if) and
- Strings and numbers representation
- Variables and datatypes
- Subroutine and functions
- Libraries
How Computers Work
- Computers process instructions (also called programs or code).
- This code has to be precise.
- Computers can process many instructions very quickly.
- How quickly? Even these little Arduinos can run 16 million instructions every second!
- We will program in a high level language. This is more powerful, but slower. (Still very fast!)
- Memory is where we store instructions and data. There are 3 types of memory.
- Input is where we acquire data
- Output is where we send data or control a device
- Compiling is converting the instructions from human readable text format to machine readable binary
- Uploading is loading this binary code into the Arduino
- Some programming terms:
Comments Datatypes Boolean logic Strings and numbers representation Variables Logic structure (if) (there are others we will get to as well) Subroutine and functions Libraries
Arduinos
Safety Notice
- Watch out - sharp bits
- Take care when connected - don't short on metal or wires...
- Static care
Intro to Arduino GUI Software
See Arduino IDE for pictures.
- Compiling
- Uploading
- Help - reference
The Bare Minimum Arduino Program
You can find all of these examples inside the Arduino IDE.
- Arduino IDE > File > Examples > Basics > BareMinimum.
This program does... nothing! But it will compile and it will run.
/* * My first program */ void setup() { // put your setup code here, to run once } void loop() { // put your main code here, to run repeatedly }
CONCEPT: Functions:
- SETUP is a function that is always run once
- LOOP is a function that is run over and over
Functions are the same as mathematical functions
- For example: sin(x) is a function that takes a number x and returns the sin value of it.
- The empty parenthesis () means that it does not take any parameters
- VOID means that it does not return anything
CONCEPT: Comments
- Comments are there to help the programmer document (explain) his own code.
- Comments are ignored by the computer.
- Lines block that start with /* and end with */ are multiline comments
- Lines starting with // are comments
CONCEPT: Curly Braces
- The inside of the two { } blocks are the definition of what the functions do.
CONCEPT: Compilation
- Turning your human readable text into machine readable binary code.
CONCEPT: Upload
- Pushing your binary to the Arduino.
Do it!
- Compile and upload the program above. What outputs do you observe?
- Add your name in the top-file multiline comment
- Add an empty function named 'hello'. Add a comment on what you think this function should do
Your First Output: PRINTLN
void setup() { Serial.begin(9600); Serial.println(F("My name is ...")); } void loop() { }
NOTE: Semi-colons ";"
- Every instruction line (calls to functions, definition of variables) end up with a semi-colon (;)
Do it!
- Modify the string to add your own name
- Add a print out of a short description of yourself
Your First Subroutine
void setup() { Serial.begin(9600); output_name(); } void loop() { } void output_name() { Serial.println(F("My name is ...")); }
Do it!
- Add a second function called output_age and call it from loop
- Do two calls to output_name() and one call to output_age()
- What do you observe?
- Move output_name() to inside output_age()
- Is the behavior the same?
- Move the function calls to loop()
- What is the difference in the behavior?
Your First Variable
void setup() { Serial.begin(9600); int age = 21; Serial.print(F("My age is ")); Serial.println(age); } void loop() { }
Do it!
- Modify the age variable to put your own age
Your First Condition: IF THEN
void setup() { Serial.begin(9600); int age = 21; Serial.print(F("My age is ")); Serial.println(age); if(age < 22) { Serial.println(F("I'm young!")); } if(age > 22) { Serial.println(F("I'm old!")); } } void loop() { }
Do it!
- The program given above has a bug. What is it?
- Change age to your age.
- Change the conditions to always print "I'm young!"
- Change the conditions to always print "I'm old!"
- Change the conditions to always print both "I'm young!" and "I'm old!"
Your First Else Condition: IF THEN ELSE
void setup() { Serial.begin(9600); int age = 21; Serial.print(F("My age is ")); Serial.println(age); if(age < 22) { Serial.println(F("I'm young!")); } else { Serial.println(F("I'm old!")); } } void loop() { }
Do it!
- Swap the "I'm young!" string with the "I'm old!" string.
- How do you need to change the condition so it's still valid?
Your First Interactive Input Program
void setup() { Serial.begin(9600); int input = Serial.read(); Serial.println(input); } void loop() { }
Do it!
- Move the code (except "Serial.begin(9600);" to loop()
- What happens?
- Change Serial.println to Serial.print
- What is the difference in the output? Why do we use Serial.println instead of Serial.print?
Your First Interactive (Input/Output) Program
void setup() { Serial.begin(9600); } void loop() { int input = Serial.read(); if(input == 'h') { Serial.println(F("Hello!")); } }
Do it!
- Add your own interactions when pressing other keys of your choice
- Move all your code except for "Serial.begin(9600);" to the loop() function
- What is the difference in the behavior?
Debugging Compile Errors
These exercises help you recognize and fix compile errors. Find the error and fix it.
No Matching Function
void setup() { Serial.begin(); } void loop() { Serial.println(F("My name is ...")); }
Undefined Reference
/* * hello void setup() { Serial.begin(9600); } void loop() { Serial.println(F("This program also does not compile")); } */
Error: Expected ',' or ';' before '}' Token
void setup() { } void loop() { int i = 1 }
Does Not Declare Parameters
void setup() { int i = 1; int j = 2; } void loop() { } void my_function { }
Missing terminating character
void setup() { } void loop() { Serial.println(F("My example program)); } void my_function() { }
See Also
- Introduction to programming using Arduino part 2 - More examples
- Arduino
- Introduction to Arduino
- Idiots Guide to Arduino
- Bonus reading: Von Neumann architecture