Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Similar documents
Review of the syntax and use of Arduino functions, with special attention to the setup and loop functions.

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Robotics and Electronics Unit 5

These are notes for the third lecture; if statements and loops.

Counter & LED (LED Blink)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Text Input and Conditionals

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch

C++ Reference NYU Digital Electronics Lab Fall 2016

CS1 Lecture 3 Jan. 22, 2018

Maciej Sobieraj. Lecture 1

How to approach a computational problem

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

CS1 Lecture 3 Jan. 18, 2019

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Procedure: Determine the polarity of the LED. Use the following image to help:

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Intro. Scheme Basics. scm> 5 5. scm>

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Microcontrollers and Interfacing week 8 exercises

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Section 1.1 Definitions and Properties

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

REPETITION CONTROL STRUCTURE LOGO

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

C++ Support Classes (Data and Variables)

CS50 Supersection (for those less comfortable)

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Flow Control: Branches and loops

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

LECTURE 5 Control Structures Part 2

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

MITOCW watch?v=kz7jjltq9r4

CHAPTER 6 DECISIONS AND REPETITIONS: BASIC ACTIVITIES IN PROGRAMS

CS102: Variables and Expressions

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Loop structures and booleans

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Variables. Data Types.

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

3 The L oop Control Structure

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

If Statements, For Loops, Functions

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

6.096 Introduction to C++

Lecture Transcript While and Do While Statements in C++

Civil Engineering Computation

Welcome! COMP s1. Programming Fundamentals

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CTEC 1802 Embedded Programming Labs

Procedures, Parameters, Values and Variables. Steven R. Bagley

Section 0.3 The Order of Operations

Smart Objects. SAPIENZA Università di Roma, M.Sc. in Product Design Fabio Patrizi

Introduction to the Java Basics: Control Flow Statements

T H E I N T E R A C T I V E S H E L L

Week - 01 Lecture - 04 Downloading and installing Python

Variables and literals

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Math Modeling in Java: An S-I Compartment Model

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Variables, expressions and statements

Flow Control. CSC215 Lecture

LOOPS. Repetition using the while statement

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

Properties and Definitions

printf( Please enter another number: ); scanf( %d, &num2);

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Expressions and Casting

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC

Basic Programming Language Syntax

The Big Python Guide

CMSC 201 Computer Science I for Majors

AN INTRODUCTION PROGRAMMING. Simon Long

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Add Subtract Multiply Divide

Lecture. Loops && Booleans. Richard E Sarkis CSC 161: The Art of Programming

C++ Programming Lecture 1 Software Engineering Group

Visual C# Instructor s Manual Table of Contents

Learning to Program with Haiku

Transcription:

Coding Workshop Learning to Program with an Arduino Lecture Notes Table of Contents Programming ntroduction Values Assignment Arithmetic Control Tests f Blocks For Blocks Functions Arduino Main Functions Arduino Hardware Functions

How? Programming doesn t have to be scary, in fact much easier than French or Spanish! With just a few keywords, should be able to write many simple programs Here s an example of a simple program: print("hello, World!") t consists of a function, print( ),and a value, "Hello, World!" ; we will come back to these later t is written in a popular and fairly easy language, python; We encourage you to learn this if you gain an interest in coding! How? The Arduino uses a slightly stricter language, a dialect of C++ The patterns you learn here will in fact work in many languages ( C-like languages, which unfortunately does not include python) Don t worry if you don t take everything in first time round! We have provided a cheat sheet, and there are some exercises for you to try which should help you put these concepts into practice Hopefully you will come to realise that writing programs is just breaking down tasks into simple instructions so that a stupid computer can repeat them; n reality you do not need to know much to write any program, as computers do not understand many di erent instructions, and only a very few are actually necessary

C++ Values Values are items of data, and in C++ it is quite important to explicitly distinguish between di erent types of data Most important are numbers; there are two main types: Whole numbers, known as int ; e.g. 3, -4 Decimal numbers, known as float ; e.g. 3.0, 2.71 We will also need truth values, known as bool, i.e. true and false n an earlier example you also met textual data; we shouldn t need it here but we refer to single letters/numbers/etc as char, e.g. %, and blocks of text as char[], e.g. "Hello, World!" Assignment Believe it or not, you re almost ready to program! Firstly, it will be nice to be able to remember values; we do this through assignment: Let s analyse a simple example, int x = 7; First note that we had to declare what type of value x is; Computers are a bit stupid, and so sometimes we have to help them along a bit This is also why we include a semicolon; Though languages can di er in the way they re written, most have punctuation to indicate the end of an instruction or statement We can now use x to refer to 7 whenever we like! We can also change x at any time, in this way the = sign di ers from maths x = 42; note that we have now changed the value of x, also note that the computer has already been told that x is a whole number, and it doesn t like to be told twice!

