actual thing accelerator
Dependencies: 4DGL-uLCD-SE ulcd_accel mbed
main.cpp@0:fd0d8678abae, 2016-01-30 (annotated)
- Committer:
- wye11
- Date:
- Sat Jan 30 07:12:15 2016 +0000
- Revision:
- 0:fd0d8678abae
huh
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wye11 | 0:fd0d8678abae | 1 | // Demo for the uLCD-144-G2 and MMA8452Q 3-axis accelerometer |
wye11 | 0:fd0d8678abae | 2 | |
wye11 | 0:fd0d8678abae | 3 | #include "mbed.h" |
wye11 | 0:fd0d8678abae | 4 | #include "MMA8452Q.h" |
wye11 | 0:fd0d8678abae | 5 | #include "uLCD_4DGL.h" |
wye11 | 0:fd0d8678abae | 6 | |
wye11 | 0:fd0d8678abae | 7 | // Graphic LCD - TX, RX, and RES pins |
wye11 | 0:fd0d8678abae | 8 | uLCD_4DGL uLCD(p9,p10,p11); |
wye11 | 0:fd0d8678abae | 9 | |
wye11 | 0:fd0d8678abae | 10 | // Accelerometer - SDA, SCL, and I2C address |
wye11 | 0:fd0d8678abae | 11 | MMA8452Q accel(p28, p27, 0x1D); |
wye11 | 0:fd0d8678abae | 12 | |
wye11 | 0:fd0d8678abae | 13 | int main() { |
wye11 | 0:fd0d8678abae | 14 | |
wye11 | 0:fd0d8678abae | 15 | // Initialize uLCD |
wye11 | 0:fd0d8678abae | 16 | uLCD.baudrate(115200); |
wye11 | 0:fd0d8678abae | 17 | uLCD.background_color(BLACK); |
wye11 | 0:fd0d8678abae | 18 | uLCD.cls(); |
wye11 | 0:fd0d8678abae | 19 | |
wye11 | 0:fd0d8678abae | 20 | // Initialize accelerometer |
wye11 | 0:fd0d8678abae | 21 | accel.init(); |
wye11 | 0:fd0d8678abae | 22 | |
wye11 | 0:fd0d8678abae | 23 | // Initial parameters for the circle |
wye11 | 0:fd0d8678abae | 24 | float x = 64; |
wye11 | 0:fd0d8678abae | 25 | float y = 64; |
wye11 | 0:fd0d8678abae | 26 | int radius = 4; |
wye11 | 0:fd0d8678abae | 27 | int speed = 4; |
wye11 | 0:fd0d8678abae | 28 | |
wye11 | 0:fd0d8678abae | 29 | // Make a ball "fall" in direction of accelerometer |
wye11 | 0:fd0d8678abae | 30 | while (1) { |
wye11 | 0:fd0d8678abae | 31 | |
wye11 | 0:fd0d8678abae | 32 | // Draw a red circle |
wye11 | 0:fd0d8678abae | 33 | uLCD.filled_circle((int)x, (int)y, radius, RED); |
wye11 | 0:fd0d8678abae | 34 | |
wye11 | 0:fd0d8678abae | 35 | // Wait before erasing old circle |
wye11 | 0:fd0d8678abae | 36 | wait(0.02); // In seconds |
wye11 | 0:fd0d8678abae | 37 | |
wye11 | 0:fd0d8678abae | 38 | // Erase old circle |
wye11 | 0:fd0d8678abae | 39 | uLCD.filled_circle((int)x, (int)y, radius, BLACK); |
wye11 | 0:fd0d8678abae | 40 | |
wye11 | 0:fd0d8678abae | 41 | // Move circle. IMPORTANT! Notice how we adjust for sensor orientation! |
wye11 | 0:fd0d8678abae | 42 | x -= (speed * accel.readY()); |
wye11 | 0:fd0d8678abae | 43 | y -= (speed * accel.readX()); |
wye11 | 0:fd0d8678abae | 44 | |
wye11 | 0:fd0d8678abae | 45 | // Make circle sit on edges |
wye11 | 0:fd0d8678abae | 46 | if ( x <= radius + 1 ) { |
wye11 | 0:fd0d8678abae | 47 | x = radius + 1; |
wye11 | 0:fd0d8678abae | 48 | } else if ( x >= 126 - radius ) { |
wye11 | 0:fd0d8678abae | 49 | x = 126 - radius; |
wye11 | 0:fd0d8678abae | 50 | } |
wye11 | 0:fd0d8678abae | 51 | if ( y <= radius + 1 ) { |
wye11 | 0:fd0d8678abae | 52 | y = radius + 1; |
wye11 | 0:fd0d8678abae | 53 | } else if ( y >= 126 - radius ) { |
wye11 | 0:fd0d8678abae | 54 | y = 126 - radius; |
wye11 | 0:fd0d8678abae | 55 | } |
wye11 | 0:fd0d8678abae | 56 | } |
wye11 | 0:fd0d8678abae | 57 | } |