Yusuke Suzuki
/
balancer
The LEDs flashes faster, depending on the inclination of the sensor. Accelerometer: MMA8451Q
main.cpp@0:3e3ddecc2783, 2014-03-09 (annotated)
- Committer:
- junshoc
- Date:
- Sun Mar 09 15:01:24 2014 +0000
- Revision:
- 0:3e3ddecc2783
The LEDs flash faster, depending on the inclination of the sensor.; ; Accelerometer : MMA8451Q
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
junshoc | 0:3e3ddecc2783 | 1 | // |
junshoc | 0:3e3ddecc2783 | 2 | // balance meter |
junshoc | 0:3e3ddecc2783 | 3 | // |
junshoc | 0:3e3ddecc2783 | 4 | |
junshoc | 0:3e3ddecc2783 | 5 | #include "mbed.h" |
junshoc | 0:3e3ddecc2783 | 6 | #include "MMA8451Q.h" |
junshoc | 0:3e3ddecc2783 | 7 | #include <iostream> |
junshoc | 0:3e3ddecc2783 | 8 | #include "math.h" |
junshoc | 0:3e3ddecc2783 | 9 | |
junshoc | 0:3e3ddecc2783 | 10 | #define TIME 0.05 // wait (balanced) |
junshoc | 0:3e3ddecc2783 | 11 | #define MINIMUM_TIME 0.003 // wait (not balanced) |
junshoc | 0:3e3ddecc2783 | 12 | |
junshoc | 0:3e3ddecc2783 | 13 | PwmOut gled(LED1); |
junshoc | 0:3e3ddecc2783 | 14 | PwmOut rled(LED2); |
junshoc | 0:3e3ddecc2783 | 15 | MMA8451Q acc(PTE25, PTE24, 0x1D<<1); |
junshoc | 0:3e3ddecc2783 | 16 | |
junshoc | 0:3e3ddecc2783 | 17 | int main() |
junshoc | 0:3e3ddecc2783 | 18 | { |
junshoc | 0:3e3ddecc2783 | 19 | gled.period_ms( 1 ); |
junshoc | 0:3e3ddecc2783 | 20 | rled.period_ms( 1 ); |
junshoc | 0:3e3ddecc2783 | 21 | |
junshoc | 0:3e3ddecc2783 | 22 | while (1) |
junshoc | 0:3e3ddecc2783 | 23 | { |
junshoc | 0:3e3ddecc2783 | 24 | for ( int i=0; i<360; i+=10 ) { |
junshoc | 0:3e3ddecc2783 | 25 | gled = cos( i*2.0*3.14/360 ) * 0.5 + 0.5; |
junshoc | 0:3e3ddecc2783 | 26 | rled = 1.0 - (cos( i*2.0*3.14/360 ) * 0.5 + 0.5); |
junshoc | 0:3e3ddecc2783 | 27 | |
junshoc | 0:3e3ddecc2783 | 28 | wait(max(TIME - (abs(acc.getAccX()) + abs(acc.getAccY())) * TIME, MINIMUM_TIME)); |
junshoc | 0:3e3ddecc2783 | 29 | } |
junshoc | 0:3e3ddecc2783 | 30 | } |
junshoc | 0:3e3ddecc2783 | 31 | } |