Skip to main content

Posts

Showing posts from September, 2017

Day 10: Statistics and Header Files

Software: Statistics and Header Files Statistical Measurements Important functions include maximum, minimum, average, and median. Example 1.1 /*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /* This function returns the average or mean value of an */ /* array x with n elements. */ double mean(double x[],int npts) { /* Declare and initialize variables. */ int k; double sum=0; /* Determine mean value. */ for (k=0; k<=npts-1; k++) sum += x[k]; /* Return mean value. */ return sum/n; } Sum was initialized to zero in declaration statement Example 1.2 /*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /* This function returns the median value in the sorted */ /* array x with npts elements. */ double median(double x[],int npts) { /* Declare variables. */ int k; double median_x; /* Determine median value. */ k = floor(npts/2); if (n%2 != 0) median_x = x[k]; else median_x = (x[k-1] + x[k])/2; /* Re...

Day 8: Intro to Data Structures | Servos and More Music

Lecture One Dimensional Arrays Arrays are a type of data structure that allows for working with a group of values using a single identifier.  Lab Servos Servos rotate to a particular angular position and stay there until told to move to a new position.  There are both standard and continuous rotation servos. Unmodified ones have a fixed range due to a potentiometer in line with the drive shaft that is used for reporting the present position. Servos are controlled by sending a pulse of a certain length; this length determines the position it will rotate to. However, in a continuous rotation servo, there is no potentiometer and the servo rotates continuously, and the pulse length determines the speed instead.  Servos draw a significant amount of current, typically more than an Arduino can supply, so it is best to use a separate power supply. Homework Hardware //Servo Potentiometer Control #include <Servo.h> const int SERVO=9; //Servo on...

Day 7: Random Numbers and Recursion | Sound

Lecture Random Numbers Random numbers can be generated using the rand function, however, the same value would be printed since it generates integers in a specified sequence. To generate a new sequence of random numbers each time, a new random-number seed is needed. First, I need to use srand to generate a seed in order for the rand function to generate new random numbers every time. We can also generate random integers between specified limits.  #include <stdio.h> #include <stdlib.h> int main(void) {   /* Declare variables and function prototype. */   unsigned int seed;   int a, b, k;   int rand_int(int a,int b);   /* Get seed value and interval limits. */   printf("Enter a positive integer seed value: \n");   scanf("%u",&seed);   srand(seed);   printf("Enter integer limits a and b (a<b): \n");   scanf("%i %i",&a,&b);   /* Generate and print ten random numbe...

Day 6: Analog Sensors | Functions and Modularity

Functions Functions are sets of statements that typically perform operation or compute value. They can help to make programs more accessible and usable for non-programmers e.g. create function that allows user to type "go forward" and move a robot forward. Modules -Functions can be split up into modules, "divide and conquer" -Each module has specific purpose, can be written and tested separately -Smaller than complete solution, therefore testing is easier -Can be used in new problem solutions without being retested -Reduces overall length of program -Allows for increased collaboration; modules can be worked on in parallel Debugging Longer Programs Use a compiler that gives meaningful information about errors. Adding comments around some sections of code can allow for better focus on other parts of the program. Test complicated functions by themselves. Programmer Defined Functions Execution of program always begins with main function. Additional ...