Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Functions Revisited / Analogue Signals

Similar presentations


Presentation on theme: "Lecture 7: Functions Revisited / Analogue Signals"— Presentation transcript:

1 Lecture 7: Functions Revisited / Analogue Signals
Bryan Burlingame 6 Mar 2018

2 Announcements Homework 4 due up front Homework 5 due next week
Read chapter 6 for next week

3 Learning Objectives Explain the concept of modular program design
Expand on the concept of a function Discuss stub functions Introduce analogue signals

4 Recall: Modular Programming
Break a large problem into smaller pieces Smaller pieces sometimes called ‘modules’ or ‘subroutines’ or ‘procedures’ or functions Why? Helps manage complexity Smaller blocks of code Easier to read Encourages re-use of code Within a particular program or across different programs Allows independent development of code Provides a layer of ‘abstraction’ Imagine trying to develop a huge program like the Windows or Mac OS with hundreds of thousands (or millions of lines of code). No way to do it all in one big program file. Is the work of hundreds of computer programmers, each working on smaller pieces of the problem that are then brought together. By ‘abstraction’, I mean a way to simplify or separate the details of how a process works to an essential set of features that allow the process to be used. Example: a stick figure compared to a Leonardo da Vinci drawing or a photograph of a man. In programming a function allows you to not need to know the details of the function in order to be able to use it. Ex. sqrt() a = sqrt(9.0);

5 Preprocessor directives
/* Calculate the area of triangles of increasing size 30 Jan 2018 Bryan Burlingame ME30, SJSU Declare variables for side and area measurements Print the header line "Side Area" From 1 to 10, calculate the area of the 90,45,45 degree triangles print the area return */ #include <stdio.h> #include <stdlib.h> int main(void) { // declare variables float side = 0; float area = 0; // print the header printf("Side\tArea\n"); // from 1 to 10 for (side = 1; side < 11; side++) area = 0.5 * side * side; printf("%0.2f\t%0.2f\n", side, area); } return(EXIT_SUCCESS); Programmer’s block Preprocessor directives Declare and initialize variables Print header For loop (Repetition) Return Statement

6 There are three distinct actions in this function
/* Calculate the area of triangles of increasing size 30 Jan 2018 Bryan Burlingame ME30, SJSU Declare a counter Print the header line "Side Area" From 1 to 10, calculate the area of the 90,45,45 degree triangles print the area return to operating system */ #include <stdio.h> #include <stdlib.h> int main(void) { // declare variables float side = 0; float area = 0; // print the header printf("Side\tArea\n"); // from 1 to 10 for (side = 1; side < 11; side++) area = 0.5 * side * side; printf("%0.2f\t%0.2f\n", side, area); } return(EXIT_SUCCESS); There are three distinct actions in this function

7 There are two distinct actions in this function
/* Calculate the area of triangles of increasing size 30 Jan 2018 Bryan Burlingame ME30, SJSU Declare a counter Print the header line "Side Area" From 1 to 10, calculate the area of the 90,45,45 degree triangles print the area return to operating system */ #include <stdio.h> #include <stdlib.h> int main(void) { // declare variables float side = 0; float area = 0; // print the header printf("Side\tArea\n"); // from 1 to 10 for (side = 1; side < 11; side++) area = 0.5 * side * side; printf("%0.2f\t%0.2f\n", side, area); } return(EXIT_SUCCESS); There are two distinct actions in this function Print the header lines

8 There are two distinct actions in this function
/* Calculate the area of triangles of increasing size 30 Jan 2018 Bryan Burlingame ME30, SJSU Declare a counter Print the header line "Side Area" From 1 to 10, calculate the area of the 90,45,45 degree triangles print the area return to operating system */ #include <stdio.h> #include <stdlib.h> int main(void) { // declare variables float side = 0; float area = 0; // print the header printf("Side\tArea\n"); // from 1 to 10 for (side = 1; side < 11; side++) area = 0.5 * side * side; printf("%0.2f\t%0.2f\n", side, area); } return(EXIT_SUCCESS); There are two distinct actions in this function Print the header Print the table detail lines

