![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
assignment 6 360- part 1 is complete-freeze balls on lcd screen with a hardware interrupt/button
Dependencies: 4DGL-uLCD-SE MMA8452Q_withfreefall bouncing_ball mbed
Fork of Assignment6 by
main.cpp@5:62b2d643b132, 2017-12-07 (annotated)
- Committer:
- jakebonney10
- Date:
- Thu Dec 07 14:51:51 2017 +0000
- Revision:
- 5:62b2d643b132
- Parent:
- 3:a7f02754bc99
Freezes balls on lcd screen with a button (p17)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
slicht | 0:8d3812068c6c | 1 | #include "mbed.h" |
slicht | 2:552e6feb8709 | 2 | #include "MMA8452Q.h" //acceleromater library |
slicht | 2:552e6feb8709 | 3 | #include "uLCD_4DGL.h" //LCD library |
slicht | 2:552e6feb8709 | 4 | #include "bouncing_ball.h" //new ball phyics library |
slicht | 1:6b99ffa62cc8 | 5 | |
slicht | 2:552e6feb8709 | 6 | #define UPDATE_TIME_S 0.02 |
slicht | 2:552e6feb8709 | 7 | #define START_X_1 10 |
slicht | 2:552e6feb8709 | 8 | #define START_Y_1 10 |
slicht | 2:552e6feb8709 | 9 | #define START_X_2 20 |
slicht | 2:552e6feb8709 | 10 | #define START_Y_2 20 |
slicht | 2:552e6feb8709 | 11 | #define RADIUS_1 6 |
slicht | 2:552e6feb8709 | 12 | #define RADIUS_2 3 |
slicht | 0:8d3812068c6c | 13 | |
slicht | 2:552e6feb8709 | 14 | #define DEBUG_MODE 0 |
slicht | 2:552e6feb8709 | 15 | |
slicht | 2:552e6feb8709 | 16 | //Function prototype for color selection function: |
slicht | 2:552e6feb8709 | 17 | int get_LCD_color(int color_integer); |
slicht | 1:6b99ffa62cc8 | 18 | |
slicht | 1:6b99ffa62cc8 | 19 | // Graphic LCD - TX, RX, and RES pins |
slicht | 2:552e6feb8709 | 20 | uLCD_4DGL uLCD(p9,p10,p11); //initialize a driver object for an LCD connected on pins 9-11 |
slicht | 1:6b99ffa62cc8 | 21 | |
slicht | 1:6b99ffa62cc8 | 22 | // Accelerometer - SDA, SCL, and I2C address |
slicht | 2:552e6feb8709 | 23 | MMA8452Q accel(p28, p27, 0x1D); //initialize a driver object for an accelerometer connected on pins 27-28. |
slicht | 1:6b99ffa62cc8 | 24 | |
slicht | 2:552e6feb8709 | 25 | physics_ball ball1; //initialize two balls for bouncing |
slicht | 2:552e6feb8709 | 26 | physics_ball ball2; //the default states from the library will be used initially |
slicht | 0:8d3812068c6c | 27 | |
jakebonney10 | 5:62b2d643b132 | 28 | // External Interrupts |
jakebonney10 | 5:62b2d643b132 | 29 | InterruptIn button(p18); |
jakebonney10 | 5:62b2d643b132 | 30 | InterruptIn freefall_acc(p17); |
jakebonney10 | 5:62b2d643b132 | 31 | Timer debounce; |
jakebonney10 | 5:62b2d643b132 | 32 | |
jakebonney10 | 5:62b2d643b132 | 33 | |
jakebonney10 | 5:62b2d643b132 | 34 | // Response to the hardware interrupt(button being pressed), set speed of balls = 0 |
jakebonney10 | 5:62b2d643b132 | 35 | void Freeze_button(){ |
jakebonney10 | 5:62b2d643b132 | 36 | ball1.set_state(ball1.posx, ball1.posy,0,0); |
jakebonney10 | 5:62b2d643b132 | 37 | ball2.set_state(ball2.posx, ball2.posy,0,0); |
jakebonney10 | 5:62b2d643b132 | 38 | wait(2); |
jakebonney10 | 5:62b2d643b132 | 39 | debounce.reset(); |
jakebonney10 | 5:62b2d643b132 | 40 | } |
jakebonney10 | 5:62b2d643b132 | 41 | //void Freeze_ff(){ |
jakebonney10 | 5:62b2d643b132 | 42 | //if |
jakebonney10 | 5:62b2d643b132 | 43 | |
slicht | 2:552e6feb8709 | 44 | int main() |
slicht | 2:552e6feb8709 | 45 | { |
jakebonney10 | 5:62b2d643b132 | 46 | button.mode(PullUp); // use internal resistors in button in order to keep output reading "high", then when button switched it will fall |
jakebonney10 | 5:62b2d643b132 | 47 | button.fall(&Freeze_button); //freeze accelerometer for 2 sec when button is pressed |
jakebonney10 | 5:62b2d643b132 | 48 | |
slicht | 1:6b99ffa62cc8 | 49 | // Initialize uLCD |
slicht | 1:6b99ffa62cc8 | 50 | uLCD.baudrate(115200); |
slicht | 1:6b99ffa62cc8 | 51 | uLCD.background_color(BLACK); |
slicht | 1:6b99ffa62cc8 | 52 | uLCD.cls(); |
slicht | 1:6b99ffa62cc8 | 53 | |
slicht | 1:6b99ffa62cc8 | 54 | // Initialize accelerometer |
slicht | 1:6b99ffa62cc8 | 55 | accel.init(); |
slicht | 1:6b99ffa62cc8 | 56 | |
slicht | 2:552e6feb8709 | 57 | //Initialize balls: |
slicht | 2:552e6feb8709 | 58 | ball1.set_state(START_X_1,START_Y_1,0,0); //speeds are set to zero |
slicht | 2:552e6feb8709 | 59 | ball1.set_state(START_X_2,START_Y_2,0,0); |
slicht | 2:552e6feb8709 | 60 | |
slicht | 2:552e6feb8709 | 61 | //Set ball radius and color: |
slicht | 2:552e6feb8709 | 62 | ball1.set_param(RADIUS_1,0); //color is unimportant |
slicht | 2:552e6feb8709 | 63 | ball2.set_param(RADIUS_2,1); //just making sure the colors are different |
slicht | 2:552e6feb8709 | 64 | |
slicht | 2:552e6feb8709 | 65 | /* Make the balls "fall" in direction of accelerometer by forcing the ball objects |
slicht | 2:552e6feb8709 | 66 | to update themselves at regular intervals, and te drawing the locations reported |
slicht | 2:552e6feb8709 | 67 | by the ball objects on the LCD screen: */ |
slicht | 2:552e6feb8709 | 68 | while (1) { //execute 'forever' |
slicht | 3:a7f02754bc99 | 69 | // Draw circles in the x and y positions stored by the ball objects: |
slicht | 2:552e6feb8709 | 70 | uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, get_LCD_color(ball1.color)); |
slicht | 2:552e6feb8709 | 71 | uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, get_LCD_color(ball2.color)); |
slicht | 2:552e6feb8709 | 72 | |
slicht | 2:552e6feb8709 | 73 | // Wait before erasing old circles: |
slicht | 2:552e6feb8709 | 74 | wait(UPDATE_TIME_S); // In seconds |
slicht | 2:552e6feb8709 | 75 | |
slicht | 2:552e6feb8709 | 76 | // Erase old circles by writing over there locations using the screen color: |
slicht | 2:552e6feb8709 | 77 | uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLACK); |
slicht | 2:552e6feb8709 | 78 | uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, BLACK); |
slicht | 1:6b99ffa62cc8 | 79 | |
slicht | 2:552e6feb8709 | 80 | // Force the objects 'ball1' and 'ball2' to update their stored positions |
slicht | 2:552e6feb8709 | 81 | // and velocities: |
slicht | 2:552e6feb8709 | 82 | ball1.update(UPDATE_TIME_S,accel); |
slicht | 2:552e6feb8709 | 83 | ball2.update(UPDATE_TIME_S,accel); |
slicht | 2:552e6feb8709 | 84 | |
slicht | 2:552e6feb8709 | 85 | if (DEBUG_MODE) { |
slicht | 3:a7f02754bc99 | 86 | //If compiled with DEBUG_MODE flag raised, print values to screen. |
slicht | 2:552e6feb8709 | 87 | uLCD.locate(0,4); |
slicht | 2:552e6feb8709 | 88 | uLCD.printf("X: %d.1\nY: %.1d",ball1.posx,ball1.posy); |
slicht | 2:552e6feb8709 | 89 | |
slicht | 2:552e6feb8709 | 90 | uLCD.locate(0,6); |
slicht | 2:552e6feb8709 | 91 | uLCD.printf("VX: %f.1\nVY: %.1f",ball1.speedx,ball1.speedy); |
slicht | 1:6b99ffa62cc8 | 92 | |
slicht | 2:552e6feb8709 | 93 | uLCD.locate(0,10); |
slicht | 2:552e6feb8709 | 94 | uLCD.printf("AX: %f.1\nAY: %.1f\nAZ: %0.1f",accel.readX(),accel.readY(),accel.readZ()); |
slicht | 2:552e6feb8709 | 95 | } |
slicht | 2:552e6feb8709 | 96 | } |
slicht | 2:552e6feb8709 | 97 | } |
slicht | 1:6b99ffa62cc8 | 98 | |
slicht | 3:a7f02754bc99 | 99 | //Interpret LCD colors. |
slicht | 2:552e6feb8709 | 100 | int get_LCD_color(int color_integer) |
slicht | 2:552e6feb8709 | 101 | { |
slicht | 2:552e6feb8709 | 102 | switch (color_integer) { |
slicht | 2:552e6feb8709 | 103 | case 0: |
slicht | 2:552e6feb8709 | 104 | return(RED); |
slicht | 2:552e6feb8709 | 105 | case 1: |
slicht | 2:552e6feb8709 | 106 | return(BLUE); |
slicht | 2:552e6feb8709 | 107 | case 2: |
slicht | 2:552e6feb8709 | 108 | return(GREEN); |
slicht | 2:552e6feb8709 | 109 | default: |
slicht | 2:552e6feb8709 | 110 | return(WHITE); |
slicht | 0:8d3812068c6c | 111 | } |
slicht | 1:6b99ffa62cc8 | 112 | } |