Aidan Cleary
/
ESlab3
Lab3 Final copy
main.cpp@5:af5a6c9606e6, 2021-05-03 (annotated)
- Committer:
- hellohello
- Date:
- Mon May 03 17:40:07 2021 +0000
- Revision:
- 5:af5a6c9606e6
- Parent:
- 4:c3bb91b3b992
- Child:
- 6:25a7f695e570
Lab 3 Final Copy
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hellohello | 3:9ea256470c8a | 1 | |
hellohello | 3:9ea256470c8a | 2 | //embedded systems lab question 3 Aidan Cleary T00209564 |
hellohello | 3:9ea256470c8a | 3 | //Lab 3: Digital sensors & equations in C |
hellohello | 3:9ea256470c8a | 4 | |
hellohello | 5:af5a6c9606e6 | 5 | #include "mbed.h"//preprosser command |
hellohello | 5:af5a6c9606e6 | 6 | #include "MMA7660.h"//preprocesser command |
hellohello | 5:af5a6c9606e6 | 7 | |
hellohello | 5:af5a6c9606e6 | 8 | #define PI 3.14159265//PI defined |
Sissors | 0:bd0546063b0a | 9 | |
hellohello | 5:af5a6c9606e6 | 10 | Serial pc(USBTX,USBRX);//serial transmission to pc via USBTX, USBRX |
hellohello | 5:af5a6c9606e6 | 11 | MMA7660 MMA(p28,p27);//IC2 pins associated with accelerometer |
Sissors | 0:bd0546063b0a | 12 | |
hellohello | 5:af5a6c9606e6 | 13 | float calculateAngle(float x,float y,float z) |
hellohello | 5:af5a6c9606e6 | 14 | { |
hellohello | 5:af5a6c9606e6 | 15 | float angle = 0; |
hellohello | 5:af5a6c9606e6 | 16 | // calculate the angle from the formula given in the instructions |
Sissors | 0:bd0546063b0a | 17 | |
hellohello | 5:af5a6c9606e6 | 18 | angle = (y*y) + (z*z);//bottom of the division line |
hellohello | 5:af5a6c9606e6 | 19 | angle = sqrt (angle);//square root |
hellohello | 5:af5a6c9606e6 | 20 | angle = x/angle; |
hellohello | 5:af5a6c9606e6 | 21 | angle = atan (angle);//arctan of angle |
hellohello | 5:af5a6c9606e6 | 22 | angle = angle *180/PI; //radians to degrees! |
hellohello | 5:af5a6c9606e6 | 23 | return angle; |
hellohello | 5:af5a6c9606e6 | 24 | } |
hellohello | 3:9ea256470c8a | 25 | |
hellohello | 5:af5a6c9606e6 | 26 | int main() //main program |
hellohello | 5:af5a6c9606e6 | 27 | { |
hellohello | 5:af5a6c9606e6 | 28 | while (1) { |
hellohello | 5:af5a6c9606e6 | 29 | pc.printf(" X%f Y%f Z%f angle%.2f degrees\r", MMA.x(), |
hellohello | 5:af5a6c9606e6 | 30 | MMA.y(), MMA.z(), calculateAngle(MMA.x(), MMA.y(), MMA.z())); |
hellohello | 5:af5a6c9606e6 | 31 | //i have used \r to repeat the function and keep the tera term tidy |
hellohello | 5:af5a6c9606e6 | 32 | wait(1);//wait 1 second |
Sissors | 0:bd0546063b0a | 33 | } |
hellohello | 5:af5a6c9606e6 | 34 | } |