mbed/ARM 活用事例 第5章 赤外線距離センサを使う

Dependencies:   TextLCD mbed

Committer:
sunifu
Date:
Tue Oct 04 13:23:35 2011 +0000
Revision:
0:b5ce43c9e6b0

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sunifu 0:b5ce43c9e6b0 1 #include "mbed.h"
sunifu 0:b5ce43c9e6b0 2 #include "TextLCD.h"
sunifu 0:b5ce43c9e6b0 3
sunifu 0:b5ce43c9e6b0 4
sunifu 0:b5ce43c9e6b0 5 TextLCD lcd(p24, p26, p27, p28, p29, p30);
sunifu 0:b5ce43c9e6b0 6 /*
sunifu 0:b5ce43c9e6b0 7 AnalogIn in(p20);
sunifu 0:b5ce43c9e6b0 8 PwmOut out(p21);
sunifu 0:b5ce43c9e6b0 9 DigitalOut myled(LED1);
sunifu 0:b5ce43c9e6b0 10
sunifu 0:b5ce43c9e6b0 11 Ticker input;
sunifu 0:b5ce43c9e6b0 12 Ticker output;
sunifu 0:b5ce43c9e6b0 13 double flash;
sunifu 0:b5ce43c9e6b0 14 float data,now,old=0.0;
sunifu 0:b5ce43c9e6b0 15
sunifu 0:b5ce43c9e6b0 16 void run(){
sunifu 0:b5ce43c9e6b0 17 old = now;
sunifu 0:b5ce43c9e6b0 18 now = in;
sunifu 0:b5ce43c9e6b0 19 data = ( now + old ) / 2.0;
sunifu 0:b5ce43c9e6b0 20 }
sunifu 0:b5ce43c9e6b0 21
sunifu 0:b5ce43c9e6b0 22 void update(){
sunifu 0:b5ce43c9e6b0 23 float range;
sunifu 0:b5ce43c9e6b0 24 lcd.locate(0,0);
sunifu 0:b5ce43c9e6b0 25 if ( data < 0.208 ){ // x <= 40cm
sunifu 0:b5ce43c9e6b0 26 flash = 0.0 ;
sunifu 0:b5ce43c9e6b0 27 lcd.printf(" Safty ");
sunifu 0:b5ce43c9e6b0 28 }else if ( data < 0.272){ // 30cm <= x < 40cm
sunifu 0:b5ce43c9e6b0 29 flash = 0.8 ;
sunifu 0:b5ce43c9e6b0 30 lcd.printf("Caution! ");
sunifu 0:b5ce43c9e6b0 31 }else if ( data < 0.389){ // 20cm <= x 30cm
sunifu 0:b5ce43c9e6b0 32 flash = 0.4 ;
sunifu 0:b5ce43c9e6b0 33 lcd.printf("Caution! ");
sunifu 0:b5ce43c9e6b0 34 }else if ( data < 0.724){ // 10cm <= x < 20cm
sunifu 0:b5ce43c9e6b0 35 flash = 0.1 ;
sunifu 0:b5ce43c9e6b0 36 lcd.printf(" Danger! ");
sunifu 0:b5ce43c9e6b0 37 }else{ // x < 10cm
sunifu 0:b5ce43c9e6b0 38 flash = 0.05 ;
sunifu 0:b5ce43c9e6b0 39 lcd.printf(" Danger! ");
sunifu 0:b5ce43c9e6b0 40 }
sunifu 0:b5ce43c9e6b0 41
sunifu 0:b5ce43c9e6b0 42
sunifu 0:b5ce43c9e6b0 43 range = 25.33 * pow((data*3.3),-1.21);
sunifu 0:b5ce43c9e6b0 44
sunifu 0:b5ce43c9e6b0 45 lcd.locate(0,1);
sunifu 0:b5ce43c9e6b0 46 if ( data > 0.12)
sunifu 0:b5ce43c9e6b0 47 lcd.printf("RANGE %5.2f[cm]",range);
sunifu 0:b5ce43c9e6b0 48 else
sunifu 0:b5ce43c9e6b0 49 lcd.printf("RANGE OVER ");
sunifu 0:b5ce43c9e6b0 50 }
sunifu 0:b5ce43c9e6b0 51
sunifu 0:b5ce43c9e6b0 52 int main() {
sunifu 0:b5ce43c9e6b0 53
sunifu 0:b5ce43c9e6b0 54 input.attach(&run,0.2);
sunifu 0:b5ce43c9e6b0 55 output.attach(&update,0.5);
sunifu 0:b5ce43c9e6b0 56 out.period(0.001);
sunifu 0:b5ce43c9e6b0 57 while(1){
sunifu 0:b5ce43c9e6b0 58 out.write(0.0);
sunifu 0:b5ce43c9e6b0 59 if ( flash != 0.0 ){
sunifu 0:b5ce43c9e6b0 60 myled = !myled ;
sunifu 0:b5ce43c9e6b0 61 wait(flash) ;
sunifu 0:b5ce43c9e6b0 62 out.write(0.5);
sunifu 0:b5ce43c9e6b0 63 wait(flash);
sunifu 0:b5ce43c9e6b0 64 }else{
sunifu 0:b5ce43c9e6b0 65 myled = 0;
sunifu 0:b5ce43c9e6b0 66 out.write(0.0);
sunifu 0:b5ce43c9e6b0 67 }
sunifu 0:b5ce43c9e6b0 68 }
sunifu 0:b5ce43c9e6b0 69 }
sunifu 0:b5ce43c9e6b0 70
sunifu 0:b5ce43c9e6b0 71
sunifu 0:b5ce43c9e6b0 72 */
sunifu 0:b5ce43c9e6b0 73
sunifu 0:b5ce43c9e6b0 74
sunifu 0:b5ce43c9e6b0 75
sunifu 0:b5ce43c9e6b0 76
sunifu 0:b5ce43c9e6b0 77 AnalogOut out(p18);
sunifu 0:b5ce43c9e6b0 78 AnalogIn in(p20);
sunifu 0:b5ce43c9e6b0 79 Ticker input;
sunifu 0:b5ce43c9e6b0 80 Ticker output;
sunifu 0:b5ce43c9e6b0 81 int freq;
sunifu 0:b5ce43c9e6b0 82 double t = 0.0 ;
sunifu 0:b5ce43c9e6b0 83 float d11 = 0.0, d12 =0.0 ;
sunifu 0:b5ce43c9e6b0 84 float data;
sunifu 0:b5ce43c9e6b0 85
sunifu 0:b5ce43c9e6b0 86 void run(){
sunifu 0:b5ce43c9e6b0 87 data = in;
sunifu 0:b5ce43c9e6b0 88 }
sunifu 0:b5ce43c9e6b0 89
sunifu 0:b5ce43c9e6b0 90 void wave(){
sunifu 0:b5ce43c9e6b0 91 if ( data < 0.1212){
sunifu 0:b5ce43c9e6b0 92 freq = 0;
sunifu 0:b5ce43c9e6b0 93 }else if ( data < 0.181){
sunifu 0:b5ce43c9e6b0 94 freq = 2093;
sunifu 0:b5ce43c9e6b0 95 }else if ( data < 0.242){
sunifu 0:b5ce43c9e6b0 96 freq = 1976;
sunifu 0:b5ce43c9e6b0 97 }else if ( data < 0.333){
sunifu 0:b5ce43c9e6b0 98 freq = 1760;
sunifu 0:b5ce43c9e6b0 99 }else if ( data < 0.394){
sunifu 0:b5ce43c9e6b0 100 freq = 1568;
sunifu 0:b5ce43c9e6b0 101 }else if ( data < 0.485){
sunifu 0:b5ce43c9e6b0 102 freq = 1397;
sunifu 0:b5ce43c9e6b0 103 }else if ( data < 0.697 ){
sunifu 0:b5ce43c9e6b0 104 freq = 1319;
sunifu 0:b5ce43c9e6b0 105 }else if ( data < 0.85){
sunifu 0:b5ce43c9e6b0 106 freq = 1175;
sunifu 0:b5ce43c9e6b0 107 }else{
sunifu 0:b5ce43c9e6b0 108 freq = 1046;
sunifu 0:b5ce43c9e6b0 109 }
sunifu 0:b5ce43c9e6b0 110 float a;
sunifu 0:b5ce43c9e6b0 111
sunifu 0:b5ce43c9e6b0 112 if ( freq != 0 ){
sunifu 0:b5ce43c9e6b0 113 a =0.5*sin(2.0 * 3.1415 * freq * t )+0.5;
sunifu 0:b5ce43c9e6b0 114 out = a;
sunifu 0:b5ce43c9e6b0 115 }
sunifu 0:b5ce43c9e6b0 116 if ( t > 1.0 ) t = 0.0 ;
sunifu 0:b5ce43c9e6b0 117 t=t+0.00005;
sunifu 0:b5ce43c9e6b0 118 }
sunifu 0:b5ce43c9e6b0 119
sunifu 0:b5ce43c9e6b0 120 int main() {
sunifu 0:b5ce43c9e6b0 121
sunifu 0:b5ce43c9e6b0 122 input.attach(&run,0.5);
sunifu 0:b5ce43c9e6b0 123 output.attach_us(&wave,50);
sunifu 0:b5ce43c9e6b0 124
sunifu 0:b5ce43c9e6b0 125 while(1){
sunifu 0:b5ce43c9e6b0 126
sunifu 0:b5ce43c9e6b0 127 lcd.locate(0,0);
sunifu 0:b5ce43c9e6b0 128 lcd.printf("%5.3f %4d",data,freq);
sunifu 0:b5ce43c9e6b0 129 if ( 0.12 <= data){
sunifu 0:b5ce43c9e6b0 130 float range = 25.33 * pow((data*3.3),-1.21);
sunifu 0:b5ce43c9e6b0 131 lcd.locate(0,1);
sunifu 0:b5ce43c9e6b0 132 lcd.printf("RANGE %5.2f[cm]",range);
sunifu 0:b5ce43c9e6b0 133 }else{
sunifu 0:b5ce43c9e6b0 134 lcd.locate(0,1);
sunifu 0:b5ce43c9e6b0 135 lcd.printf("RANGE -----[cm]");
sunifu 0:b5ce43c9e6b0 136 }
sunifu 0:b5ce43c9e6b0 137 lcd.locate(12,0);
sunifu 0:b5ce43c9e6b0 138 lcd.putc('[');
sunifu 0:b5ce43c9e6b0 139 if ( freq == 0){
sunifu 0:b5ce43c9e6b0 140 lcd.putc('-');
sunifu 0:b5ce43c9e6b0 141 lcd.putc('-');
sunifu 0:b5ce43c9e6b0 142 }else if ( freq == 2093){
sunifu 0:b5ce43c9e6b0 143 lcd.putc(0xC4);
sunifu 0:b5ce43c9e6b0 144 lcd.putc(0xDE);
sunifu 0:b5ce43c9e6b0 145 }else if (freq == 1976){
sunifu 0:b5ce43c9e6b0 146 lcd.putc(0xBC);
sunifu 0:b5ce43c9e6b0 147 lcd.putc(0xFE);
sunifu 0:b5ce43c9e6b0 148 }else if ( freq == 1760){
sunifu 0:b5ce43c9e6b0 149 lcd.putc(0xD7);
sunifu 0:b5ce43c9e6b0 150 lcd.putc(0xFE);
sunifu 0:b5ce43c9e6b0 151 }else if ( freq == 1568){
sunifu 0:b5ce43c9e6b0 152 lcd.putc(0xBF);
sunifu 0:b5ce43c9e6b0 153 lcd.putc(0xFE);
sunifu 0:b5ce43c9e6b0 154 }else if ( freq == 1397){
sunifu 0:b5ce43c9e6b0 155 lcd.putc(0xCC);
sunifu 0:b5ce43c9e6b0 156 lcd.putc(0xA7);
sunifu 0:b5ce43c9e6b0 157 }else if ( freq == 1319 ){
sunifu 0:b5ce43c9e6b0 158 lcd.putc(0xD0);
sunifu 0:b5ce43c9e6b0 159 lcd.putc(0xFE);
sunifu 0:b5ce43c9e6b0 160 }else if ( freq == 1175){
sunifu 0:b5ce43c9e6b0 161 lcd.putc(0xDA);
sunifu 0:b5ce43c9e6b0 162 lcd.putc(0xFE);
sunifu 0:b5ce43c9e6b0 163 }else{
sunifu 0:b5ce43c9e6b0 164 lcd.putc(0xC4);
sunifu 0:b5ce43c9e6b0 165 lcd.putc(0xDE);
sunifu 0:b5ce43c9e6b0 166 }
sunifu 0:b5ce43c9e6b0 167 lcd.putc(']');
sunifu 0:b5ce43c9e6b0 168 }
sunifu 0:b5ce43c9e6b0 169 }