Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Advertisements

ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Microcontroller Fundamentals
Lab7: Introduction to Arduino
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.
Week 4: Control Structures - Repetition
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Finish your programs from last week STOPLIGHT CIRCUIT! You may need … – int – void setup() – void loop() – pinMode – digitalWrite – delay.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Embedded Programming and Robotics
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Lecture 9: Microcontrollers – part 1 BJ Furman 29OCT2012.
chipKit Sense Switch & Control LED
Arduino. What is it? A open-source software suite and single-board microcontroller. Allows easy and affordable prototyping of microcontroller applications.
Franz Duran INTRODUCTION TO A RDUINO PROGRAMMING & INTERFACING Engr. Franz Duran, MEP-ECE RapidSignal Electronics.
Tweaking Your Simon Adding a photoresistor and changing code Instruction by Pete Lewis and Linz Craig.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Suleyman Demirel University CSS340 Microprocessor Systems – Lecture 1 Getting Started to Arduino.
Lecture 7: Microcontrollers & I/O Bryan Burlingame 14 October 2015.
Code The Arduino Environment.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Lecture 10: Peripherals Bryan Burlingame 04 November 2015.
CPS120: Introduction to Computer Science Decision Making in Programs.
Microcontrollers, Microcomputers, and Microprocessors
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Controlling Program Flow with Decision Structures.
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.
Lecture 2: Logic Bryan Burlingame 10 Feb Ref: xkcd:
CPS120: Introduction to Computer Science Decision Making in Programs.
Microcontroller basics Embedded systems for mortals.
Microcontroller basics Embedded systems for mortals.
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
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?
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016.
Arduino.
INTRODUCTION TO ROBOTICS Part 5: Programming
Lecture 3: Logic Bryan Burlingame 06 Sept 2017.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Week 4: Microcontrollers & Flow Control
Assist. Prof. Rassim Suliyev - SDU 2017
Val Manes Department of Math & Computer Science
Microcontroller basics
Arduino & its hardware interfacing
Arduino Programming Part II
UTA010 : Engineering Design – II
Arduino Part 1 Topics: Microcontrollers Programming Basics
INC 161 , CPE 100 Computer Programming
Introduction to Arduinos
Week 5: Microcontrollers
ARDUINO     What is an Arduino? Features 14 Digital I/O pins 6 Analogue inputs 6 PWM pins USB serial 16MHz Clock speed 32KB Flash memory 2KB SRAM.
Week 6: Microcontrollers II
IoT Programming the Particle Photon.
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Arduino Board.
Introduction to Arduino
Lab #1: Getting Started.
Arduino Board.
Arduino म्हणजे काय?.
Lecture 8: Arduino 20 March 2019.
Setting up a basic program with Arduino
Introduction to Arduinos
Introduction to arduino
Presentation transcript:

Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016

Announcements & The Plan for Today™  Homework #2 due  Homework #3 due in two weeks  Discuss the Arduino and embedded systems  Discuss the flow control statements, if, for, & while

What is a Microcontroller? A small computer usually implemented on a single IC that contains a central processing unit (CPU), some memory, and peripheral devices such as counter/timers, analog-to- digital converters, serial communication hardware, etc. 32K-ATMega328/dp/B004G5AVS6 ATmega328 the ‘brain’ of the Arduino

Where are Microcontrollers Used? Everywhere!  Car  Phone  Toothbrush  Microwave oven  Copier  Television  PC keyboard  Appliances

The Arduino Platform Atmel ATmega328 microcontroller 14 digital I/O pins  6 with PWM 6 analog I/O pins 32 kB (-2 kB) Flash memory 2 kB RAM 1 kB EEPROM 16 MHz clock $22 - $30 built  $13 ‘breadboardable’ FTDI USB chip Digital Pins Analog Pins USB jack Microcontroller power jack Voltage regulator Pwr/GND Pins ICSP Header Reset Button Power LED Pin 13 LED Rx + Tx LEDs

Handling the Arduino - How NOT to Do It! Improper Handling - NEVER!!!

Handling the Arduino - The Proper Way Proper Handling - by the edges!!!

Fundamental Flow of an Arduino Program Start Setup Loop End

Programming the Arduino An arduino program == ‘sketch’  Must have: setup() loop()  setup() configures pin modes and registers  loop() runs the main body of the program forever  like while(1) {…}  Where is main() ? Arduino simplifies things Does things for you /* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW0_pin = 12; /* configure pins */ void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT_PULLUP); } /* loop forever */ void loop() { if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW); }

Using setup() A digital pin can either be an output or an input  Output your program determines what the voltage on a pin is (either 0V (LOW or logic 0) or 5V (HIGH or logic 1)  Information is sent out  Input the world outside the microcontroller determines the voltage applied to the pin  Information is taken in const byte ledPin = 13; // LED on digital pin 13 void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } where can you find out about the commands, etc? pinMode()  sets whether a pin is an input or an output ledPin  byte constant assigned the value of 13 OUTPUT is a macro defined constant Which has the value 1 INPUT is a macro …  ?

Digital I/O Example - loop() Algorithm Refine the pseudocode, cont. :  loop forever (use function loop()) If button is not pressed:  voltage on button pin 12 will be _______  make pin 6 voltage low (LED will go off or stay off) If button is pressed:  voltage on button pin 12 will be _______  make pin 6 voltage high (LED will go on or stay on) void loop() { if(digitalRead(SW0_pin) == LOW) { digitalWrite(RGB_blue_pin, HIGH); } else { digitalWrite(RGB_blue_pin, LOW); } high (5V) low (0V)

/* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW0_pin = 12; /* configure pins */ void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT_PULLUP); } /* loop forever */ void loop() { if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW); } Digital I/O Example - Modification Modify Arduino program, so that LED is on until button is pressed, then turns off  How? Pin assignments?  setup()?  Need to turn on the LED!  loop()?  Swap values of second argument in digitalWrite calls

