Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 5: Microcontrollers

Similar presentations


Presentation on theme: "Week 5: Microcontrollers"— Presentation transcript:

1 Week 5: Microcontrollers
Eric Wertz 21 Feb 2018

2 Announcements & The Plan for Today™
Homework #3 due next week Discuss the Arduino and embedded systems Read chapter 5 in the text Had planned to talk about blocking and non-blocking code and debouncing switches.

3 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

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

5 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

6 Fundamental Flow of an Arduino Program
Start Setup Loop End

7 A complete Arduino Sketch
// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count = blue_count + 1; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop A complete Arduino Sketch with integrated pseudocode Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

8 A complete Arduino Sketch
// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keepsps digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count = blue_count + 1; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop A complete Arduino Sketch with integrated pseudocode Verify the code syntax Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

9 A complete Arduino Sketch
// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keepsps digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count = blue_count + 1; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop A complete Arduino Sketch with integrated pseudocode Upload the code to the board Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

10 A comment about “Magic Numbers”
// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; A comment about “Magic Numbers” It is considered poor form to litter numbers throughout your code Typos with numbers can generate bugs which are wickedly difficult to see Which is correct: or ? Better: Use constant variables to hold any number more complex than one or zero The compiler will catch any typo and you only need to get it correct in one place

11 An Arduino Sketch begins with the setup This function runs only once
The purpose is to initialize the system for use /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } Commonly setup identifies: Which pins are inputs Which pins are outputs Initializes the libraries used for various peripherals Serial console Tones Servo motors Etc. More info:

12 BLUE_LED  byte constant assigned the value of 6
pinMode(PIN, MODE); sets whether a pin is an input, input_pullup or an output BLUE_LED  byte constant assigned the value of 6 OUTPUT, INPUT, and INPUT_PULLUP are constants defined in Arduino Output are signals generated by the Arduino For example, the voltage necessary to turn on an LED Input are signals read by the Arduino INPUT_PULLUP turns is frequently used by switches Turns on a pullup resistor inside the microcontroller connected to high When the switch (button) is pressed, it drives the pin low This is an active low signal /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } Notice how the switch is wired

13 Serial.begin(COMM_SPEED); starts the serial console
The serial console is a way to obtain text information from the Arduino /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } Launch serial console

14 Serial.begin(COMM_SPEED); starts the serial console
The serial console is a way to obtain text information from the Arduino /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } Serial console

15 Primary system loop void loop() The code in the block repeats forever
The loop function launches immediately after the setup function terminates // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

16 Since the loop routine restarts when it ends
usual variables reset on each loop To keep the value of the variable across loops, use the static keyword In this example, blue_count keeps the count through loops // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

17 In this line, however, blue_count will return to 0 on each loop
// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop int blue_count = 0; // Create an int to count blues, resets to zero on every loop digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop In this line, however, blue_count will return to 0 on each loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

18 digitalWrite(pin, voltage_level);
Digital Output digitalWrite(pin, voltage_level); Sets the voltage on a digital pin to either ground (LOW) or 5 Volts (HIGH). Think of a light, either on or off // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

19 Returns the value detected on a pin (HIGH or LOW)
Digital Input digitalWrite(pin); Returns the value detected on a pin (HIGH or LOW) Recall: LOW is returned, when a button is pressed on the experimenter board // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

20 // Symbolic Constants/////////////////////////////////////////////////////
const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count = blue_count + 1; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop When the switch is pressed, specifically when a LOW (0V or ground) is detected on the pin connected to switch 0, perform the tasks between the curly braces { } Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

21 Allows the Arduino to send text output to the PC
Serial Output Allows the Arduino to send text output to the PC Serial.print(“String”); prints the string Serial.println(“String”); prints the string and then starts a new line // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_duration); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Serial.print(variable); prints the value of the variable Serial.println(variable); prints the value of the variable and then starts a new line More limited than printf Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

22 Delay delay(DELAY_IN_MILLISECONDS);
// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Delay delay(DELAY_IN_MILLISECONDS); Delays the system for a number of milliseconds. Blocking operation While the delay is occurring, nothing changes Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

23 Delete all nonstatic variables Launch void loop() again
// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count = blue_count + 1; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever Repeat the loop Delete all nonstatic variables Launch void loop() again

24 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.

25 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.

26 References Modular Programming in C math.h


Download ppt "Week 5: Microcontrollers"

Similar presentations


Ads by Google