Manipulating Values Computers excel at arithmetic and decisions, and almost all computation is simple combinations of these Arithmetic is achieved as simply as using the following four symbols: +, -, *, /, with parentheses where appropriate! Computers are quite strict, and dividing whole numbers will give you the quotient, you can get the remainder with % Continuing on with our example, let s try to find the product of 7 and 6: int x = 7; int y = 6; x = x * y; You may wonder whether the last line makes sense... n algebra this would mean y =1orx = 0, but here the = sign means assignment, so the computer first works out the answer on the right, and then assigns it to the name on the left Conditions and Control The last thing we need to be able to write any program is the ability to make decisions We will consider two decision making structures or blocks: if and for (there are others but they don t really do anything else) if lets you decide whether to run a block of code (set of statements) based on a previous result, for example if x is even; it is often accompanied by else which runs a di erent block otherwise for lets you run a block of code multiple times, for every time that something is true, though it is easier to think of as doing something for every number in a given range

How to Decide? equality == x == 4 is x 4? inequality < > x > 4 is x more than 4? <= >= x <= 4 is x less than or equal to 4? not!!( x > 4) is x not greater than 4?!= x!= 4 is x not 4? You can replace x above with any expression, for example to test for evenness: (x % 2) == 0 tests if the remainder of dividing x by 2 is 0 You can also save the truth of these tests to a bool variable: bool is_x_even = ((x % 2) == 0); if Blocks if (x == 42) { float y = 3.14159; The above shows the structure of an if block Try to identify the parts discussed earlier; notice the condition and the subsequent code block code blocks are punctuated by curly braces We can now try to put together what we ve learned and include the else structure int x = 37; if ((x % 2) == 0) { x = x / 2; else { x = 3*x + 1;

for Blocks for ( int i = 0; i < 5; i = i + 1) { x = x + i; The for block is a little more complicated, let s try to understand it: Like in the if block, we have some parentheses and a code block; the parentheses are more complicated though and contain exactly three statements The result is that we add the numbers 0, 1, 2, 3, 4 to x one-by-one by iterating over the values 0 apple i < 5 The first says that we want a whole number i starting at 0; the second says we should run the code block as long as i < 5; the third says that after each step we should increase i by 1 You may see a common abbreviation for the third expression, i++,toincrement i ;andsimilarly, i-- to decrement it Congratulations! Though it may not seem like it, you can now write any computer program imaginable! There are some more tricks to be learned to write e cient and concise code, and we will expose you to one of them in the next slide (though you need not make use of it yourself today) To test your understanding, try to identify the purpose of the following code: int x = 1; int n = 10; for ( int i = 1; i <= n; i++) { x = x * i;

Functions n large projects it can become repetitive writing code that does the same thing over and over again, for example calculating powers of numbers For convenience, most programming languages let you define functions or subroutines; these are (almost) just like mathematical functions, and in fact functions like sin do exist! These also let you do special things such as display output ( printf ) to the screen so you don t need to understand hidden complexities like interrupts; unfortunately the arduino doesn t have a screen, but we will be using other output functions to control some LEDs You have seen how functions are called in our first example, you state their name and give them parameters: float x = sin(1.5708); Arduino Functions n C++ all code must actually go in a function; for the Arduino there are two main functions - setup and loop When the Arduino is turned on, it calls setup,runningany code within, for example to setup variables and electrical pin modes After setup, the Arduino then calls loop repeatedly until it loses power Note, you can also add comments to your code using // int x; void setup() { x = 42; void loop() { x--; // decrement x

Arduino Functions There are around 9 very good Arduino functions to know, but for today it is good enough for us to know three of them: pinmode,measure(nput )orset(output ) the voltage digitalwrite, set the voltage to 5 V ( HGH )or0v(low ) delay, stop everything for a given number of milliseconds Here s an example using these to blink an LED attached to pin 13 (and a 1 k resistor!) on and o : int pin = 13; void setup() { pinmode( pin, OUTPUT); void loop() { digitalwrite( pin, HGH); delay(1000); digitalwrite( pin, LOW); delay(1000);