SCKMISOMOSISSOC1ICPAIN1AIN0T1T0INT1INT0TXDRXD LED pwm LED0LED1LED2LED3 greenbluered piezo servo SW0SW1SW2SW3 Spartronics Experimenter Digital Pin Assignments

photocellPOTtemp sensor Spartronics Experimenter Analog Pin Assignments

Code to Set Up Button Pins Two steps: 1. Make the pin an INPUT pinMode() 2. Turn the pull-up resistor on digitalWrite() a 1 to the pin const byte SW0 = 12; // button SW0 const byte SW1 = 8; // button SW1 const byte SW2 = 7; // button SW2 const byte SW3 = 4; // button SW3 void setup() { pinMode(SW0, INPUT_PULLUP); // make SW0 an // INPUT with a pullup resistor etc. } (See full_test.pde for a more elegant approach to setting up button pins)

Boolean Logic And && FalseTrue False TrueFalseTrue Or || FalseTrue False True Exclusive Or (Xor) ^^ FalseTrue False True False

Numeric Comparison Operators  ! (not) Changes true to false and false to true , >= Less than, and less than or equal to are different operations Note: !( =  ==, != (not equal) Note: equivalence uses a double ‘=‘, assignment uses a single ‘=‘, be wary = returns the value being assigned  Technically, a = b = c = d = 5; is legal. Why? = is performed right to left, so the d is assigned 5, which returns 5. That 5 is assigned to c

Examples float b = 17.0; float d = 3.14; float c = 20.0; float e = 33.0; (b < c); //true (b + c); //true (not zero) ((int)(b/c)); //false (is zero, why?) (b e); //false (b e); // true (b e) || (c < e); //true (b e) || (b + c); //true, why? printf(“%f”, b) && (b + c); //true, why?

Flow control These Boolean operations are used along with flow control (or branching) statements to control the flow of a program Decisions TrueFalse

Flow control if/if else/else – do this, do that, or do the other switch – choose between a bunch of items for – Do something for some number of times  also commonly referred to as iteration i.e. iterating over a range or iterating over a data set while – For as long as some decision is true, keep doing some stuff do.. while – Do something. At the end, if some thing is true, do it again.

Selection Structure Overview Three kinds of selections structures  if (also called, ‘single-selection’) if condition is true Perform action if condition is false, action is skipped, program continues  if/else (also called, ‘double-selection’) if condition is true Perform action else ( if condition is false ) Perform a different action (this will be skipped if condition is true)  switch (also called ‘multiple-selection’) Allows selection among many actions depending on the integral value of a variable or expression

Single Selection IF - Flowchart TRUE FALSE Speed > 65 connector flow line decision symbol action symbol Print “You’re speeding” The symbol > is a Relational Operator. The Expression “speed > 65” evaluates to 1 if true, 0 if false

if - example int speed = 5; int b = 4; if( speed > 65 ) { // do everything until the closing } printf( “You are speeding!\n” ); } // technically, when one statement is between // the curly braces, the braces are optional. // Even so, don’t omit them

Double-Selection IF - Flowchart TRUE Speed > 65 FALSE Print “Over speed limit” Print “Within speed limit”

if - example int speed = 5; int b = 4; if( speed > 65 ) { // do everything until the closing } printf( “You are speeding!\n” ); } // technically, when one statement is between // the curly braces, the braces are optional. // Even so, don’t omit them else { // note the indentation. printf( “Speed is within legal limits\n” ); }

if - example int speed = 5; int b = 4; if( speed > 65 ) { // do everything until the closing } printf( “You are speeding!\n” ); } // technically, when one statement is between // the curly braces, the braces are optional. // Even so, don’t omit them else if( speed < 65 ) { // note the indentation. printf( “Speed is within legal limits\n” ); } else { printf( “Speed is precisely 65\n” ); }

for Loop Structure – Flow Chart initialization T F Terminal decision statement Iteration operation Initializes the loop control variable: ex. a = 0; Tests the loop control variable to see if it is time to quit looping: ex. a < 5; Increments the loop control variable: ex. ++a

for Loop Structure – Flow Chart initialization T F Terminal decision statement Iteration operation for( a = 0; a < 5; ++a ) { //for( initialization, termination, iteration) printf( “%d\n”, a ); }

while Loop - Flowchart View Statement is executed while condition is true  Note that the condition must first be true in order for the statement to be executed even once statement TRUE FALSE condition

while Loop - Flowchart View statement TRUE FALSE condition while( a < 5 ) { printf( “%d\n”, a ); ++a; }

References Modular Programming in C math.h /xsh/math.h.html /xsh/math.h.html