EMS1EP Lecture 7 Program Structure and Serial I/O Dr. Robert Ross.

Slides:



Advertisements
Similar presentations
Wireless Cue Light Project
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
EMS1EP Lecture 6 Digital Inputs
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
EMS1EP Lecture 8 Pulse Width Modulation (PWM)
EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.
ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
Lab7: Introduction to Arduino
ARDUINO FRAMEWORK.
Procedural programming in Java
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
How to use Arduino By: Andrew Hoffmaster.
Embedded Sumo 1T4 – 1T5 UTRA.
An Introduction to C Adam Gleitman – IAP 2014.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
CS 201 Functions Debzani Deb.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Software design and development Marcus Hunt. Application and limits of procedural programming Procedural programming is a powerful language, typically.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Arduino Part 2 Topics: Serial Communication Programming Constructs: functions, loops and conditionals Digital Input.
Computers in Surveying SVY2301 / E4006 Automated Surveying.
How to use the CCS compiler with the serial port.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
chipKit Sense Switch & Control LED
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Slide 1 Project 1 Task 2 T&N3311 PJ1 Information & Communications Technology HD in Telecommunications and Networking Task 2 Briefing The Design of a Computer.
Code The Arduino Environment.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
Microcontrollers, Microcomputers, and Microprocessors
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Serial Communication RS-232. In order to make two devices communicate, whether they are desktop computers, microcontrollers, or any other form of integrated.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Microcontroller basics Embedded systems for mortals.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Introduction to Arduino A very basic intro to Arduino, the IDE and the Servos class.
Introduction to Programming the Arduino Dr Gaw 3/21/14.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Lab 7 Basic 1: Game of Memory
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Assist. Prof. Rassim Suliyev - SDU 2017
A bit of C programming Lecture 3 Uli Raich.
Arduino & its hardware interfacing
Arduino Programming Part II
Microprocessor Systems Design I
Welcome to Arduino A Microcontroller.
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Fundamentals of Data Representation
Programming 2: The Arduino IDE & First Sketches
C Programming Getting started Variables Basic C operators Conditionals
Fundamental Programming
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Setting up a basic program with Arduino
Presentation transcript:

EMS1EP Lecture 7 Program Structure and Serial I/O Dr. Robert Ross

Overview (what you should learn today) Program structure: – Flow diagrams – Functions – Comments – Libraries for other functionality Serial I/O: – Baud rates – Sending data from Arduino – Receiving data with Arduino Data Types

Program Structure Sometimes people try to write everything in the main loop() function Bad design practice: – OK for very small amounts of code – Hard to read – Difficult to see the flow of code – the big picture

Flow diagrams As programs get more complex it is good to design them on paper first before writing the code – Saves time – Detect errors earlier – Good to refer back to later for quick understanding Flow diagrams are one good design on paper technique for designing algorithms Another way – pseudo code

Flow diagram Start or End Processing Logical Decision

Flow diagram – Flash LED int ledPin = 10; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, LOW);//LED ON delay(1000); // Wait 1S digitalWrite(ledPin, HIGH);// LED OFF delay(1000); // Wait 1S } Setup Pins Set LED ON Delay Set LED OFF Delay

Flow diagram – Button controlled LED Setup Pins Set LED ON Set LED OFF Is button pressed? int ledPin = 10; int buttonPin = 9; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { if (LOW == digitalRead(buttonPin)){ digitalWrite(ledPin, LOW); } else{ digitalWrite(ledPin, HIGH); } }

Flow diagram class exercise Solve the following problem using a flow diagram to develop the algorithm – Create a 3 bit counter – If a button is pressed the counter should count down – If the button isn’t pressed the counter should count up

Functions Functions are blocks of code that can be reused Also called procedures in some languages Typically have a specific function or purpose May be implementations of one of the blocks from the flow diagram Advantages: – Reuse -> reliability – Hides implementation -> Easier to read code – Separates functional blocks

Functions To use functions they need to be called We can pass data to functions (the functions may need this data to compute a result) We can receive data from functions – this is the functions return result Functions are written outside your loop() (normally underneath) and can be called from within the loop or from other functions

Functions General form of a function: return_type function_name(arg_type arg_name,... ) { statements; } return_type : The type of the data that will be returned (eg. char, unsigned int, signed int ect). If the function has no return value, then use: void function_name: What you call your function (eg. dly123) arg_type: The type of data supplied as arguments (eg. int, char). If there are no arguments, then use: void arg_name: The names of the arguments (eg. test_value)

