Darold Kelly / Mbed 2 deprecated MECH478ProjectCode

Dependencies:   mbed MMA8452Q 4DGL-uLCD-SE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*-------------------------------------------------------------
00002 //-------------------------------------------------------------\\
00003 ||        Bought to you by....                                  ||
00004 ||    _____                         _   _                       ||
00005 ||    \  _ \              _     _  | | / /    _   _  _     _    ||
00006 ||    | | | |__ ___  ___ | | __| | | |/ /___ | | | |\ \  / /    ||
00007 ||    | | | | -\  - \ __ \ |/ _` | |   /  __\| | | | \ \/ /     ||
00008 ||    | |/  |   \ _ / __| || (_| | | |\ \  _\| | | |  | |       ||
00009 ||    /____/ _/\_\ \_\ __/_|\__,_| |_| \_\____\| |_|  |_|       ||
00010 || Electrical Engineer @ University of the District of Colombia ||
00011 ||                                                              ||
00012 || Date: 5/11/22                                                ||
00013 ||                                                              ||
00014 || Description:  MECH478  CBE Completion Project                ||
00015 ||                                                              ||
00016 \\-------------------------------------------------------------//
00017   -------------------------------------------------------------  */
00018 
00019 // uLCD-144-G2 and MMA8452Q 3-axis accelerometer
00020 
00021 #include "mbed.h"
00022 #include "MMA8452Q.h"
00023 #include "uLCD_4DGL.h"
00024 
00025 // Graphic LCD - TX, RX, and RES pins
00026 uLCD_4DGL uLCD(p9,p10,p11);
00027 
00028 // Accelerometer - SDA, SCL, and I2C address
00029 MMA8452Q accel(p28, p27, 0x1D);
00030 
00031 int main() {
00032 
00033     // Initialize uLCD
00034     uLCD.baudrate(115200);
00035     uLCD.background_color(BLACK);
00036     uLCD.cls();
00037 
00038     // Initialize accelerometer
00039     accel.init();
00040 
00041     // Initial parameters for the circle
00042     float x = 64;
00043     float y = 64;
00044     int radius = 4;
00045     int speed = 4;
00046 
00047     // Make a ball "fall" in direction of accelerometer
00048     while (1) {
00049 
00050         // Draw a red circle
00051         uLCD.filled_circle((int)x, (int)y, radius, RED);
00052 
00053         // Wait before erasing old circle
00054         wait(0.02);         // In seconds
00055 
00056         // Erase old circle
00057         uLCD.filled_circle((int)x, (int)y, radius, BLACK);
00058 
00059         // Move circle. IMPORTANT! Notice how we adjust for sensor orientation!
00060         x -= (speed * accel.readY());
00061         y -= (speed * accel.readX());
00062 
00063         // Make circle sit on edges
00064         if ( x <= radius + 1 ) {
00065             x = radius + 1;
00066         } else if ( x >= 126 - radius ) {
00067             x = 126 - radius;
00068         }
00069         if ( y <= radius + 1 ) {
00070             y = radius + 1;
00071         } else if ( y >= 126 - radius ) {
00072             y = 126 - radius;
00073         }
00074     }
00075 }