9 There are two distinct actions in this function
/* Calculate the area of triangles of increasing size 30 Jan 2018 Bryan Burlingame ME30, SJSU Declare a counter Print the header line "Side Area" From 1 to 10, calculate the area of the 90,45,45 degree triangles print the area return to operating system */ #include <stdio.h> #include <stdlib.h> int main(void) { // declare variables float side = 0; float area = 0; // print the header printf("Side\tArea\n"); // from 1 to 10 for (side = 1; side < 11; side++) area = 0.5 * side * side; printf("%0.2f\t%0.2f\n", side, area); } return(EXIT_SUCCESS); There are two distinct actions in this function Print the header Print the table detail lines Calculate the area

10 Functions The ‘building blocks’ of a C program
You’ve used predefined functions already: main() printf() User-defined functions Your own code In combination with predefined functions Each function should implement one concept

11 Standard Libraries The built-in building blocks
stdio.h printf – print to the screen fprintf – print to a file fgets – get a string from a stream getchar – get a character from the keyboard rename – rename a file math.h sin – calculate the sine cos – calculate the cosine pow – raise a value to a power sqrt – calculate square root tgamma – calculate the gamma function

12 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; float area = 0; // from 1 to 10 for (side = 1; side < 11; side++) area = triarea(side); printf("%0.2f\t%0.2f\n", side, area); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Over the previous version, this version is an improvement. It segregates the triangle area calculation and creates something which can be independently tested.

13 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2f\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Note: a function can be called just about anywhere its return value could be used Note how the triarea function call replaces the area variable in the printf statement

14 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2f\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Nomenclature Function Protoype – The declaration of the existence of a function. Tells the parser what a given function looks like Function Call – Where a function is used in a program Function Definition – Where the functions operations are actually defined Function Parameters – The values which are passed into a function from the function call Return Value – The value a function returns from the function call. After the function completes, the function call is replaced with its return value Return Type – The data type of the return value. Void indicates there is no return value

15 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2f\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Nomenclature Function Protoype – The declaration of the existence of a function. Tells the parser what a given function looks like Function Call – Where a function is used in a program Function Definition – Where the functions operations are actually defined Function Parameters – The values which are passed into a function from the function call Return Value – The value a function returns from the function call. After the function completes, the function call is replaced with its return value Return Type – The data type of the return value. Void indicates there is no return value

16 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2f\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Nomenclature Function Protoype – The declaration of the existence of a function. Tells the parser what a given function looks like Function Call – Where a function is used in a program Function Definition – Where the functions operations are actually defined Function Parameters – The values which are passed into a function from the function call Return Value – The value a function returns from the function call. After the function completes, the function call is replaced with its return value Return Type – The data type of the return value. Void indicates there is no return value

17 void indicates there are no parameters
#include <stdio.h> #include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } ///////////////////////////////// void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2f\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Nomenclature Function Protoype – The declaration of the existence of a function. Tells the parser what a given function looks like Function Call – Where a function is used in a program Function Definition – Where the functions operations are actually defined Function Parameters – The values which are passed into a function from the function call Return Value – The value a function returns from the function call. After the function completes, the function call is replaced with its return value Return Type – The data type of the return value. Void indicates there is no return value void indicates there are no parameters

18 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } ///////////////////////////////// void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2f\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Nomenclature Function Protoype – The declaration of the existence of a function. Tells the parser what a given function looks like Function Call – Where a function is used in a program Function Definition – Where the functions operations are actually defined Function Parameters – The values which are passed into a function from the function call Return Value – The value a function returns from the function call. After the function completes, the function call is replaced with its return value Return Type – The data type of the return value. Void indicates there is no return value

19 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } ///////////////////////////////// void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2f\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { return (0.5 * side * side); } Nomenclature Function Protoype – The declaration of the existence of a function. Tells the parser what a given function looks like Function Call – Where a function is used in a program Function Definition – Where the functions operations are actually defined Function Parameters – The values which are passed into a function from the function call Return Value – The value a function returns from the function call. After the function completes, the function call is replaced with its return value Return Type – The data type of the return value. Void indicates there is no return value

20 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } ///////////////////////////////// void print_the_header() printf(“print_the_header()"); void print_the_detail() printf(“print_the_detail()\n"); printf(“triarea(float side) return value: %f\n“, triarea(1.0)); /////////////////////////////////////////// float triarea(float side) { return (-1000); } Debug Methods Stub functions – functions which generate a known return value Allows the programmer to implement each part of the logic, piece by piece Note how this program doesn’t do anything it is supposed to do, but the primary logic is laid out. Now the programmer can implement and test each function piece by piece

