2つのanalogスティックのanalog値をserial通信で送信しLEDをpwm制御するプログラムの送信側。受信側とセットで使えます。
Dependencies: mbed
※解決策思いつかず。放置済み。※
main.cpp@0:9bca2a7d36b5, 2014-12-21 (annotated)
- Committer:
- eil4nyqn
- Date:
- Sun Dec 21 06:34:09 2014 +0000
- Revision:
- 0:9bca2a7d36b5
www;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eil4nyqn | 0:9bca2a7d36b5 | 1 | #include "mbed.h" |
eil4nyqn | 0:9bca2a7d36b5 | 2 | |
eil4nyqn | 0:9bca2a7d36b5 | 3 | AnalogIn stick[] = {p20,p19}; |
eil4nyqn | 0:9bca2a7d36b5 | 4 | Serial pc(USBTX,USBRX); |
eil4nyqn | 0:9bca2a7d36b5 | 5 | Serial mc(p13,p14); |
eil4nyqn | 0:9bca2a7d36b5 | 6 | PwmOut testled[] = {LED1,LED2,LED3,LED4}; |
eil4nyqn | 0:9bca2a7d36b5 | 7 | |
eil4nyqn | 0:9bca2a7d36b5 | 8 | int main() { |
eil4nyqn | 0:9bca2a7d36b5 | 9 | double signal,signal2,signal3,signal4,analog3,analog4; |
eil4nyqn | 0:9bca2a7d36b5 | 10 | uint8_t data = 0,data2 = 0,i; |
eil4nyqn | 0:9bca2a7d36b5 | 11 | double analog[10],analog2[10],ave,ave2,sum,sum2; |
eil4nyqn | 0:9bca2a7d36b5 | 12 | wait(0.25); |
eil4nyqn | 0:9bca2a7d36b5 | 13 | for(i=0;i < 10;i++){ |
eil4nyqn | 0:9bca2a7d36b5 | 14 | analog[i] = stick[0]; |
eil4nyqn | 0:9bca2a7d36b5 | 15 | analog2[i] = stick[1]; |
eil4nyqn | 0:9bca2a7d36b5 | 16 | sum += analog[i]; |
eil4nyqn | 0:9bca2a7d36b5 | 17 | sum2 += analog2[i]; |
eil4nyqn | 0:9bca2a7d36b5 | 18 | wait(0.10); |
eil4nyqn | 0:9bca2a7d36b5 | 19 | } |
eil4nyqn | 0:9bca2a7d36b5 | 20 | ave = sum/10; |
eil4nyqn | 0:9bca2a7d36b5 | 21 | ave2 = sum2/10; |
eil4nyqn | 0:9bca2a7d36b5 | 22 | testled[0] = 1; |
eil4nyqn | 0:9bca2a7d36b5 | 23 | wait(0.2); |
eil4nyqn | 0:9bca2a7d36b5 | 24 | |
eil4nyqn | 0:9bca2a7d36b5 | 25 | while(1) { |
eil4nyqn | 0:9bca2a7d36b5 | 26 | analog3 = stick[0]; |
eil4nyqn | 0:9bca2a7d36b5 | 27 | analog4 = stick[1]; |
eil4nyqn | 0:9bca2a7d36b5 | 28 | if(analog3 > ave-0.050 && analog3 < ave+0.050) { |
eil4nyqn | 0:9bca2a7d36b5 | 29 | signal = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 30 | signal3 = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 31 | data = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 32 | mc.putc(data); |
eil4nyqn | 0:9bca2a7d36b5 | 33 | pc.printf("%f\n",data); |
eil4nyqn | 0:9bca2a7d36b5 | 34 | testled[0] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 35 | testled[1] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 36 | }else if(analog3 > ave-0.050){ |
eil4nyqn | 0:9bca2a7d36b5 | 37 | signal = (analog3-ave)/ave; |
eil4nyqn | 0:9bca2a7d36b5 | 38 | data = (signal+0.005)*50; |
eil4nyqn | 0:9bca2a7d36b5 | 39 | mc.putc(data); |
eil4nyqn | 0:9bca2a7d36b5 | 40 | pc.printf("%f\n",data); |
eil4nyqn | 0:9bca2a7d36b5 | 41 | testled[0] = signal; |
eil4nyqn | 0:9bca2a7d36b5 | 42 | testled[1] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 43 | }else if(analog3 < ave+0.050){ |
eil4nyqn | 0:9bca2a7d36b5 | 44 | signal3 = (analog3-ave)/ave*(-1); |
eil4nyqn | 0:9bca2a7d36b5 | 45 | data = (signal3+0.005)*50+64; |
eil4nyqn | 0:9bca2a7d36b5 | 46 | mc.putc(data); |
eil4nyqn | 0:9bca2a7d36b5 | 47 | pc.printf("%f\n",data); |
eil4nyqn | 0:9bca2a7d36b5 | 48 | testled[1] = signal3; |
eil4nyqn | 0:9bca2a7d36b5 | 49 | testled[0] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 50 | } |
eil4nyqn | 0:9bca2a7d36b5 | 51 | if(analog4 > ave2-0.050 && analog4 < ave2+0.050) { |
eil4nyqn | 0:9bca2a7d36b5 | 52 | signal2 = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 53 | signal4 = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 54 | data = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 55 | mc.putc(data); |
eil4nyqn | 0:9bca2a7d36b5 | 56 | pc.printf("%f\n",data); |
eil4nyqn | 0:9bca2a7d36b5 | 57 | testled[2] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 58 | testled[3] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 59 | }else if(analog4 > ave2-0.050){ |
eil4nyqn | 0:9bca2a7d36b5 | 60 | signal2 = (analog4-ave2)/ave2; |
eil4nyqn | 0:9bca2a7d36b5 | 61 | data = (signal2+0.005)*50+128; |
eil4nyqn | 0:9bca2a7d36b5 | 62 | mc.putc(data); |
eil4nyqn | 0:9bca2a7d36b5 | 63 | pc.printf("%f\n",data); |
eil4nyqn | 0:9bca2a7d36b5 | 64 | testled[2] = signal2; |
eil4nyqn | 0:9bca2a7d36b5 | 65 | testled[3] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 66 | }else if(analog4 < ave2+0.050){ |
eil4nyqn | 0:9bca2a7d36b5 | 67 | signal4 = (analog4-ave2)/ave2*(-1); |
eil4nyqn | 0:9bca2a7d36b5 | 68 | data = (signal4+0.005)*50+192; |
eil4nyqn | 0:9bca2a7d36b5 | 69 | mc.putc(data); |
eil4nyqn | 0:9bca2a7d36b5 | 70 | pc.printf("%f\n",data); |
eil4nyqn | 0:9bca2a7d36b5 | 71 | testled[3] = signal4; |
eil4nyqn | 0:9bca2a7d36b5 | 72 | testled[2] = 0; |
eil4nyqn | 0:9bca2a7d36b5 | 73 | } |
eil4nyqn | 0:9bca2a7d36b5 | 74 | pc.printf("%u_%u_%f_%f_%f_%f",data,data2,signal,signal2,signal3,signal4); |
eil4nyqn | 0:9bca2a7d36b5 | 75 | } |
eil4nyqn | 0:9bca2a7d36b5 | 76 | } |