目的
adcのテスト用
どうしてこうなったかは、6-10,10-20,20-80を参考
void setup() {
Serial.begin(9600);
}
int sensorValue; //センサーの読み取り値
float Voltage; //電圧
float ir_length; //長さ
void loop() {
sensorValue = analogRead(A0);
Voltage = (float)sensorValue * ( 5.0 / 1024.0 );
if (Voltage >= 0 && Voltage <= 0.4) {Voltage = 0.4;}
if (Voltage >= 3.12 && Voltage <= 5.0) {Voltage = 3.12;}
if (Voltage >= 0.4 && Voltage <= 1.3 ) { //80-20
ir_length = 1.0/ ( 0.0125 + ( ( Voltage - 0.4) * (0.0375/0.9) ) );
} else if (Voltage >= 1.3 && Voltage <= 2.3 ) { //20-10
ir_length = 1/ ( 0.05 + ( ( Voltage - 1.3) * 0.05 ) );
} else if (Voltage >= 2.3 && Voltage <= 2.75) { //10-8
ir_length = 1.0/ ( 0.1 + ( ( Voltage - 2.3) * (0.025/0.45) ) );
} else if (Voltage >= 2.75 && Voltage <= 2.98) { //8-7
ir_length = 1.0/ ( 0.125 + ( ( Voltage - 2.75) * (0.0179/0.23) ) );
} else if (Voltage >= 2.98 && Voltage <= 3.12) { //7-6
ir_length = 1.0/ ( 0.1429 + ( ( Voltage - 2.98) * (0.0238/0.14) ) );
}
//Serial.println(sensorValue);
Serial.print("800");
Serial.print(",");
Serial.print( ir_length*10 );
Serial.print(",");
Serial.println("0");
delay(10);
}
電圧の計算 +-1mVの誤差
#include <stdio.h>
int main(void){
int ii;
int s;
int u;
for(ii = 0;ii <= 1023;ii++) {
s=(ii*5000)/1024;
u=((ii*8)+( (ii*8)/10 )+( (ii*2)/100 ))/10 + (ii*4);
printf("%d \t %d \t %d \n",ii,s,u);
}//ii
}//main
配列化 参考
#include <stdio.h>
//80-20
unsigned char qq[]={
80,78,77,76,75,75,74,73,72,71,
70,69,68,68,67,66,65,65,64,63,
63,62,61,61,60,60,59,58,58,57,
57,56,56,55,55,54,54,53,53,52,
52,51,51,50,50,50,49,49,48,48,
47,47,47,46,46,46,45,45,45,44,
44,44,43,43,43,42,42,42,41,41,
41,41,40,40,40,40,39,39,39,38,
38,38,38,37,37,37,37,37,36,36,
36,36,35,35,35,35,35,34,34,34,
34,34,33,33,33,33,33,32,32,32,
32,32,32,31,31,31,31,31,31,30,
30,30,30,30,30,30,29,29,29,29,
29,29,28,28,28,28,28,28,28,28,
27,27,27,27,27,27,27,27,26,26,
26,26,26,26,26,26,25,25,25,25,
25,25,25,25,25,25,24,24,24,24,
24,24,24,24,24,24,23,23,23,23,
23,23,23,23,23,23,22,22,22,22,
22,22,22,22,22,22,22,22,21,21,
21,21,21,21,21,21,21,21,21,21,
21,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20
};
//20-10
unsigned char pp[]={
200,199,198,197,196,196,195,194,193,193,
192,191,190,190,189,188,187,187,186,185,
185,184,183,183,182,181,181,180,179,179,
178,177,177,176,176,175,174,174,173,173,
172,171,171,170,170,169,168,168,167,167,
166,166,165,165,164,163,163,162,162,161,
161,160,160,159,159,158,158,157,157,156,
156,155,155,154,154,153,153,152,152,151,
151,151,150,150,149,149,148,148,147,147,
147,146,146,145,145,144,144,144,143,143,
142,142,142,141,141,140,140,140,139,139,
138,138,138,137,137,136,136,136,135,135,
135,134,134,134,133,133,132,132,132,131,
131,131,130,130,130,129,129,129,128,128,
128,127,127,127,126,126,126,125,125,125,
125,124,124,124,123,123,123,122,122,122,
121,121,121,121,120,120,120,119,119,119,
119,118,118,118,117,117,117,117,116,116,
116,116,115,115,115,114,114,114,114,113,
113,113,113,112,112,112,112,111,111,111,
111,110,110,110,110,109,109,109,109,108,
108,108,108,107,107,107,107,107,106,106,
106,106,105,105,105,105,105,104,104,104,
104,103,103,103,103,103,102,102,102,102,
102,101,101,101,101,101,100,100,100,100,
100
};
//10-6
unsigned char oo[]={
100,99,98,98,97,97,96,96,95,95,
94,94,93,93,92,92,91,91,90,90,
90,89,89,88,88,87,87,86,86,86,
85,85,84,84,84,83,83,82,82,82,
81,81,81,80,80,80,79,79,78,78,
77,77,76,76,75,75,74,74,74,73,
73,72,72,71,71,71,70,70,69,69,
68,67,66,66,65,64,63,63,62,61,
61,60,60
};
//赤外線距離センサー(GP2Y0A21YK)の電圧から距離を求める。
//Voltage //電圧
int ir_len_i(int Voltage)
{
int ir_length; //長さ
if (Voltage >= 0 && Voltage <= 400 ) {return(800);}
if (Voltage >= 3120 && Voltage <= 5000) {return(60); }
if (Voltage >= 400 && Voltage <= 1300 ) { //80-20
ir_length = (int)qq[(Voltage-400)>>2];
ir_length = ir_length * 10;
} else if (Voltage >= 1300 && Voltage <= 2300 ) { //20-10
ir_length = (int)pp[(Voltage-1300)>>2];
} else if (Voltage >= 2300 && Voltage <= 3120 ) { //10-6
ir_length = oo[(Voltage-2300)/10];
}//end if
//戻り値
return(ir_length);
} //ir_le_i
int main(void){
int ii;
int s;
for(ii = 0;ii <= 1023;ii++) {
s=(ii*5000)/1024;
printf("%d \t %d \n",s,ir_len_i(s));
}//ii
}//main
0 800
4 800
9 800
14 800
19 800
24 800
29 800
34 800
39 800
43 800
48 800
53 800
58 800
63 800
68 800
73 800
78 800
83 800
87 800
92 800
97 800
102 800
107 800
112 800
117 800
122 800
126 800
131 800
136 800
141 800
146 800
151 800
156 800
161 800
166 800
170 800
175 800
180 800
185 800
190 800
195 800
200 800
205 800
209 800
214 800
219 800
224 800
229 800
234 800
239 800
244 800
249 800
253 800
258 800
263 800
268 800
273 800
278 800
283 800
288 800
292 800
297 800
302 800
307 800
312 800
317 800
322 800
327 800
332 800
336 800
341 800
346 800
351 800
356 800
361 800
366 800
371 800
375 800
380 800
385 800
390 800
395 800
400 800
405 780
410 770
415 760
419 750
424 740
429 730
434 720
439 710
444 690
449 680
454 680
458 670
463 660
468 650
473 640
478 630
483 630
488 610
493 610
498 600
502 600
507 590
512 580
517 570
522 570
527 560
532 550
537 550
541 540
546 540
551 530
556 520
561 520
566 510
571 510
576 500
581 500
585 490
590 490
595 480
600 470
605 470
610 470
615 460
620 460
625 450
629 450
634 450
639 440
644 440
649 430
654 430
659 430
664 420
668 420
673 410
678 410
683 410
688 400
693 400
698 400
703 400
708 390
712 390
717 380
722 380
727 380
732 370
737 370
742 370
747 370
751 370
756 360
761 360
766 360
771 350
776 350
781 350
786 350
791 340
795 340
800 340
805 340
810 330
815 330
820 330
825 330
830 320
834 320
839 320
844 320
849 320
854 310
859 310
864 310
869 310
874 310
878 300
883 300
888 300
893 300
898 300
903 300
908 290
913 290
917 290
922 290
927 290
932 280
937 280
942 280
947 280
952 280
957 280
961 270
966 270
971 270
976 270
981 270
986 270
991 270
996 260
1000 260
1005 260
1010 260
1015 260
1020 260
1025 250
1030 250
1035 250
1040 250
1044 250
1049 250
1054 250
1059 250
1064 240
1069 240
1074 240
1079 240
1083 240
1088 240
1093 240
1098 240
1103 240
1108 230
1113 230
1118 230
1123 230
1127 230
1132 230
1137 230
1142 230
1147 220
1152 220
1157 220
1162 220
1166 220
1171 220
1176 220
1181 220
1186 220
1191 220
1196 210
1201 210
1206 210
1210 210
1215 210
1220 210
1225 210
1230 210
1235 210
1240 210
1245 200
1250 200
1254 200
1259 200
1264 200
1269 200
1274 200
1279 200
1284 200
1289 200
1293 200
1298 200
1303 200
1308 198
1313 197
1318 196
1323 196
1328 194
1333 193
1337 193
1342 192
1347 191
1352 190
1357 189
1362 188
1367 187
1372 186
1376 185
1381 185
1386 184
1391 183
1396 182
1401 181
1406 181
1411 180
1416 179
1420 178
1425 177
1430 177
1435 176
1440 175
1445 174
1450 174
1455 173
1459 173
1464 171
1469 171
1474 170
1479 170
1484 168
1489 168
1494 167
1499 167
1503 166
1508 165
1513 165
1518 164
1523 163
1528 162
1533 162
1538 161
1542 161
1547 160
1552 159
1557 159
1562 158
1567 158
1572 157
1577 156
1582 156
1586 155
1591 155
1596 154
1601 153
1606 153
1611 152
1616 151
1621 151
1625 151
1630 150
1635 150
1640 149
1645 148
1650 148
1655 147
1660 147
1665 146
1669 146
1674 145
1679 145
1684 144
1689 144
1694 143
1699 143
1704 142
1708 142
1713 141
1718 141
1723 140
1728 140
1733 139
1738 139
1743 138
1748 138
1752 137
1757 137
1762 136
1767 136
1772 135
1777 135
1782 135
1787 134
1791 134
1796 133
1801 133
1806 132
1811 132
1816 131
1821 131
1826 131
1831 130
1835 130
1840 129
1845 129
1850 129
1855 128
1860 128
1865 127
1870 127
1875 127
1879 126
1884 126
1889 125
1894 125
1899 125
1904 124
1909 124
1914 124
1918 123
1923 123
1928 122
1933 122
1938 122
1943 121
1948 121
1953 121
1958 120
1962 120
1967 120
1972 119
1977 119
1982 119
1987 118
1992 118
1997 117
2001 117
2006 117
2011 117
2016 116
2021 116
2026 116
2031 115
2036 115
2041 114
2045 114
2050 114
2055 114
2060 113
2065 113
2070 113
2075 112
2080 112
2084 112
2089 111
2094 111
2099 111
2104 110
2109 110
2114 110
2119 110
2124 109
2128 109
2133 109
2138 108
2143 108
2148 108
2153 107
2158 107
2163 107
2167 107
2172 106
2177 106
2182 106
2187 106
2192 105
2197 105
2202 105
2207 105
2211 104
2216 104
2221 104
2226 103
2231 103
2236 103
2241 103
2246 102
2250 102
2255 102
2260 102
2265 101
2270 101
2275 101
2280 101
2285 100
2290 100
2294 100
2299 100
2304 100
2309 100
2314 99
2319 99
2324 98
2329 98
2333 98
2338 98
2343 97
2348 97
2353 97
2358 97
2363 96
2368 96
2373 96
2377 96
2382 95
2387 95
2392 95
2397 95
2402 94
2407 94
2412 94
2416 94
2421 93
2426 93
2431 93
2436 93
2441 92
2446 92
2451 92
2456 92
2460 91
2465 91
2470 91
2475 91
2480 90
2485 90
2490 90
2495 90
2500 90
2504 90
2509 90
2514 89
2519 89
2524 89
2529 89
2534 88
2539 88
2543 88
2548 88
2553 87
2558 87
2563 87
2568 87
2573 86
2578 86
2583 86
2587 86
2592 86
2597 86
2602 85
2607 85
2612 85
2617 85
2622 84
2626 84
2631 84
2636 84
2641 84
2646 84
2651 83
2656 83
2661 83
2666 83
2670 82
2675 82
2680 82
2685 82
2690 82
2695 82
2700 81
2705 81
2709 81
2714 81
2719 81
2724 81
2729 81
2734 80
2739 80
2744 80
2749 80
2753 80
2758 80
2763 79
2768 79
2773 79
2778 79
2783 78
2788 78
2792 78
2797 78
2802 77
2807 77
2812 77
2817 77
2822 76
2827 76
2832 76
2836 76
2841 75
2846 75
2851 75
2856 75
2861 74
2866 74
2871 74
2875 74
2880 74
2885 74
2890 73
2895 73
2900 73
2905 73
2910 72
2915 72
2919 72
2924 72
2929 72
2934 71
2939 71
2944 71
2949 71
2954 71
2958 71
2963 70
2968 70
2973 70
2978 70
2983 69
2988 69
2993 69
2998 69
3002 68
3007 68
3012 67
3017 67
3022 66
3027 66
3032 66
3037 66
3041 65
3046 65
3051 64
3056 64
3061 63
3066 63
3071 63
3076 63
3081 62
3085 62
3090 61
3095 61
3100 61
3105 61
3110 60
3115 60
3120 60
3125 60
3129 60
3134 60
3139 60
3144 60
3149 60
3154 60
3159 60
3164 60
3168 60
3173 60
3178 60
3183 60
3188 60
3193 60
3198 60
3203 60
3208 60
3212 60
3217 60
3222 60
3227 60
3232 60
3237 60
3242 60
3247 60
3251 60
3256 60
3261 60
3266 60
3271 60
3276 60
3281 60
3286 60
3291 60
3295 60
3300 60
3305 60
3310 60
3315 60
3320 60
3325 60
3330 60
3334 60
3339 60
3344 60
3349 60
3354 60
3359 60
3364 60
3369 60
3374 60
3378 60
3383 60
3388 60
3393 60
3398 60
3403 60
3408 60
3413 60
3417 60
3422 60
3427 60
3432 60
3437 60
3442 60
3447 60
3452 60
3457 60
3461 60
3466 60
3471 60
3476 60
3481 60
3486 60
3491 60
3496 60
3500 60
3505 60
3510 60
3515 60
3520 60
3525 60
3530 60
3535 60
3540 60
3544 60
3549 60
3554 60
3559 60
3564 60
3569 60
3574 60
3579 60
3583 60
3588 60
3593 60
3598 60
3603 60
3608 60
3613 60
3618 60
3623 60
3627 60
3632 60
3637 60
3642 60
3647 60
3652 60
3657 60
3662 60
3666 60
3671 60
3676 60
3681 60
3686 60
3691 60
3696 60
3701 60
3706 60
3710 60
3715 60
3720 60
3725 60
3730 60
3735 60
3740 60
3745 60
3750 60
3754 60
3759 60
3764 60
3769 60
3774 60
3779 60
3784 60
3789 60
3793 60
3798 60
3803 60
3808 60
3813 60
3818 60
3823 60
3828 60
3833 60
3837 60
3842 60
3847 60
3852 60
3857 60
3862 60
3867 60
3872 60
3876 60
3881 60
3886 60
3891 60
3896 60
3901 60
3906 60
3911 60
3916 60
3920 60
3925 60
3930 60
3935 60
3940 60
3945 60
3950 60
3955 60
3959 60
3964 60
3969 60
3974 60
3979 60
3984 60
3989 60
3994 60
3999 60
4003 60
4008 60
4013 60
4018 60
4023 60
4028 60
4033 60
4038 60
4042 60
4047 60
4052 60
4057 60
4062 60
4067 60
4072 60
4077 60
4082 60
4086 60
4091 60
4096 60
4101 60
4106 60
4111 60
4116 60
4121 60
4125 60
4130 60
4135 60
4140 60
4145 60
4150 60
4155 60
4160 60
4165 60
4169 60
4174 60
4179 60
4184 60
4189 60
4194 60
4199 60
4204 60
4208 60
4213 60
4218 60
4223 60
4228 60
4233 60
4238 60
4243 60
4248 60
4252 60
4257 60
4262 60
4267 60
4272 60
4277 60
4282 60
4287 60
4291 60
4296 60
4301 60
4306 60
4311 60
4316 60
4321 60
4326 60
4331 60
4335 60
4340 60
4345 60
4350 60
4355 60
4360 60
4365 60
4370 60
4375 60
4379 60
4384 60
4389 60
4394 60
4399 60
4404 60
4409 60
4414 60
4418 60
4423 60
4428 60
4433 60
4438 60
4443 60
4448 60
4453 60
4458 60
4462 60
4467 60
4472 60
4477 60
4482 60
4487 60
4492 60
4497 60
4501 60
4506 60
4511 60
4516 60
4521 60
4526 60
4531 60
4536 60
4541 60
4545 60
4550 60
4555 60
4560 60
4565 60
4570 60
4575 60
4580 60
4584 60
4589 60
4594 60
4599 60
4604 60
4609 60
4614 60
4619 60
4624 60
4628 60
4633 60
4638 60
4643 60
4648 60
4653 60
4658 60
4663 60
4667 60
4672 60
4677 60
4682 60
4687 60
4692 60
4697 60
4702 60
4707 60
4711 60
4716 60
4721 60
4726 60
4731 60
4736 60
4741 60
4746 60
4750 60
4755 60
4760 60
4765 60
4770 60
4775 60
4780 60
4785 60
4790 60
4794 60
4799 60
4804 60
4809 60
4814 60
4819 60
4824 60
4829 60
4833 60
4838 60
4843 60
4848 60
4853 60
4858 60
4863 60
4868 60
4873 60
4877 60
4882 60
4887 60
4892 60
4897 60
4902 60
4907 60
4912 60
4916 60
4921 60
4926 60
4931 60
4936 60
4941 60
4946 60
4951 60
4956 60
4960 60
4965 60
4970 60
4975 60
4980 60
4985 60
4990 60
4995 60