ee202hw1
Dependencies: Useless SB TSI mbed
Fork of Final_Homework1 by
main.cpp@0:c8a7071d8692, 2014-02-08 (annotated)
- Committer:
- bmdlh
- Date:
- Sat Feb 08 22:23:55 2014 +0000
- Revision:
- 0:c8a7071d8692
- Child:
- 1:a00bb47c1028
uesless
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bmdlh | 0:c8a7071d8692 | 1 | //KL46Z try out |
bmdlh | 0:c8a7071d8692 | 2 | #include "mbed.h" |
bmdlh | 0:c8a7071d8692 | 3 | #include "TSISensor.h" |
bmdlh | 0:c8a7071d8692 | 4 | #include "MMA8451Q.h" |
bmdlh | 0:c8a7071d8692 | 5 | #include "MAG3110.h" |
bmdlh | 0:c8a7071d8692 | 6 | #include <cstdlib> |
bmdlh | 0:c8a7071d8692 | 7 | #include <iostream> |
bmdlh | 0:c8a7071d8692 | 8 | #define OFF 0; |
bmdlh | 0:c8a7071d8692 | 9 | #define ON 1; |
bmdlh | 0:c8a7071d8692 | 10 | |
bmdlh | 0:c8a7071d8692 | 11 | Serial pc(USBTX, USBRX); |
bmdlh | 0:c8a7071d8692 | 12 | |
bmdlh | 0:c8a7071d8692 | 13 | TSISensor tsi; |
bmdlh | 0:c8a7071d8692 | 14 | MMA8451Q acc51(PTE25, PTE24, 0x1D<<1); |
bmdlh | 0:c8a7071d8692 | 15 | MAG3110 mag(PTE25, PTE24); |
bmdlh | 0:c8a7071d8692 | 16 | |
bmdlh | 0:c8a7071d8692 | 17 | PwmOut rled(PTE29); |
bmdlh | 0:c8a7071d8692 | 18 | PwmOut gled(PTD5); |
bmdlh | 0:c8a7071d8692 | 19 | AnalogIn lightSensor(PTE22); |
bmdlh | 0:c8a7071d8692 | 20 | DigitalIn SW12(PTC3); |
bmdlh | 0:c8a7071d8692 | 21 | DigitalIn SW32(PTC12); |
bmdlh | 0:c8a7071d8692 | 22 | //AnalogOut lcd03(PTB23); |
bmdlh | 0:c8a7071d8692 | 23 | //PwmOut lcd07(PTB7); |
bmdlh | 0:c8a7071d8692 | 24 | |
bmdlh | 0:c8a7071d8692 | 25 | void calXY() //magnetometer calibration: finding max and min of X, Y axis |
bmdlh | 0:c8a7071d8692 | 26 | { |
bmdlh | 0:c8a7071d8692 | 27 | int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; |
bmdlh | 0:c8a7071d8692 | 28 | |
bmdlh | 0:c8a7071d8692 | 29 | rled = ON; |
bmdlh | 0:c8a7071d8692 | 30 | |
bmdlh | 0:c8a7071d8692 | 31 | printf("Waiting for initial press\n"); |
bmdlh | 0:c8a7071d8692 | 32 | // Wait for slider to be pressed |
bmdlh | 0:c8a7071d8692 | 33 | while( tsi.readDistance() == 0 ) { |
bmdlh | 0:c8a7071d8692 | 34 | rled = ON; |
bmdlh | 0:c8a7071d8692 | 35 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 36 | rled = OFF; |
bmdlh | 0:c8a7071d8692 | 37 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 38 | } |
bmdlh | 0:c8a7071d8692 | 39 | |
bmdlh | 0:c8a7071d8692 | 40 | printf("Waiting for release\n"); |
bmdlh | 0:c8a7071d8692 | 41 | |
bmdlh | 0:c8a7071d8692 | 42 | // Wait for release |
bmdlh | 0:c8a7071d8692 | 43 | while( tsi.readDistance() != 0 ) { |
bmdlh | 0:c8a7071d8692 | 44 | rled = OFF; |
bmdlh | 0:c8a7071d8692 | 45 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 46 | rled = ON; |
bmdlh | 0:c8a7071d8692 | 47 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 48 | } |
bmdlh | 0:c8a7071d8692 | 49 | rled = OFF; |
bmdlh | 0:c8a7071d8692 | 50 | wait(0.5); |
bmdlh | 0:c8a7071d8692 | 51 | |
bmdlh | 0:c8a7071d8692 | 52 | printf("Rotate\n"); |
bmdlh | 0:c8a7071d8692 | 53 | |
bmdlh | 0:c8a7071d8692 | 54 | tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB); |
bmdlh | 0:c8a7071d8692 | 55 | tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB); |
bmdlh | 0:c8a7071d8692 | 56 | |
bmdlh | 0:c8a7071d8692 | 57 | while(tsi.readDistance() == 0) { |
bmdlh | 0:c8a7071d8692 | 58 | gled = ON; |
bmdlh | 0:c8a7071d8692 | 59 | wait(0.1); |
bmdlh | 0:c8a7071d8692 | 60 | gled = OFF; |
bmdlh | 0:c8a7071d8692 | 61 | wait(0.1); |
bmdlh | 0:c8a7071d8692 | 62 | newX = mag.readVal(MAG_OUT_X_MSB); |
bmdlh | 0:c8a7071d8692 | 63 | newY = mag.readVal(MAG_OUT_Y_MSB); |
bmdlh | 0:c8a7071d8692 | 64 | if (newX > tempXmax) tempXmax = newX; |
bmdlh | 0:c8a7071d8692 | 65 | if (newX < tempXmin) tempXmin = newX; |
bmdlh | 0:c8a7071d8692 | 66 | if (newY > tempYmax) tempYmax = newY; |
bmdlh | 0:c8a7071d8692 | 67 | if (newY < tempYmin) tempYmin = newY; |
bmdlh | 0:c8a7071d8692 | 68 | } |
bmdlh | 0:c8a7071d8692 | 69 | |
bmdlh | 0:c8a7071d8692 | 70 | mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); |
bmdlh | 0:c8a7071d8692 | 71 | |
bmdlh | 0:c8a7071d8692 | 72 | // Wait for release |
bmdlh | 0:c8a7071d8692 | 73 | while( tsi.readDistance() != 0 ) { |
bmdlh | 0:c8a7071d8692 | 74 | gled = OFF; |
bmdlh | 0:c8a7071d8692 | 75 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 76 | gled = ON; |
bmdlh | 0:c8a7071d8692 | 77 | wait(0.2); |
bmdlh | 0:c8a7071d8692 | 78 | } |
bmdlh | 0:c8a7071d8692 | 79 | gled = OFF; |
bmdlh | 0:c8a7071d8692 | 80 | wait(1.0); |
bmdlh | 0:c8a7071d8692 | 81 | } |
bmdlh | 0:c8a7071d8692 | 82 | |
bmdlh | 0:c8a7071d8692 | 83 | int main() |
bmdlh | 0:c8a7071d8692 | 84 | { |
bmdlh | 0:c8a7071d8692 | 85 | using namespace std; |
bmdlh | 0:c8a7071d8692 | 86 | |
bmdlh | 0:c8a7071d8692 | 87 | float onTime = 1.0; |
bmdlh | 0:c8a7071d8692 | 88 | float offTime = 0.0; |
bmdlh | 0:c8a7071d8692 | 89 | float holdTime = 3.0; |
bmdlh | 0:c8a7071d8692 | 90 | bool on = true; |
bmdlh | 0:c8a7071d8692 | 91 | bool off = false; |
bmdlh | 0:c8a7071d8692 | 92 | int magX = 0, magY = 0, magZ = 0; |
bmdlh | 0:c8a7071d8692 | 93 | //lcd03.write(0.33); |
bmdlh | 0:c8a7071d8692 | 94 | //lcd07.write(0.25); |
bmdlh | 0:c8a7071d8692 | 95 | //lcd07.period_ms(10); |
bmdlh | 0:c8a7071d8692 | 96 | pc.baud(9600); |
bmdlh | 0:c8a7071d8692 | 97 | calXY(); |
bmdlh | 0:c8a7071d8692 | 98 | mag.Overwrite_dr_osr(40,16); |
bmdlh | 0:c8a7071d8692 | 99 | acc51.Overwrite_dr(200); |
bmdlh | 0:c8a7071d8692 | 100 | tsi.Overwrite_ps_nscn(5,5); |
bmdlh | 0:c8a7071d8692 | 101 | while(true) { |
bmdlh | 0:c8a7071d8692 | 102 | |
bmdlh | 0:c8a7071d8692 | 103 | rled = onTime - abs(acc51.getAccX()); |
bmdlh | 0:c8a7071d8692 | 104 | gled = onTime - abs(acc51.getAccY()); |
bmdlh | 0:c8a7071d8692 | 105 | mag.getValues(&magX, &magY, &magZ); |
bmdlh | 0:c8a7071d8692 | 106 | |
bmdlh | 0:c8a7071d8692 | 107 | cout << "MMA8451: " << acc51.getAccX() << "\t" << acc51.getAccY() << "\t" << acc51.getAccZ() << "\n\r" << endl; |
bmdlh | 0:c8a7071d8692 | 108 | cout << "MAG3110: " << magX << "\t" << magY << "\t" << magZ << "\n\r" << endl; |
bmdlh | 0:c8a7071d8692 | 109 | cout << "MAG3110: " << mag.getHeading() << "\n\r" << endl; |
bmdlh | 0:c8a7071d8692 | 110 | wait(holdTime); |
bmdlh | 0:c8a7071d8692 | 111 | |
bmdlh | 0:c8a7071d8692 | 112 | rled = tsi.readPercentage(); |
bmdlh | 0:c8a7071d8692 | 113 | gled = tsi.readPercentage(); |
bmdlh | 0:c8a7071d8692 | 114 | |
bmdlh | 0:c8a7071d8692 | 115 | cout << "Touch: " << tsi.readPercentage() << "\n\r" << endl; |
bmdlh | 0:c8a7071d8692 | 116 | cout << "SW12: " << SW12 << "\n\r" << endl; |
bmdlh | 0:c8a7071d8692 | 117 | cout << "SW32: " << SW32 << "\n\r" << endl; |
bmdlh | 0:c8a7071d8692 | 118 | cout << "Light Sensor: " << lightSensor << "\n\r" << endl; |
bmdlh | 0:c8a7071d8692 | 119 | wait(holdTime); |
bmdlh | 0:c8a7071d8692 | 120 | } |
bmdlh | 0:c8a7071d8692 | 121 | return 0; |
bmdlh | 0:c8a7071d8692 | 122 | } |