Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO ROBOTICS Part 5: Programming

Similar presentations


Presentation on theme: "INTRODUCTION TO ROBOTICS Part 5: Programming"— Presentation transcript:

1 INTRODUCTION TO ROBOTICS Part 5: Programming
Robotics and Automation

2 Copyright © Texas Education Agency, 2013. All rights reserved.
Program Requirements Programming requires 1. Something that will run the program A computer A microprocessor A microcontroller (what we will use!) 2. A computer program that lets a user write a computer program RobotC Another requirement is a way to deliver (download) the program from the PC to the device. This is usually some type of cable, but the connection can also be wireless (Bluetooth/WiFi). Copyright © Texas Education Agency, All rights reserved.

3 What is a Microcontroller?
A microcontroller is an entire computer on a chip. RAM, ROM, I/O, port controllers, converters, timers, and CPU are integrated as a single system. A microprocessor is a general purpose, multi-function device, but its primary job is to execute instructions. It will run many different programs that will seem to be running all at the same time. A microcontroller has many hardware parts built in, but is designed to run only one program at a time. It will not run more than one program. If you focus on how many functions are built into a microcontroller you might think it would be more powerful and more expensive than a microprocessor, but it is actually much simpler and much less expensive. Many microcontrollers use old and obsolete microprocessors as the CPU. Consider the fact that new microprocessors run at 3 GHz, while a newer one runs at 16 MHz. Converters include analog to digital and digital to analog.

4 Copyright © Texas Education Agency, 2013. All rights reserved.
A Program void setup() {   // put your setup code here, to run once; } void loop() {   // put your main code here, to run repeatedly;  } The program is shown as an example of the proper format and syntax. It is a starting point. We are now going to go through a process of modifying this program to make it read a switch in a robot. This program has every required element needed to run in a microcontroller, with proper syntax and formatting. Copyright © Texas Education Agency, All rights reserved.

5 Sample Program These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

6 Copyright © Texas Education Agency, 2013. All rights reserved.
Program Parts This is an example from an IDE. Normal C++ has only one required function, the main () function. However, the main function does not show the important concept that programs have different parts that do different things and can be written in stages. The first thing to notice is the program has two parts A part that runs once A part that runs over and over again Copyright © Texas Education Agency, All rights reserved.

7 Copyright © Texas Education Agency, 2013. All rights reserved.
Syntax The next thing to notice is the use of formatting symbols in the program. These symbols form what is called the syntax, which is the language the program uses. Copyright © Texas Education Agency, All rights reserved.

8 Copyright © Texas Education Agency, 2013. All rights reserved.
Syntax Each symbol has a particular meaning. Some of the symbols are required for the program to be compiled and executed correctly. Others define parts of a program such as statements and functions. Copyright © Texas Education Agency, All rights reserved.

9 Comments Best Coding Practices includes comments to describe what each section of the code does in easy to understand English terms.

10 It has only become worse as far as normal people knowing technical details about how their devices and technology actually work. It is better in terms of our quality of life and how these devices have made our lives easier and better.

11 These general concepts are from Coding Horror, by Jeff Atwood
These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

12 Variables In software, a holder whose value can be changed by the program itself is called a VARIABLE. A variable must be defined before you can use the variable (example: to store or read data), during initialization, before the main program loop.

13 These general concepts are from Coding Horror, by Jeff Atwood
These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

14 These general concepts are from Coding Horror, by Jeff Atwood
These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

15 These general concepts are from Coding Horror, by Jeff Atwood
These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

16 Syntax In order statement to be executed, it must end in a semicolon (;).

17 These general concepts are from Coding Horror, by Jeff Atwood
These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

18 Copyright © Texas Education Agency, 2013. All rights reserved.
Symbols Braces { }, or curly brackets, go around executable program sections called functions. Both an opening brace (or left brace) and a closing brace (or right brace) are needed. The semicolon ( ; ) ends a program statement or command. An easy way to get a syntax error is to forget to use a semicolon after a command. There are also square braces: [ ]. Square braces perform a different function and are not used the same way as curly brackets, so we do not typically use the term “braces” without being more specific. A statement is usually only one line of a program and a function is usually many lines (or statements). Copyright © Texas Education Agency, All rights reserved.

19 Functions An instruction within a larger computer program is a called a FUNCTION. The parenthesis () after each instruction is required and tells the program that the name used is a function and not something else.

20 These general concepts are from Coding Horror, by Jeff Atwood
These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

21 Argument A value that you pass to a routine is a called a ARGUMENT.

22 These general concepts are from Coding Horror, by Jeff Atwood
These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

23 Example Program These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from

24 Copyright © Texas Education Agency, 2013. All rights reserved.
Example Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); This is a program that can read a switch value (open or closed). Later, we will want to use that value to make a decision. Int is a keyword that means store a piece of data as 16 binary bits. switchpin and switchdata are the names we made up for those pieces of data (we are defining the data pin to read as pin 7 and assigning the switchdata initial value as 0). pinmode is a keyword. GetDigitalInput is an abstraction (and purely imaginary). The equals sign reads (or assigns) the value of pin 7 (which can only be high or low, 1 or 0) to the data location called switch data. Copyright © Texas Education Agency, All rights reserved.

