![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Publish
Dependencies: mbed MMA8452Q 4DGL-uLCD-SE
main.cpp@1:f86748aa5d16, 2022-05-12 (annotated)
- Committer:
- daroldkellyjr
- Date:
- Thu May 12 15:29:41 2022 +0000
- Revision:
- 1:f86748aa5d16
- Parent:
- 0:ba1ff21c7bad
Publish commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daroldkellyjr | 0:ba1ff21c7bad | 1 | /*------------------------------------------------------------- |
daroldkellyjr | 0:ba1ff21c7bad | 2 | //-------------------------------------------------------------\\ |
daroldkellyjr | 0:ba1ff21c7bad | 3 | || Bought to you by.... || |
daroldkellyjr | 0:ba1ff21c7bad | 4 | || _____ _ _ || |
daroldkellyjr | 0:ba1ff21c7bad | 5 | || \ _ \ _ _ | | / / _ _ _ _ || |
daroldkellyjr | 0:ba1ff21c7bad | 6 | || | | | |__ ___ ___ | | __| | | |/ /___ | | | |\ \ / / || |
daroldkellyjr | 0:ba1ff21c7bad | 7 | || | | | | -\ - \ __ \ |/ _` | | / __\| | | | \ \/ / || |
daroldkellyjr | 0:ba1ff21c7bad | 8 | || | |/ | \ _ / __| || (_| | | |\ \ _\| | | | | | || |
daroldkellyjr | 0:ba1ff21c7bad | 9 | || /____/ _/\_\ \_\ __/_|\__,_| |_| \_\____\| |_| |_| || |
daroldkellyjr | 0:ba1ff21c7bad | 10 | || Electrical Engineer @ University of the District of Colombia || |
daroldkellyjr | 0:ba1ff21c7bad | 11 | || || |
daroldkellyjr | 0:ba1ff21c7bad | 12 | || Date: 5/11/22 || |
daroldkellyjr | 0:ba1ff21c7bad | 13 | || || |
daroldkellyjr | 0:ba1ff21c7bad | 14 | || Description: MECH478 CBE Completion Project || |
daroldkellyjr | 0:ba1ff21c7bad | 15 | || || |
daroldkellyjr | 0:ba1ff21c7bad | 16 | \\-------------------------------------------------------------// |
daroldkellyjr | 0:ba1ff21c7bad | 17 | ------------------------------------------------------------- */ |
daroldkellyjr | 0:ba1ff21c7bad | 18 | |
daroldkellyjr | 0:ba1ff21c7bad | 19 | // uLCD-144-G2 and MMA8452Q 3-axis accelerometer |
daroldkellyjr | 0:ba1ff21c7bad | 20 | |
daroldkellyjr | 0:ba1ff21c7bad | 21 | #include "mbed.h" |
daroldkellyjr | 0:ba1ff21c7bad | 22 | #include "MMA8452Q.h" |
daroldkellyjr | 0:ba1ff21c7bad | 23 | #include "uLCD_4DGL.h" |
daroldkellyjr | 0:ba1ff21c7bad | 24 | |
daroldkellyjr | 0:ba1ff21c7bad | 25 | // Graphic LCD - TX, RX, and RES pins |
daroldkellyjr | 0:ba1ff21c7bad | 26 | uLCD_4DGL uLCD(p9,p10,p11); |
daroldkellyjr | 0:ba1ff21c7bad | 27 | |
daroldkellyjr | 0:ba1ff21c7bad | 28 | // Accelerometer - SDA, SCL, and I2C address |
daroldkellyjr | 0:ba1ff21c7bad | 29 | MMA8452Q accel(p28, p27, 0x1D); |
daroldkellyjr | 0:ba1ff21c7bad | 30 | |
daroldkellyjr | 0:ba1ff21c7bad | 31 | int main() { |
daroldkellyjr | 0:ba1ff21c7bad | 32 | |
daroldkellyjr | 0:ba1ff21c7bad | 33 | // Initialize uLCD |
daroldkellyjr | 0:ba1ff21c7bad | 34 | uLCD.baudrate(115200); |
daroldkellyjr | 0:ba1ff21c7bad | 35 | uLCD.background_color(BLACK); |
daroldkellyjr | 0:ba1ff21c7bad | 36 | uLCD.cls(); |
daroldkellyjr | 0:ba1ff21c7bad | 37 | |
daroldkellyjr | 0:ba1ff21c7bad | 38 | // Initialize accelerometer |
daroldkellyjr | 0:ba1ff21c7bad | 39 | accel.init(); |
daroldkellyjr | 0:ba1ff21c7bad | 40 | |
daroldkellyjr | 0:ba1ff21c7bad | 41 | // Initial parameters for the circle |
daroldkellyjr | 0:ba1ff21c7bad | 42 | float x = 64; |
daroldkellyjr | 0:ba1ff21c7bad | 43 | float y = 64; |
daroldkellyjr | 0:ba1ff21c7bad | 44 | int radius = 4; |
daroldkellyjr | 0:ba1ff21c7bad | 45 | int speed = 4; |
daroldkellyjr | 0:ba1ff21c7bad | 46 | |
daroldkellyjr | 0:ba1ff21c7bad | 47 | // Make a ball "fall" in direction of accelerometer |
daroldkellyjr | 0:ba1ff21c7bad | 48 | while (1) { |
daroldkellyjr | 0:ba1ff21c7bad | 49 | |
daroldkellyjr | 0:ba1ff21c7bad | 50 | // Draw a red circle |
daroldkellyjr | 0:ba1ff21c7bad | 51 | uLCD.filled_circle((int)x, (int)y, radius, RED); |
daroldkellyjr | 0:ba1ff21c7bad | 52 | |
daroldkellyjr | 0:ba1ff21c7bad | 53 | // Wait before erasing old circle |
daroldkellyjr | 0:ba1ff21c7bad | 54 | wait(0.02); // In seconds |
daroldkellyjr | 0:ba1ff21c7bad | 55 | |
daroldkellyjr | 0:ba1ff21c7bad | 56 | // Erase old circle |
daroldkellyjr | 0:ba1ff21c7bad | 57 | uLCD.filled_circle((int)x, (int)y, radius, BLACK); |
daroldkellyjr | 0:ba1ff21c7bad | 58 | |
daroldkellyjr | 0:ba1ff21c7bad | 59 | // Move circle. IMPORTANT! Notice how we adjust for sensor orientation! |
daroldkellyjr | 0:ba1ff21c7bad | 60 | x -= (speed * accel.readY()); |
daroldkellyjr | 0:ba1ff21c7bad | 61 | y -= (speed * accel.readX()); |
daroldkellyjr | 0:ba1ff21c7bad | 62 | |
daroldkellyjr | 0:ba1ff21c7bad | 63 | // Make circle sit on edges |
daroldkellyjr | 0:ba1ff21c7bad | 64 | if ( x <= radius + 1 ) { |
daroldkellyjr | 0:ba1ff21c7bad | 65 | x = radius + 1; |
daroldkellyjr | 0:ba1ff21c7bad | 66 | } else if ( x >= 126 - radius ) { |
daroldkellyjr | 0:ba1ff21c7bad | 67 | x = 126 - radius; |
daroldkellyjr | 0:ba1ff21c7bad | 68 | } |
daroldkellyjr | 0:ba1ff21c7bad | 69 | if ( y <= radius + 1 ) { |
daroldkellyjr | 0:ba1ff21c7bad | 70 | y = radius + 1; |
daroldkellyjr | 0:ba1ff21c7bad | 71 | } else if ( y >= 126 - radius ) { |
daroldkellyjr | 0:ba1ff21c7bad | 72 | y = 126 - radius; |
daroldkellyjr | 0:ba1ff21c7bad | 73 | } |
daroldkellyjr | 0:ba1ff21c7bad | 74 | } |
daroldkellyjr | 0:ba1ff21c7bad | 75 | } |