Example Function void flashLED5Times(){ int counter = 5; while(counter > 0){ digitalWrite(LED1,LOW); //LED ON delay(500); digitalWrite(LED1,HIGH); //LED OFF delay(500); counter--; }

Example Function int factorial(int factorial_order){ int factorial_result = 1; while(factorial_order > 0){ factorial_result *= factorial_order; factorial_order--; } return factorial_result; }

Comments Comments are parts of the codes which the compiler ignores Comments are there for the programmers benefit Types of comments: – Inline comments: Start with // and go until the end of the line – Block comments: Start with /* end with */ and may go over multiple lines Use comments to clarify any code which isn’t clear Use a block comment to describe what each of your functions does

Function Comments /*func: flashLED5Times *desc: When called this function flashes an LED 5 times. * For each flash the LED is on for 500ms and off for 500ms *param: no parameters *result: no return value */ void flashLED5Times(){ int counter = 5; while(counter > 0){ digitalWrite(LED1,LOW); //LED ON delay(500); digitalWrite(LED1,HIGH); //LED OFF delay(500); counter--; }

Other libraries People have written lots of libraries to interface with many different devices These are included in the Arduino software and allow for quick programs to be developed Later on we will use a servo library to allow us to interface servo motors with the Ardiuno

Serial Comm: Intro In first lecture brief examples of sending “hello world” to the computer from the Arduino Much more that we can do with it – Sending strings – Sending data or variable values – Receiving characters from the PC

Serial Comms: baud rate Need to choose a baud rate (or data rate) Specifies the amount of bits per second (bps) that will be sent Needs to be the same at both the sender and the receiver Standard values: – 2400, 4800, 9600, 19200

Serial Comms: baud rate Choosing the baud rate will depend on how much data you need to send Slower baud rate tends to be more reliable – less corruption Faster baud – can send more data in fixed time Each time a character (8 bits) is sent a start and stop bit is also sent For 2400baud -> 2400 / (8+2) = 240 characters per second that can be sent (not very many) Often default is 9600 baud

Serial Comms: Arduino -> PC Before serial data can be sent it first needs to be setup One line of code in the setup() function – Serial.begin( ); – baud rate = 4800, 9600, ect. Make sure you set the same baud rate on the PC end

Serial Comms: Arduino -> PC Use Serial.print( ) to send the data to the PC Can be strings or values: Serial.print(“Hello there”); Serial.print(counterValue); Serial.println(“Test123”); //Prints a newline character to go to the next line in the terminal //Function can be overloaded to transmit data in different formats Serial.print(counterValue,DEC); //Decimal Serial.print(counterValue,HEX); //Hex Serial.print(counterValue,OCT); //Octal Serial.print(counterValue,BIN); //Binary

Serial Comms: Arduino -> PC Class Exercise Write code to output the following data to a terminal using a loop and a variable which is decremented: Count Down Timer Count=5 Count=4 Count=3 Count=2 Count=1 Count finished

Serial Comms: Terminal Programs The terminal is where received data can be viewed and sent from on the PC Data is all transmitted over the USB port In the Arduino IDE use the “Serial Monitor”

Serial Comms: Terminal Programs Lots of other terminal programs are around – some of which have more functionality (eg. Strings can be stored and send or encoded for specific protocols) Windows generally comes with HyperTerm A decent free terminal with lots of features is RealTerm

Serial Comms: PC -> Arduino PC can also send data to the Arduino to make it do things If(Serial.available()){ //If there is at least 1 character received char c = Serial.read(); if(‘a’ == c){//If the character was an ‘a’ //Do something }

ASCII – Data format Data sent as ASCII characters If you want to use numbers – need to convert them from ASCII to decimal (subtract 0x30)

Serial Comms: Class exercise Write code to mirror back anything that is typed on the terminal The Arduino should receive the terminal character and send it back again

Data Types Different data types are different sizes C has the following standard data types: These data types can be modified using key words to change their size and the way they store data (how these modify are compiler dependent) Data TypeSize (Bits)Range of Values char8-128 to 127 int to long int to float323.4x to 3.4x double641.7x to 1.7x

Data Type Modifiers: (signed/unsigned) Signed number have both a positive and negative number (for 2’s complement signed numbers the top bit is a signed bit) Unsigned numbers are only positive Eg: – signed char (range -128 to 127) – unsigned char (range: 0 to 255) – signed int (range to ) – unsigned int (range 0 to ) How to calculate: (based on n bits) – signed: range -2 n-1 to (2 n-1 )-1 – unsigned: range 0 to (2 n )-1

Declaring Data types //Characters char char1 = 0; //Same as signed character signed char char2 = 0; //Signed character unsigned char char3 = 0;//Unsigned character //Integers int i1 = 0; //Same as signed integer signed int i2 = 0; //Signed integer unsigned int i3 = 0; //Unsigned integer float f1 = 5.221//For floating point numbers doubled1 = //Large floating point numbers

Summary (What you learnt in this session) Functions are good as they logically break up program functionality and improve reliablity Comments are your friend (along with anyone trying to read your code afterwards) Sending serial data can be useful for debugging and communicating with the PC Lots of different data types available to you – use them carefully