25 Copyright © Texas Education Agency, 2013. All rights reserved.
Program Loops A program (or program segment) that runs the same section of code over and over Necessary when waiting for an input while performing other tasks like generating video while waiting for user input, driving around until bumping into an object, or performing a particular task only when a specific condition is present. Most programs involve one or more loops A microcontroller will execute thousands of instructions every second. The commands inside the loop (to read data and then perform an action based on that data) can range from only a few commands to hundreds, but when using a loop once and the list of commands have been executed, the program loops back to the beginning and runs the same loop again, over and over as long as the program runs. For example, if a microcontroller can execute 20,000 instructions per second (a conservative estimate), and the loop instructions, which include reading the sensor, has 100 statements, that means the loop will run 200 times per second, and the sensor will be read (and a decision made) 200 times per second. Performing a “print” function is an example of a program loop (subroutine) that is run only when asked (hit the print button). Copyright © Texas Education Agency, All rights reserved.

26 Copyright © Texas Education Agency, 2013. All rights reserved.
Types of Loops Infinite loops Usually the main program loop Can also be the result of a programming error Example: while Counting loops Performs an action a specified number of times Example: for Conditional loops Performs a specific action as a result of a specific input Example: if, else These are just general examples. Specific examples depend on the programming language. Copyright © Texas Education Agency, All rights reserved.

27 These are comments (not executed)
Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); These are comments (not executed) Comments can use the double slash at the beginning of some text, but the comment only lasts for the rest of that line. If you want a comment to cover more than one line, you have to use a different symbol: /* to start the comment and */ to end the comment. This works in HTML also. Do not put any statements between these because they will not be executed. Copyright © Texas Education Agency, All rights reserved.

28 Copyright © Texas Education Agency, 2013. All rights reserved.
Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); These are statements GetDigitalInput is an abstraction. Other terms are either defined here or are keywords. Copyright © Texas Education Agency, All rights reserved.

29 Statements are computer commands terminated with a semicolon ( ; )
Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Statements are computer commands terminated with a semicolon ( ; ) Copyright © Texas Education Agency, All rights reserved.

30 These are called functions
Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); These are called functions Copyright © Texas Education Agency, All rights reserved.

31 Functions are enclosed in brackets
Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Functions are enclosed in brackets These are called curly brackets to distinguish them from square brackets. Copyright © Texas Education Agency, All rights reserved.

32 Functions are enclosed in brackets
Improved Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Functions are enclosed in brackets Copyright © Texas Education Agency, All rights reserved.

33 Do the stuff in this function once
Improved Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Do the stuff in this function once Copyright © Texas Education Agency, All rights reserved.

34 Do the stuff in this function over and over again
Improved Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Do the stuff in this function over and over again Copyright © Texas Education Agency, All rights reserved.


Download ppt "INTRODUCTION TO ROBOTICS Part 5: Programming"

Similar presentations


Ads by Google