Mark Oehlberg
/
Accelerometer_to_color
This program changes the accelerometer data in to an RGB color
main.cpp@0:6c4e46da8d65, 2014-08-01 (annotated)
- Committer:
- oehlberg
- Date:
- Fri Aug 01 18:29:07 2014 +0000
- Revision:
- 0:6c4e46da8d65
Accelerometer to RGB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oehlberg | 0:6c4e46da8d65 | 1 | #include "mbed.h" |
oehlberg | 0:6c4e46da8d65 | 2 | #include "MMA8451Q.h"//This is the onboard accelerometer |
oehlberg | 0:6c4e46da8d65 | 3 | |
oehlberg | 0:6c4e46da8d65 | 4 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)//This is the board I have and I guess they changed some pins around |
oehlberg | 0:6c4e46da8d65 | 5 | PinName const SDA = PTE25; |
oehlberg | 0:6c4e46da8d65 | 6 | PinName const SCL = PTE24; |
oehlberg | 0:6c4e46da8d65 | 7 | #elif defined (TARGET_KL05Z) |
oehlberg | 0:6c4e46da8d65 | 8 | PinName const SDA = PTB4; |
oehlberg | 0:6c4e46da8d65 | 9 | PinName const SCL = PTB3; |
oehlberg | 0:6c4e46da8d65 | 10 | #else |
oehlberg | 0:6c4e46da8d65 | 11 | #error TARGET NOT DEFINED |
oehlberg | 0:6c4e46da8d65 | 12 | #endif |
oehlberg | 0:6c4e46da8d65 | 13 | |
oehlberg | 0:6c4e46da8d65 | 14 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
oehlberg | 0:6c4e46da8d65 | 15 | |
oehlberg | 0:6c4e46da8d65 | 16 | //Serial pc(USBTX, USBRX);//The accelerometer didn't have to do this...I guess this is how you could change baud rates |
oehlberg | 0:6c4e46da8d65 | 17 | //int baudrate = 115200; |
oehlberg | 0:6c4e46da8d65 | 18 | |
oehlberg | 0:6c4e46da8d65 | 19 | int main(void) { |
oehlberg | 0:6c4e46da8d65 | 20 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
oehlberg | 0:6c4e46da8d65 | 21 | //pc.baud(baudrate); |
oehlberg | 0:6c4e46da8d65 | 22 | PwmOut rled(LED1);//pwms are 0-1 with 0 being off and 1 on |
oehlberg | 0:6c4e46da8d65 | 23 | PwmOut gled(LED2); |
oehlberg | 0:6c4e46da8d65 | 24 | PwmOut bled(LED3); |
oehlberg | 0:6c4e46da8d65 | 25 | |
oehlberg | 0:6c4e46da8d65 | 26 | printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
oehlberg | 0:6c4e46da8d65 | 27 | float x,y,z,accx, accy, accz; |
oehlberg | 0:6c4e46da8d65 | 28 | |
oehlberg | 0:6c4e46da8d65 | 29 | while (true) { |
oehlberg | 0:6c4e46da8d65 | 30 | |
oehlberg | 0:6c4e46da8d65 | 31 | accx = abs(acc.getAccX()); |
oehlberg | 0:6c4e46da8d65 | 32 | accy = abs(acc.getAccY()); |
oehlberg | 0:6c4e46da8d65 | 33 | accz = abs(acc.getAccZ()); |
oehlberg | 0:6c4e46da8d65 | 34 | |
oehlberg | 0:6c4e46da8d65 | 35 | x = rled = 1.0 - accx; |
oehlberg | 0:6c4e46da8d65 | 36 | y = gled = 1.0 - accy; |
oehlberg | 0:6c4e46da8d65 | 37 | z = bled = 1.0 - accz; |
oehlberg | 0:6c4e46da8d65 | 38 | |
oehlberg | 0:6c4e46da8d65 | 39 | wait(0.5);//I don't think 12c can poll as fast as the processor can go. The delay is needed for this |
oehlberg | 0:6c4e46da8d65 | 40 | printf("Red: %1.5f, Green: %1.5f, Blue: %1.5f\t Acceleration X: %1.10f Y: %fZ: %1.5f\n\r", x, y, z,accx,accy,accz);//so \n goes down one line, but does not get me to the beggining of a line. \r gets to the beginning of the line so the formatting is right |
oehlberg | 0:6c4e46da8d65 | 41 | } |
oehlberg | 0:6c4e46da8d65 | 42 | } |