Week 4: Microcontrollers & Flow Control

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.
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.
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.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
Embedded Programming and Robotics
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Colorado Space Grant Consortium Gateway To Space ASEN 1400 / ASTR 2500 Class #12 Gateway To Space ASEN 1400 / ASTR 2500 Class #12 T-58.
Lecture 9: Microcontrollers – part 1 BJ Furman 29OCT2012.
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.
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.
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.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Lecture 10: Peripherals Bryan Burlingame 04 November 2015.
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.
Microcontroller basics Embedded systems for mortals.
Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.
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?
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.
Lecture 2: Logic Bryan Burlingame 31 Aug 2016.
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
IOT Design: An Embedded System Overview MicroControllers
Assist. Prof. Rassim Suliyev - SDU 2017
Week 6: Style and Peripherals
Val Manes Department of Math & Computer Science
Microcontroller basics
Chapter 4 C Program Control Part I
Arduino & its hardware interfacing
Arduino Programming Part II
UTA010 : Engineering Design – II
An Arduino Workshop A Microcontroller.
UCD ElecSoc Robotics Club 2017/2018
Arduino Part 1 Topics: Microcontrollers Programming Basics
INC 161 , CPE 100 Computer Programming
Programming Boe-Bots (Part 1)
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.
Lecture 7: Functions Revisited / Analogue Signals
Week 6: Microcontrollers II
Microsoft Visual Basic 2005 BASICS
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Structured Program
Introduction to Arduino
Arduino Board.
Arduino म्हणजे काय?.
Lecture 8: Arduino 20 March 2019.
Setting up a basic program with Arduino
Introduction to Arduinos
SAURABH GINGADE.
Introduction to arduino
Presentation transcript:

Week 4: Microcontrollers & Flow Control Bryan Burlingame 13 Sept 2017

Announcements & The Plan for Today™ Homework #1 & #2 due up front Homework #3 Discuss the flow control statements, if, for, & while Discuss the Arduino and embedded systems Read: finish chapter 5 & all of chapter 6 in the text Had planned to talk about blocking and non-blocking code and debouncing switches.

Recall: Boolean Logic And && False True Exclusive Or (Xor) ^^ 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: !(<) is the same as >= ==, != (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 < c) && (d > e); //false (b < c) || (d > e); // true (b < c) && (d > e) || (c < e); //true (b < c) && (d > 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 True False

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’) 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 connector flow line action symbol decision symbol TRUE Print “You’re speeding” Speed > 65 FALSE If the speed is greater than 65, take the action to print a message. If the speed is not greater than 65, just keep going on in the program. What does Speed > 65 evaluate to if Speed == 72 ? 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 Print “Within speed limit” Print “Over speed limit” Speed > 65 FALSE TRUE Note that if Speed > 65 is true, then the message “Over speed limit is printed”, and then the program continues. If Speed > 65 is not true, then the message “Within limit” is printed.

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; }

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. ATmega328 the ‘brain’ of the Arduino http://www.amazon.com/AVR-Pin-20MHz-32K-ATMega328/dp/B004G5AVS6

Where are Microcontrollers Used? Everywhere! Car Phone Toothbrush Microwave oven Copier Television PC keyboard Appliances http://ecomodder.com/wiki/index.php/MPGuino

The Arduino Platform http://arduino.cc/ USB jack Microcontroller power jack Voltage regulator Pwr/GND Pins ICSP Header Reset Button Power LED Pin 13 LED Rx + Tx LEDs Digital Pins 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 Analog Pins http://arduino.cc/

Fundamental Flow of an Arduino Program Start Setup Loop End

Programming the Arduino /* 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); An arduino program == ‘sketch’ Must have: setup() loop() configures pin modes and registers runs the main body of the program forever like while(1) {…} Where is main() ? Arduino simplifies things Does things for you This slide is meant as a quick overview of an Arduino sketch. We’ll unpack and describe the pieces in more detail later. Think of setup() as what you would do when you first get in a rental car or a car you have borrowed from a friend: adjust the mirrors, adjust the steering wheel, the seats, change the stereo channel settings, etc. The car can be driven by anyone, but everyone likes to customize it for their comfort. main() {    init();    setup();    while (1)       loop(); } And the reason that they do it this way is so it's guaranteed that init() gets called, which is critical for Arduino initialization.

Using setup() A digital pin can either be an output or an input Output 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); } 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 pinMode() sets whether a pin is an input, input_pullup 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 …  ? Emphasize that the distinction between inputs and outputs. As an example, you might have the class envision that the front row of students in the class are digital pins (everyone else are internals elements of the microcontroller). Designate one student as an OUTPUT. Have the rest of the students (in unison) tell the OUTPUT student to raise his or her hand. (Have the designated ‘OUTPUT’ follow the command!). Then have the rest of the class (in unison) tell the ‘OUTPUT’ to put his or her hand down. Thus, the microcontroller has sent information ‘out’, and the pin designated as an OUTPUT has responded accordingly. Similarly designate one student in the front row as an INPUT. Have the rest of the class ask the INPUT student what the state of the room light is (is it on or off?). Have the INPUT student report to the rest of the class the state of the room light. Explain the declaration of ledPin as a const byte variable. Explain the advantage over #define ledPin 13 Digital HIGH = = 5V (usually taken as logical 1) Digital LOW = = 0V (usually taken as logical 0) wiring.h in arduino/hardware/cores/arduino #DEFINEs OUTPUT as 0x1 and INPUT as 0x0 where can you find out about the commands, etc? http://arduino.cc/en/Reference/Extended

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: make pin 6 voltage high (LED will go on or stay on) high (5V) low (0V) void loop() { if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); } else digitalWrite(RGB_blue_pin, LOW);

Digital I/O Example - Modification /* 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); 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 Pin assignments stay the same - no change to the external hardware or its connection to the Arduino setup() - add a line to turn on the LED loop() - swap the arguments in the digitalWrite() function calls: if button is pressed, then LOW, else, HIGH

Spartronics Experimenter Digital Pin Assignments 13 12 11 10 9 8 7 6 5 4 3 2 1 SCK MISO MOSI SS OC1 ICP AIN1 AIN0 T1 T0 INT1 INT0 TXD RXD LED pwm LED0 LED1 LED2 LED3 green blue red piezo servo SW0 SW1 SW2 SW3 Piezo rectangle is hyperlinked to an image of the actual Experimenter board. The piezo speaker is hyperlinked back to this slide.

Spartronics Experimenter Analog Pin Assignments 7 6 5 4 3 2 1 photocell POT temp sensor The rectangle around cells for pins 2, 1, 0 are hyperlinked to an image of the actual Experimenter board. Each respective element is hyperlinked back to this slide.

References Modular Programming in C http://www.icosaedro.it/c-modules.html math.h http://www.opengroup.org/onlinepubs/007908799/xsh/math.h.html