Asim Taj
/
mainmas
ok
main.cpp@0:adbbe24964e9, 2017-11-27 (annotated)
- Committer:
- Asimtaj
- Date:
- Mon Nov 27 23:08:26 2017 +0000
- Revision:
- 0:adbbe24964e9
ok;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Asimtaj | 0:adbbe24964e9 | 1 | #include "mbed.h" |
Asimtaj | 0:adbbe24964e9 | 2 | |
Asimtaj | 0:adbbe24964e9 | 3 | |
Asimtaj | 0:adbbe24964e9 | 4 | PwmOut PWM0(p21); //PWM out |
Asimtaj | 0:adbbe24964e9 | 5 | AnalogIn ain0(p19); //Analog input A0 |
Asimtaj | 0:adbbe24964e9 | 6 | AnalogIn ain1(p18); //Analog input A0 |
Asimtaj | 0:adbbe24964e9 | 7 | DigitalOut ledasim(LED1); |
Asimtaj | 0:adbbe24964e9 | 8 | DigitalOut ledasim2(LED2); |
Asimtaj | 0:adbbe24964e9 | 9 | Serial pc(USBTX, USBRX); // tx, rx |
Asimtaj | 0:adbbe24964e9 | 10 | Serial uart(p28, p27); |
Asimtaj | 0:adbbe24964e9 | 11 | Timer t; //initialize timer |
Asimtaj | 0:adbbe24964e9 | 12 | |
Asimtaj | 0:adbbe24964e9 | 13 | int main() { |
Asimtaj | 0:adbbe24964e9 | 14 | float test=0; |
Asimtaj | 0:adbbe24964e9 | 15 | float test2 = 0; |
Asimtaj | 0:adbbe24964e9 | 16 | float pretest = 0; |
Asimtaj | 0:adbbe24964e9 | 17 | char str[125]=""; |
Asimtaj | 0:adbbe24964e9 | 18 | float receive = 0; |
Asimtaj | 0:adbbe24964e9 | 19 | int rflag = 0; |
Asimtaj | 0:adbbe24964e9 | 20 | float rpm; //declare rpm variable |
Asimtaj | 0:adbbe24964e9 | 21 | |
Asimtaj | 0:adbbe24964e9 | 22 | PWM0.period(0.0025f); // 0.0025 second period |
Asimtaj | 0:adbbe24964e9 | 23 | PWM0.write(0.8f); // 80% duty cycle, relative to period |
Asimtaj | 0:adbbe24964e9 | 24 | ledasim=!ledasim; |
Asimtaj | 0:adbbe24964e9 | 25 | |
Asimtaj | 0:adbbe24964e9 | 26 | wait(30); |
Asimtaj | 0:adbbe24964e9 | 27 | pretest = (ain0*3.3)/0.178; |
Asimtaj | 0:adbbe24964e9 | 28 | while(1){ |
Asimtaj | 0:adbbe24964e9 | 29 | wait(0.25); |
Asimtaj | 0:adbbe24964e9 | 30 | test=(ain0*3.3)/0.178; |
Asimtaj | 0:adbbe24964e9 | 31 | wait(0.25); |
Asimtaj | 0:adbbe24964e9 | 32 | test2 = (ain0*3.3)/0.178; |
Asimtaj | 0:adbbe24964e9 | 33 | if((test <5.3 || test >4.7) && (test2 <5.3 || test2 > 4.7)){ |
Asimtaj | 0:adbbe24964e9 | 34 | test=test; |
Asimtaj | 0:adbbe24964e9 | 35 | |
Asimtaj | 0:adbbe24964e9 | 36 | } |
Asimtaj | 0:adbbe24964e9 | 37 | if ((test >5.3 || test <4.7) && (test2 <5.3 || test2 >4.7)) { |
Asimtaj | 0:adbbe24964e9 | 38 | test = test2; |
Asimtaj | 0:adbbe24964e9 | 39 | |
Asimtaj | 0:adbbe24964e9 | 40 | } |
Asimtaj | 0:adbbe24964e9 | 41 | if ((test2 > 5.3 || test2 < 4.7) && (test < 5.3 || test > 4.7)){ |
Asimtaj | 0:adbbe24964e9 | 42 | test = test; |
Asimtaj | 0:adbbe24964e9 | 43 | |
Asimtaj | 0:adbbe24964e9 | 44 | } |
Asimtaj | 0:adbbe24964e9 | 45 | if((test >5.3 || test <4.7) && (test2 >5.3 || test2 < 4.7)){ |
Asimtaj | 0:adbbe24964e9 | 46 | test = test; |
Asimtaj | 0:adbbe24964e9 | 47 | } |
Asimtaj | 0:adbbe24964e9 | 48 | |
Asimtaj | 0:adbbe24964e9 | 49 | if((test <5.3 || test >4.7) && (pretest <5.3 || pretest > 4.7)){ |
Asimtaj | 0:adbbe24964e9 | 50 | test=test; |
Asimtaj | 0:adbbe24964e9 | 51 | pretest = test; |
Asimtaj | 0:adbbe24964e9 | 52 | |
Asimtaj | 0:adbbe24964e9 | 53 | } |
Asimtaj | 0:adbbe24964e9 | 54 | if ((test >5.3 || test <4.7) && (pretest <5.3 || pretest >4.7)) { |
Asimtaj | 0:adbbe24964e9 | 55 | test = pretest; |
Asimtaj | 0:adbbe24964e9 | 56 | |
Asimtaj | 0:adbbe24964e9 | 57 | } |
Asimtaj | 0:adbbe24964e9 | 58 | if ((pretest > 5.3 || pretest < 4.7) && (test < 5.3 || test > 4.7)){ |
Asimtaj | 0:adbbe24964e9 | 59 | test = test; |
Asimtaj | 0:adbbe24964e9 | 60 | |
Asimtaj | 0:adbbe24964e9 | 61 | pretest = test; |
Asimtaj | 0:adbbe24964e9 | 62 | } |
Asimtaj | 0:adbbe24964e9 | 63 | if((test >5.3 || test <4.7) && (pretest >5.3 || pretest < 4.7)){ |
Asimtaj | 0:adbbe24964e9 | 64 | test = test; |
Asimtaj | 0:adbbe24964e9 | 65 | pretest = test; |
Asimtaj | 0:adbbe24964e9 | 66 | } |
Asimtaj | 0:adbbe24964e9 | 67 | |
Asimtaj | 0:adbbe24964e9 | 68 | if (ain1 <0.9){ //senses beam crossing |
Asimtaj | 0:adbbe24964e9 | 69 | t.start(); //start timer} |
Asimtaj | 0:adbbe24964e9 | 70 | while(ain1<0.9){} |
Asimtaj | 0:adbbe24964e9 | 71 | while(ain1>0.9){} |
Asimtaj | 0:adbbe24964e9 | 72 | t.stop(); //stop timer |
Asimtaj | 0:adbbe24964e9 | 73 | rpm = (1/t.read())*60; //obtain value of rpm |
Asimtaj | 0:adbbe24964e9 | 74 | |
Asimtaj | 0:adbbe24964e9 | 75 | t = 0; |
Asimtaj | 0:adbbe24964e9 | 76 | } |
Asimtaj | 0:adbbe24964e9 | 77 | |
Asimtaj | 0:adbbe24964e9 | 78 | //pc.printf("%f\n\r",test); |
Asimtaj | 0:adbbe24964e9 | 79 | pc.printf("%f\n\r",rpm); |
Asimtaj | 0:adbbe24964e9 | 80 | if(test > 5.3 || test < 4.7) { |
Asimtaj | 0:adbbe24964e9 | 81 | PWM0.period(0.0f); // 0second period |
Asimtaj | 0:adbbe24964e9 | 82 | PWM0.write(0.0f); |
Asimtaj | 0:adbbe24964e9 | 83 | |
Asimtaj | 0:adbbe24964e9 | 84 | } |
Asimtaj | 0:adbbe24964e9 | 85 | if(uart.readable() && rflag == 0) { |
Asimtaj | 0:adbbe24964e9 | 86 | char message[125]; |
Asimtaj | 0:adbbe24964e9 | 87 | uart.scanf("%s",message); |
Asimtaj | 0:adbbe24964e9 | 88 | receive = atof(message); |
Asimtaj | 0:adbbe24964e9 | 89 | } |
Asimtaj | 0:adbbe24964e9 | 90 | |
Asimtaj | 0:adbbe24964e9 | 91 | if(receive == 1 && rflag == 0){ |
Asimtaj | 0:adbbe24964e9 | 92 | sprintf(str, "%f", test); |
Asimtaj | 0:adbbe24964e9 | 93 | if(uart.writeable()) |
Asimtaj | 0:adbbe24964e9 | 94 | { |
Asimtaj | 0:adbbe24964e9 | 95 | uart.printf("%s\n",str); |
Asimtaj | 0:adbbe24964e9 | 96 | rflag = rflag + 1; |
Asimtaj | 0:adbbe24964e9 | 97 | rflag = 0; |
Asimtaj | 0:adbbe24964e9 | 98 | } |
Asimtaj | 0:adbbe24964e9 | 99 | } |
Asimtaj | 0:adbbe24964e9 | 100 | } |
Asimtaj | 0:adbbe24964e9 | 101 | |
Asimtaj | 0:adbbe24964e9 | 102 | |
Asimtaj | 0:adbbe24964e9 | 103 | |
Asimtaj | 0:adbbe24964e9 | 104 | } |
Asimtaj | 0:adbbe24964e9 | 105 | |
Asimtaj | 0:adbbe24964e9 | 106 | |
Asimtaj | 0:adbbe24964e9 | 107 | |
Asimtaj | 0:adbbe24964e9 | 108 | |
Asimtaj | 0:adbbe24964e9 | 109 | |
Asimtaj | 0:adbbe24964e9 | 110 | |
Asimtaj | 0:adbbe24964e9 | 111 | |
Asimtaj | 0:adbbe24964e9 | 112 | |
Asimtaj | 0:adbbe24964e9 | 113 |