Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ulcd_accel/main.cpp@0:6a73d3dc037e, 2014-07-28 (annotated)
- Committer:
- ShawnHymel
- Date:
- Mon Jul 28 20:29:28 2014 +0000
- Revision:
- 0:6a73d3dc037e
Added main.cpp files from each of the demo programs. Libraries are not included.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ShawnHymel | 0:6a73d3dc037e | 1 | // Demo for the uLCD-144-G2 and MMA8452Q 3-axis accelerometer |
| ShawnHymel | 0:6a73d3dc037e | 2 | |
| ShawnHymel | 0:6a73d3dc037e | 3 | #include "mbed.h" |
| ShawnHymel | 0:6a73d3dc037e | 4 | #include "MMA8452Q.h" |
| ShawnHymel | 0:6a73d3dc037e | 5 | #include "uLCD_4DGL.h" |
| ShawnHymel | 0:6a73d3dc037e | 6 | |
| ShawnHymel | 0:6a73d3dc037e | 7 | // Graphic LCD - TX, RX, and RES pins |
| ShawnHymel | 0:6a73d3dc037e | 8 | uLCD_4DGL uLCD(p9,p10,p11); |
| ShawnHymel | 0:6a73d3dc037e | 9 | |
| ShawnHymel | 0:6a73d3dc037e | 10 | // Accelerometer - SDA, SCL, and I2C address |
| ShawnHymel | 0:6a73d3dc037e | 11 | MMA8452Q accel(p28, p27, 0x1D); |
| ShawnHymel | 0:6a73d3dc037e | 12 | |
| ShawnHymel | 0:6a73d3dc037e | 13 | int main() { |
| ShawnHymel | 0:6a73d3dc037e | 14 | |
| ShawnHymel | 0:6a73d3dc037e | 15 | // Initialize uLCD |
| ShawnHymel | 0:6a73d3dc037e | 16 | uLCD.baudrate(115200); |
| ShawnHymel | 0:6a73d3dc037e | 17 | uLCD.background_color(BLACK); |
| ShawnHymel | 0:6a73d3dc037e | 18 | uLCD.cls(); |
| ShawnHymel | 0:6a73d3dc037e | 19 | |
| ShawnHymel | 0:6a73d3dc037e | 20 | // Initialize accelerometer |
| ShawnHymel | 0:6a73d3dc037e | 21 | accel.init(); |
| ShawnHymel | 0:6a73d3dc037e | 22 | |
| ShawnHymel | 0:6a73d3dc037e | 23 | // Initial parameters for the circle |
| ShawnHymel | 0:6a73d3dc037e | 24 | float x = 64; |
| ShawnHymel | 0:6a73d3dc037e | 25 | float y = 64; |
| ShawnHymel | 0:6a73d3dc037e | 26 | int radius = 4; |
| ShawnHymel | 0:6a73d3dc037e | 27 | int speed = 4; |
| ShawnHymel | 0:6a73d3dc037e | 28 | |
| ShawnHymel | 0:6a73d3dc037e | 29 | // Make a ball "fall" in direction of accelerometer |
| ShawnHymel | 0:6a73d3dc037e | 30 | while (1) { |
| ShawnHymel | 0:6a73d3dc037e | 31 | |
| ShawnHymel | 0:6a73d3dc037e | 32 | // Draw a red circle |
| ShawnHymel | 0:6a73d3dc037e | 33 | uLCD.filled_circle((int)x, (int)y, radius, RED); |
| ShawnHymel | 0:6a73d3dc037e | 34 | |
| ShawnHymel | 0:6a73d3dc037e | 35 | // Wait before erasing old circle |
| ShawnHymel | 0:6a73d3dc037e | 36 | wait(0.02); // In seconds |
| ShawnHymel | 0:6a73d3dc037e | 37 | |
| ShawnHymel | 0:6a73d3dc037e | 38 | // Erase old circle |
| ShawnHymel | 0:6a73d3dc037e | 39 | uLCD.filled_circle((int)x, (int)y, radius, BLACK); |
| ShawnHymel | 0:6a73d3dc037e | 40 | |
| ShawnHymel | 0:6a73d3dc037e | 41 | // Move circle. IMPORTANT! Notice how we adjust for sensor orientation! |
| ShawnHymel | 0:6a73d3dc037e | 42 | x -= (speed * accel.readY()); |
| ShawnHymel | 0:6a73d3dc037e | 43 | y -= (speed * accel.readX()); |
| ShawnHymel | 0:6a73d3dc037e | 44 | |
| ShawnHymel | 0:6a73d3dc037e | 45 | // Make circle sit on edges |
| ShawnHymel | 0:6a73d3dc037e | 46 | if ( x <= radius + 1 ) { |
| ShawnHymel | 0:6a73d3dc037e | 47 | x = radius + 1; |
| ShawnHymel | 0:6a73d3dc037e | 48 | } else if ( x >= 126 - radius ) { |
| ShawnHymel | 0:6a73d3dc037e | 49 | x = 126 - radius; |
| ShawnHymel | 0:6a73d3dc037e | 50 | } |
| ShawnHymel | 0:6a73d3dc037e | 51 | if ( y <= radius + 1 ) { |
| ShawnHymel | 0:6a73d3dc037e | 52 | y = radius + 1; |
| ShawnHymel | 0:6a73d3dc037e | 53 | } else if ( y >= 126 - radius ) { |
| ShawnHymel | 0:6a73d3dc037e | 54 | y = 126 - radius; |
| ShawnHymel | 0:6a73d3dc037e | 55 | } |
| ShawnHymel | 0:6a73d3dc037e | 56 | } |
| ShawnHymel | 0:6a73d3dc037e | 57 | } |