Part 1 should work correctly if all of part 2 and sd card junk is commented out. Attempted to update the libraries multiple times but could not get code to compile. the error "overload function" kept appearing.
Dependencies: 4DGL-uLCD-SE MMA8452Q SDFileSystem bouncing_ball mbed
Fork of OCE360_4 by
main.cpp@5:d432702334a9, 2017-11-17 (annotated)
- Committer:
- jakebonney10
- Date:
- Fri Nov 17 03:21:26 2017 +0000
- Revision:
- 5:d432702334a9
- Parent:
- 3:a7f02754bc99
Part 1 should work correctly if all of part 2 and sd card junk is commented out. Attempted to update the libraries multiple times but could not get code to compile. the error "overload function" kept appearing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
slicht | 2:552e6feb8709 | 1 | // A solution to OCE360 Homework #4. |
slicht | 2:552e6feb8709 | 2 | // Objective: Use object oriented programming to create a system that displays |
slicht | 2:552e6feb8709 | 3 | // multiple balls bouncing around the LCD screen. |
slicht | 3:a7f02754bc99 | 4 | // Stephen Licht, 11/7/2017 |
slicht | 1:6b99ffa62cc8 | 5 | |
slicht | 0:8d3812068c6c | 6 | #include "mbed.h" |
slicht | 2:552e6feb8709 | 7 | #include "MMA8452Q.h" //acceleromater library |
slicht | 2:552e6feb8709 | 8 | #include "uLCD_4DGL.h" //LCD library |
slicht | 2:552e6feb8709 | 9 | #include "bouncing_ball.h" //new ball phyics library |
jakebonney10 | 5:d432702334a9 | 10 | #include "SDFileSystem.h" |
slicht | 1:6b99ffa62cc8 | 11 | |
slicht | 2:552e6feb8709 | 12 | #define UPDATE_TIME_S 0.02 |
slicht | 2:552e6feb8709 | 13 | #define START_X_1 10 |
slicht | 2:552e6feb8709 | 14 | #define START_Y_1 10 |
slicht | 2:552e6feb8709 | 15 | #define START_X_2 20 |
slicht | 2:552e6feb8709 | 16 | #define START_Y_2 20 |
slicht | 2:552e6feb8709 | 17 | #define RADIUS_1 6 |
slicht | 2:552e6feb8709 | 18 | #define RADIUS_2 3 |
slicht | 0:8d3812068c6c | 19 | |
slicht | 2:552e6feb8709 | 20 | #define DEBUG_MODE 0 |
slicht | 2:552e6feb8709 | 21 | |
jakebonney10 | 5:d432702334a9 | 22 | // Variables for SD file system code |
jakebonney10 | 5:d432702334a9 | 23 | FILE *file; |
jakebonney10 | 5:d432702334a9 | 24 | float voltage_in; |
jakebonney10 | 5:d432702334a9 | 25 | float degrees_c; |
jakebonney10 | 5:d432702334a9 | 26 | int n = 0; |
jakebonney10 | 5:d432702334a9 | 27 | int c; |
jakebonney10 | 5:d432702334a9 | 28 | |
jakebonney10 | 5:d432702334a9 | 29 | // Define Tickers/timers |
jakebonney10 | 5:d432702334a9 | 30 | Ticker flipper1; |
jakebonney10 | 5:d432702334a9 | 31 | Ticker flipper2; |
jakebonney10 | 5:d432702334a9 | 32 | Timer timestamp; |
jakebonney10 | 5:d432702334a9 | 33 | Timer debounce; // debounce timer for Record external interrupt |
jakebonney10 | 5:d432702334a9 | 34 | Timer debounce2; // debounce timer for LCDshow ecternal interrupt |
jakebonney10 | 5:d432702334a9 | 35 | |
jakebonney10 | 5:d432702334a9 | 36 | //internal leds |
jakebonney10 | 5:d432702334a9 | 37 | DigitalOut myled2(LED2); |
jakebonney10 | 5:d432702334a9 | 38 | DigitalOut myled4(LED4); |
jakebonney10 | 5:d432702334a9 | 39 | |
jakebonney10 | 5:d432702334a9 | 40 | //Function prototype for color selection and Ticker updates |
slicht | 2:552e6feb8709 | 41 | int get_LCD_color(int color_integer); |
jakebonney10 | 5:d432702334a9 | 42 | void LCD_update(void); // LCD screen update function prototype |
jakebonney10 | 5:d432702334a9 | 43 | void BALL_update(void); // Ball update function prototype |
jakebonney10 | 5:d432702334a9 | 44 | void recording(void); // Recording function prototype |
jakebonney10 | 5:d432702334a9 | 45 | void display(void); // Display LCD screen function Prototype |
jakebonney10 | 5:d432702334a9 | 46 | void flip2() { // flip 2 function |
jakebonney10 | 5:d432702334a9 | 47 | myled2 = !myled2; |
jakebonney10 | 5:d432702334a9 | 48 | } |
jakebonney10 | 5:d432702334a9 | 49 | |
jakebonney10 | 5:d432702334a9 | 50 | // Temperature Analog input (pin 15) |
jakebonney10 | 5:d432702334a9 | 51 | AnalogIn ain(p15); |
jakebonney10 | 5:d432702334a9 | 52 | |
jakebonney10 | 5:d432702334a9 | 53 | // External Interrupt inputs |
jakebonney10 | 5:d432702334a9 | 54 | InterruptIn record(p18); |
jakebonney10 | 5:d432702334a9 | 55 | InterruptIn LCDshow(p17); |
jakebonney10 | 5:d432702334a9 | 56 | |
jakebonney10 | 5:d432702334a9 | 57 | // SD card (SPI pins) |
jakebonney10 | 5:d432702334a9 | 58 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
jakebonney10 | 5:d432702334a9 | 59 | |
jakebonney10 | 5:d432702334a9 | 60 | //Define Serial Terminal |
jakebonney10 | 5:d432702334a9 | 61 | Serial pc(USBTX, USBRX); |
slicht | 1:6b99ffa62cc8 | 62 | |
slicht | 1:6b99ffa62cc8 | 63 | // Graphic LCD - TX, RX, and RES pins |
slicht | 2:552e6feb8709 | 64 | uLCD_4DGL uLCD(p9,p10,p11); //initialize a driver object for an LCD connected on pins 9-11 |
slicht | 1:6b99ffa62cc8 | 65 | |
slicht | 1:6b99ffa62cc8 | 66 | // Accelerometer - SDA, SCL, and I2C address |
slicht | 2:552e6feb8709 | 67 | MMA8452Q accel(p28, p27, 0x1D); //initialize a driver object for an accelerometer connected on pins 27-28. |
slicht | 1:6b99ffa62cc8 | 68 | |
slicht | 2:552e6feb8709 | 69 | physics_ball ball1; //initialize two balls for bouncing |
slicht | 2:552e6feb8709 | 70 | physics_ball ball2; //the default states from the library will be used initially |
slicht | 0:8d3812068c6c | 71 | |
slicht | 2:552e6feb8709 | 72 | int main() |
slicht | 2:552e6feb8709 | 73 | { |
jakebonney10 | 5:d432702334a9 | 74 | // led for ball update |
jakebonney10 | 5:d432702334a9 | 75 | myled2 = 0; |
jakebonney10 | 5:d432702334a9 | 76 | myled4 = 0; |
jakebonney10 | 5:d432702334a9 | 77 | flipper2.attach(&flip2, .02); |
jakebonney10 | 5:d432702334a9 | 78 | |
slicht | 1:6b99ffa62cc8 | 79 | // Initialize uLCD |
slicht | 1:6b99ffa62cc8 | 80 | uLCD.baudrate(115200); |
slicht | 1:6b99ffa62cc8 | 81 | uLCD.background_color(BLACK); |
jakebonney10 | 5:d432702334a9 | 82 | uLCD.cls(); |
jakebonney10 | 5:d432702334a9 | 83 | |
slicht | 1:6b99ffa62cc8 | 84 | // Initialize accelerometer |
slicht | 1:6b99ffa62cc8 | 85 | accel.init(); |
slicht | 1:6b99ffa62cc8 | 86 | |
slicht | 2:552e6feb8709 | 87 | //Initialize balls: |
slicht | 2:552e6feb8709 | 88 | ball1.set_state(START_X_1,START_Y_1,0,0); //speeds are set to zero |
slicht | 2:552e6feb8709 | 89 | ball1.set_state(START_X_2,START_Y_2,0,0); |
slicht | 2:552e6feb8709 | 90 | |
slicht | 2:552e6feb8709 | 91 | //Set ball radius and color: |
slicht | 2:552e6feb8709 | 92 | ball1.set_param(RADIUS_1,0); //color is unimportant |
slicht | 2:552e6feb8709 | 93 | ball2.set_param(RADIUS_2,1); //just making sure the colors are different |
jakebonney10 | 5:d432702334a9 | 94 | |
jakebonney10 | 5:d432702334a9 | 95 | // Start Timers and Tickers |
jakebonney10 | 5:d432702334a9 | 96 | debounce.start(); |
jakebonney10 | 5:d432702334a9 | 97 | debounce2.start(); |
jakebonney10 | 5:d432702334a9 | 98 | |
jakebonney10 | 5:d432702334a9 | 99 | flipper2.attach(&BALL_update, 0.02); // update ball position @ 50hz |
jakebonney10 | 5:d432702334a9 | 100 | flipper1.attach(&LCD_update, 0.0909); // update LCD screen @ 11hz |
jakebonney10 | 5:d432702334a9 | 101 | |
jakebonney10 | 5:d432702334a9 | 102 | //hardwareinterrupt |
jakebonney10 | 5:d432702334a9 | 103 | record.rise(&recording); |
jakebonney10 | 5:d432702334a9 | 104 | LCDshow.rise(&display); |
jakebonney10 | 5:d432702334a9 | 105 | |
slicht | 2:552e6feb8709 | 106 | /* Make the balls "fall" in direction of accelerometer by forcing the ball objects |
slicht | 2:552e6feb8709 | 107 | to update themselves at regular intervals, and te drawing the locations reported |
slicht | 2:552e6feb8709 | 108 | by the ball objects on the LCD screen: */ |
slicht | 2:552e6feb8709 | 109 | while (1) { //execute 'forever' |
jakebonney10 | 5:d432702334a9 | 110 | } |
jakebonney10 | 5:d432702334a9 | 111 | } |
jakebonney10 | 5:d432702334a9 | 112 | // Functions: |
jakebonney10 | 5:d432702334a9 | 113 | void display() |
jakebonney10 | 5:d432702334a9 | 114 | } |
jakebonney10 | 5:d432702334a9 | 115 | void LCD_update() { // LCD screen update function |
jakebonney10 | 5:d432702334a9 | 116 | |
jakebonney10 | 5:d432702334a9 | 117 | |
slicht | 3:a7f02754bc99 | 118 | // Draw circles in the x and y positions stored by the ball objects: |
slicht | 2:552e6feb8709 | 119 | uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, get_LCD_color(ball1.color)); |
slicht | 2:552e6feb8709 | 120 | uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, get_LCD_color(ball2.color)); |
slicht | 2:552e6feb8709 | 121 | |
slicht | 2:552e6feb8709 | 122 | // Wait before erasing old circles: |
slicht | 2:552e6feb8709 | 123 | wait(UPDATE_TIME_S); // In seconds |
slicht | 2:552e6feb8709 | 124 | |
slicht | 2:552e6feb8709 | 125 | // Erase old circles by writing over there locations using the screen color: |
slicht | 2:552e6feb8709 | 126 | uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLACK); |
slicht | 2:552e6feb8709 | 127 | uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, BLACK); |
slicht | 1:6b99ffa62cc8 | 128 | |
jakebonney10 | 5:d432702334a9 | 129 | } |
jakebonney10 | 5:d432702334a9 | 130 | void BALL_update() // BALL position update function |
jakebonney10 | 5:d432702334a9 | 131 | { |
jakebonney10 | 5:d432702334a9 | 132 | |
slicht | 2:552e6feb8709 | 133 | // Force the objects 'ball1' and 'ball2' to update their stored positions |
slicht | 2:552e6feb8709 | 134 | // and velocities: |
slicht | 2:552e6feb8709 | 135 | ball1.update(UPDATE_TIME_S,accel); |
slicht | 2:552e6feb8709 | 136 | ball2.update(UPDATE_TIME_S,accel); |
slicht | 2:552e6feb8709 | 137 | |
jakebonney10 | 5:d432702334a9 | 138 | myled2=!myled2; |
jakebonney10 | 5:d432702334a9 | 139 | |
jakebonney10 | 5:d432702334a9 | 140 | // Potential way of counting updates |
jakebonney10 | 5:d432702334a9 | 141 | n = n + 1; |
jakebonney10 | 5:d432702334a9 | 142 | |
jakebonney10 | 5:d432702334a9 | 143 | void recording() { |
jakebonney10 | 5:d432702334a9 | 144 | |
jakebonney10 | 5:d432702334a9 | 145 | if (debounce.read_ms()>200) // only allow function if debounce timer |
jakebonney10 | 5:d432702334a9 | 146 | // Resets and starts time stamp timer |
jakebonney10 | 5:d432702334a9 | 147 | timestamp.reset(); |
jakebonney10 | 5:d432702334a9 | 148 | timestamp.start(); |
jakebonney10 | 5:d432702334a9 | 149 | // Turn onboard LED on |
jakebonney10 | 5:d432702334a9 | 150 | myled4 = 1; |
jakebonney10 | 5:d432702334a9 | 151 | |
jakebonney10 | 5:d432702334a9 | 152 | // Open file for writing |
jakebonney10 | 5:d432702334a9 | 153 | file = fopen("/sd/temp_data.txt", "w"); |
jakebonney10 | 5:d432702334a9 | 154 | if ( file == NULL ) { |
jakebonney10 | 5:d432702334a9 | 155 | error("ERROR: Could not open file for writing!\n\r"); |
jakebonney10 | 5:d432702334a9 | 156 | return -1; |
jakebonney10 | 5:d432702334a9 | 157 | } |
slicht | 2:552e6feb8709 | 158 | |
jakebonney10 | 5:d432702334a9 | 159 | // Tell the user we need to wait while we collect some data |
jakebonney10 | 5:d432702334a9 | 160 | pc.printf("\nCollecting data (Do not remove SD Card!) ...\n\r"); |
jakebonney10 | 5:d432702334a9 | 161 | |
jakebonney10 | 5:d432702334a9 | 162 | // Collect temperatures with timestamps every second |
jakebonney10 | 5:d432702334a9 | 163 | for(i = 0; i < 10; i++) { |
jakebonney10 | 5:d432702334a9 | 164 | voltage_in = ain * 3.3; |
jakebonney10 | 5:d432702334a9 | 165 | degrees_c = (voltage_in - 0.5) * 100.0; |
jakebonney10 | 5:d432702334a9 | 166 | fprintf(file, "%2.2fs: %3.1f deg C\n\r", timer.read(), degrees_c); |
jakebonney10 | 5:d432702334a9 | 167 | wait(1); |
jakebonney10 | 5:d432702334a9 | 168 | } |
slicht | 1:6b99ffa62cc8 | 169 | |
jakebonney10 | 5:d432702334a9 | 170 | // Close file and re-open it for reading |
jakebonney10 | 5:d432702334a9 | 171 | fclose(file); |
jakebonney10 | 5:d432702334a9 | 172 | file = fopen("/sd/temp_data.txt", "r"); |
jakebonney10 | 5:d432702334a9 | 173 | if ( file == NULL ) { |
jakebonney10 | 5:d432702334a9 | 174 | error("ERROR: Could not open file for reading!\n\r"); |
jakebonney10 | 5:d432702334a9 | 175 | return -1; |
slicht | 2:552e6feb8709 | 176 | } |
jakebonney10 | 5:d432702334a9 | 177 | |
jakebonney10 | 5:d432702334a9 | 178 | // Print results to console |
jakebonney10 | 5:d432702334a9 | 179 | pc.printf("Temperature data:\n\r"); |
jakebonney10 | 5:d432702334a9 | 180 | while(1) { |
jakebonney10 | 5:d432702334a9 | 181 | c = fgetc(file); |
jakebonney10 | 5:d432702334a9 | 182 | if ( c == EOF ) { |
jakebonney10 | 5:d432702334a9 | 183 | break; |
jakebonney10 | 5:d432702334a9 | 184 | } |
jakebonney10 | 5:d432702334a9 | 185 | pc.putc(c); |
jakebonney10 | 5:d432702334a9 | 186 | } |
jakebonney10 | 5:d432702334a9 | 187 | |
jakebonney10 | 5:d432702334a9 | 188 | // Close the file and finish |
jakebonney10 | 5:d432702334a9 | 189 | fclose(file); |
jakebonney10 | 5:d432702334a9 | 190 | pc.printf("Done! Safe to remove SD card\n\r"); |
jakebonney10 | 5:d432702334a9 | 191 | |
jakebonney10 | 5:d432702334a9 | 192 | return 0; |
slicht | 2:552e6feb8709 | 193 | } |
slicht | 1:6b99ffa62cc8 | 194 | |
jakebonney10 | 5:d432702334a9 | 195 | |
slicht | 3:a7f02754bc99 | 196 | //Interpret LCD colors. |
slicht | 2:552e6feb8709 | 197 | int get_LCD_color(int color_integer) |
slicht | 2:552e6feb8709 | 198 | { |
slicht | 2:552e6feb8709 | 199 | switch (color_integer) { |
slicht | 2:552e6feb8709 | 200 | case 0: |
slicht | 2:552e6feb8709 | 201 | return(RED); |
slicht | 2:552e6feb8709 | 202 | case 1: |
slicht | 2:552e6feb8709 | 203 | return(BLUE); |
slicht | 2:552e6feb8709 | 204 | case 2: |
slicht | 2:552e6feb8709 | 205 | return(GREEN); |
slicht | 2:552e6feb8709 | 206 | default: |
slicht | 2:552e6feb8709 | 207 | return(WHITE); |
slicht | 0:8d3812068c6c | 208 | } |
slicht | 1:6b99ffa62cc8 | 209 | } |