21 #include <stdio.h>
#include <stdlib.h> /////////////////////////////////////////// void print_the_header(); void print_the_detail(); float triarea(float side); int main(void) { print_the_header(); print_the_detail(); return(EXIT_SUCCESS); } ///////////////////////////////// void print_the_header() // print the header printf("Side\tArea\n"); void print_the_detail() // declare variables float side = 0; // from 1 to 10 for (side = 1; side < 11; side++) printf("%0.2f\t%0.2d\n", side, triarea(side)); /////////////////////////////////////////// float triarea(float side) { printf(“Enter triarea with param: %f\n”, side); printf(“Area: %f\n”, 0.5 * side * side); printf(“Leaving triarea\n”); return (0.5 * side * side); } Debug print (logging) (good to do to the Serial.console in the Arduino) Print what is known around the problem. If we never see the prints that we enter triarea, then we know there’s a problem with the function call. If we see that the area is correct in the function, but isn’t outside the function, then there’s likely a problem with either the return type or how the return value is handled. If the area is wrong in the function and outside the function, then we know the calculation is wrong

22 Analog Signals Real world systems tend to be analog
Covers the range from 0V to 5V Recall: Microcontrollers work in binary, so how does this work We approximate! Step-wise approximation of a continuous function

23 Analog Out (PWM) Concept
No facility exists on most microcontrollers to directly output an analog voltage (i.e., a voltage that varies continuously over the range of 0 to 5V) Use Pulse Width Modulation (PWM) to approximate Digital outputs are capable of 0V or 5V Over a fraction (ton) of a time period tcycle, keep pin at 5V, the rest of the time, at 0V Average voltage is proportional to ton/tcycle, called the ‘Duty Cycle’ 5V time So far, we’ve talked about digital I/O and analog input. How about getting an analog output?

24 Arduino analogWrite( )
analogWrite(pin, value); 0  value  255 (How many bits?) 0% duty cycle --> 0 V --> analogWrite(pin, 0); 100% duty cycle --> 5 V --> analogWrite(pin, 255); Must be a PWM pin

25 Analog Output Example Fade the red LED in, then out
const byte ledPin = 3; // red RGB LED on Experimenter const byte FADE_MAX = 255; // max value for setting duty cycle const byte FADE_INC = 5; // increment for changing duty cycle void setup() { pinMode(ledPin, OUTPUT); // note pinMode is not necessary for analog outputs } void loop() int fadeValue = 0; // PWM value // fade in from min to max in increments of 5 points: for(fadeValue = 0 ; fadeValue <= FADE_MAX; fadeValue +=FADE_INC) analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255): // fade out from max to min in increments of 5 points: for(fadeValue = FADE_MAX; fadeValue >= 0; fadeValue -=FADE_INC) Fade the red LED in, then out duty cycle is incremented then decremented 256 steps 0% to 100%

26 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 red green blue 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. Notice how all the LEDs are on PWM pins

27 Arduino analogRead( ) analogRead(pin);
Returns an integer 0 – 1024 (how many bits?) 0 V --> analogRead(pin) == 0 5 V --> analogWrite(pin) == 1024 Must be an Analog pin

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

29 Resolution All analog to digital and digital to analog systems have a minimum detectable change. This is the resolution of the system On these Arduinos 8 BIT DAC (digital to analog conversion) 5V / 28 = 20 millivolts 10 BIT ADC (analog to digital conversion) 5V / 210 = 5 millivolts

30 Arduino map Map is a useful Arduino function to map a value in one range onto a value in a different range map(val, from_low, from_high, to_low, to_high); y = map(x, 0, 256, 0, 1024); Maps a value stored in x which has a range from 0 – 256 and spreads over the range from 0 – 1024 (i.e. 0 stays 0, 128 (1/2 of the old max) becomes 512 (1/2 of the new max), and 256 becomes 1024) and stores the new value in y

31 References Kochan, S. Programming in C, Fourth Edition (2014)
Downey, A. Scheffler, T. How to Think Like a Computer Scientist – C Version (2018)


Download ppt "Lecture 7: Functions Revisited / Analogue Signals"

Similar presentations


Ads by Google