programa para prender el led RGB por m,edio de un control y un sensor infrarojo
Fork of irda1 by
main.cpp@3:4cc8e31494e0, 2013-11-22 (annotated)
- Committer:
- caaruizze
- Date:
- Fri Nov 22 14:44:50 2013 +0000
- Revision:
- 3:4cc8e31494e0
- Parent:
- 2:6a15ab0305c8
control de led on-off rgb por medio de un sensor infrarojo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 0:74d57f8ae247 | 1 | #include "mbed.h" |
tony63 | 0:74d57f8ae247 | 2 | #include <Pulse1.h> |
caaruizze | 3:4cc8e31494e0 | 3 | int dec; |
tony63 | 2:6a15ab0305c8 | 4 | PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo |
tony63 | 0:74d57f8ae247 | 5 | Serial pc(USBTX, USBRX); |
tony63 | 0:74d57f8ae247 | 6 | DigitalOut led(LED1); |
tony63 | 2:6a15ab0305c8 | 7 | DigitalOut led2(LED2); |
caaruizze | 3:4cc8e31494e0 | 8 | DigitalOut led3(LED3); |
caaruizze | 3:4cc8e31494e0 | 9 | |
tony63 | 2:6a15ab0305c8 | 10 | int header =0; //tiempo de cabecera pulso abajo |
caaruizze | 3:4cc8e31494e0 | 11 | const int head_H = 8470*1.2;; //+20% medida con osciloscopio en microsegundos |
caaruizze | 3:4cc8e31494e0 | 12 | const int head_L =8470*0.8;//-20% medida con osciloscopio |
tony63 | 2:6a15ab0305c8 | 13 | int i=0; |
caaruizze | 3:4cc8e31494e0 | 14 | const int T_alto=1530;//ponga su tiempo de la prueba |
caaruizze | 3:4cc8e31494e0 | 15 | const int T_bajo=500;//ponga su tiempo de la prueba |
caaruizze | 3:4cc8e31494e0 | 16 | const int num_bits = 33;//ponga su numero de bits |
tony63 | 2:6a15ab0305c8 | 17 | int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos |
caaruizze | 3:4cc8e31494e0 | 18 | int dat[num_bits]; |
caaruizze | 3:4cc8e31494e0 | 19 | int comp[num_bits]; |
caaruizze | 3:4cc8e31494e0 | 20 | int v[num_bits]; |
caaruizze | 3:4cc8e31494e0 | 21 | int r[num_bits]; |
tony63 | 2:6a15ab0305c8 | 22 | int dato; // tiempo de cada dato que se lee |
caaruizze | 3:4cc8e31494e0 | 23 | int j,x,k; |
caaruizze | 3:4cc8e31494e0 | 24 | |
tony63 | 0:74d57f8ae247 | 25 | int main(){ |
caaruizze | 3:4cc8e31494e0 | 26 | |
caaruizze | 3:4cc8e31494e0 | 27 | led2=1; |
tony63 | 0:74d57f8ae247 | 28 | led=1; |
caaruizze | 3:4cc8e31494e0 | 29 | led3=1; |
caaruizze | 3:4cc8e31494e0 | 30 | while(1) |
caaruizze | 3:4cc8e31494e0 | 31 | { |
caaruizze | 3:4cc8e31494e0 | 32 | dat[0]=dat[2]=dat[3]=dat[5]=dat[7]=dat[9]=dat[12]=dat[14]=1,dat[16]=0,dat[17]=1,dat[19]=0,dat[26]=dat[28]=dat[29]=1,dat[30]=0,dat[31]=1; |
caaruizze | 3:4cc8e31494e0 | 33 | dat[1]=0,dat[4]=dat[6]=dat[8]=dat[10]=dat[11]=dat[13]=dat[15]=dat[18]=dat[20]=dat[21]=0,dat[22]=1,dat[23]=0,dat[24]=1,dat[25]=0,dat[27]=1; |
caaruizze | 3:4cc8e31494e0 | 34 | v[0]=1; |
caaruizze | 3:4cc8e31494e0 | 35 | v[1]=0; |
caaruizze | 3:4cc8e31494e0 | 36 | v[2]=1; |
caaruizze | 3:4cc8e31494e0 | 37 | v[3]=1; |
caaruizze | 3:4cc8e31494e0 | 38 | v[4]=0; |
caaruizze | 3:4cc8e31494e0 | 39 | v[5]=1; |
caaruizze | 3:4cc8e31494e0 | 40 | v[6]=0; |
caaruizze | 3:4cc8e31494e0 | 41 | v[7]=1; |
caaruizze | 3:4cc8e31494e0 | 42 | v[8]=0; |
caaruizze | 3:4cc8e31494e0 | 43 | v[9]=1; |
caaruizze | 3:4cc8e31494e0 | 44 | v[10]=0; |
caaruizze | 3:4cc8e31494e0 | 45 | v[11]=0; |
caaruizze | 3:4cc8e31494e0 | 46 | v[12]=1; |
caaruizze | 3:4cc8e31494e0 | 47 | v[13]=0; |
caaruizze | 3:4cc8e31494e0 | 48 | v[14]=1; |
caaruizze | 3:4cc8e31494e0 | 49 | v[15]=0; |
caaruizze | 3:4cc8e31494e0 | 50 | v[16]=1; |
caaruizze | 3:4cc8e31494e0 | 51 | v[17]=0; |
caaruizze | 3:4cc8e31494e0 | 52 | v[18]=0; |
caaruizze | 3:4cc8e31494e0 | 53 | v[19]=1; |
caaruizze | 3:4cc8e31494e0 | 54 | v[20]=1; |
caaruizze | 3:4cc8e31494e0 | 55 | v[ 21]=0; |
caaruizze | 3:4cc8e31494e0 | 56 | v[22]=0; |
caaruizze | 3:4cc8e31494e0 | 57 | v[23]=0; |
caaruizze | 3:4cc8e31494e0 | 58 | v[24]=0; |
caaruizze | 3:4cc8e31494e0 | 59 | v[25]=1; |
caaruizze | 3:4cc8e31494e0 | 60 | v[26]=1; |
caaruizze | 3:4cc8e31494e0 | 61 | v[27]=0; |
caaruizze | 3:4cc8e31494e0 | 62 | v[28]=0; |
caaruizze | 3:4cc8e31494e0 | 63 | v[29]=1; |
caaruizze | 3:4cc8e31494e0 | 64 | v[30]=1; |
caaruizze | 3:4cc8e31494e0 | 65 | v[31]=1; |
caaruizze | 3:4cc8e31494e0 | 66 | r[0]=1; |
caaruizze | 3:4cc8e31494e0 | 67 | r[1]=0; |
caaruizze | 3:4cc8e31494e0 | 68 | r[2]=1; |
caaruizze | 3:4cc8e31494e0 | 69 | r[3]=1; |
caaruizze | 3:4cc8e31494e0 | 70 | r[4]=0; |
caaruizze | 3:4cc8e31494e0 | 71 | r[5]=1; |
caaruizze | 3:4cc8e31494e0 | 72 | r[6]=0; |
caaruizze | 3:4cc8e31494e0 | 73 | r[7]=1; |
caaruizze | 3:4cc8e31494e0 | 74 | r[8]=0; |
caaruizze | 3:4cc8e31494e0 | 75 | r[9]=1; |
caaruizze | 3:4cc8e31494e0 | 76 | r[10]=0; |
caaruizze | 3:4cc8e31494e0 | 77 | r[11]=0; |
caaruizze | 3:4cc8e31494e0 | 78 | r[12]=1; |
caaruizze | 3:4cc8e31494e0 | 79 | r[13]=0; |
caaruizze | 3:4cc8e31494e0 | 80 | r[14]=1; |
caaruizze | 3:4cc8e31494e0 | 81 | r[15]=0; |
caaruizze | 3:4cc8e31494e0 | 82 | r[16]=1; |
caaruizze | 3:4cc8e31494e0 | 83 | r[17]=1; |
caaruizze | 3:4cc8e31494e0 | 84 | r[18]=0; |
caaruizze | 3:4cc8e31494e0 | 85 | r[19]=0; |
caaruizze | 3:4cc8e31494e0 | 86 | r[20]=0; |
caaruizze | 3:4cc8e31494e0 | 87 | r[21]=0; |
caaruizze | 3:4cc8e31494e0 | 88 | r[22]=1; |
caaruizze | 3:4cc8e31494e0 | 89 | r[23]=0; |
caaruizze | 3:4cc8e31494e0 | 90 | r[24]=0; |
caaruizze | 3:4cc8e31494e0 | 91 | r[25]=0; |
caaruizze | 3:4cc8e31494e0 | 92 | r[26]=1; |
caaruizze | 3:4cc8e31494e0 | 93 | r[27]=1; |
caaruizze | 3:4cc8e31494e0 | 94 | r[28]=1; |
caaruizze | 3:4cc8e31494e0 | 95 | r[29]=1; |
caaruizze | 3:4cc8e31494e0 | 96 | r[30]=0; |
caaruizze | 3:4cc8e31494e0 | 97 | r[31]=1; |
caaruizze | 3:4cc8e31494e0 | 98 | ini1: header=0; |
tony63 | 2:6a15ab0305c8 | 99 | header = irda.read_low_us(); //funcion para leer un pulso de caida o bajo |
tony63 | 2:6a15ab0305c8 | 100 | if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20% |
tony63 | 2:6a15ab0305c8 | 101 | else goto ini1; |
tony63 | 0:74d57f8ae247 | 102 | |
tony63 | 2:6a15ab0305c8 | 103 | seguir: |
tony63 | 2:6a15ab0305c8 | 104 | //leo los datos de la trama y se meten a un arreglo |
caaruizze | 3:4cc8e31494e0 | 105 | wait_us(2100); |
caaruizze | 3:4cc8e31494e0 | 106 | //led2=0; |
caaruizze | 3:4cc8e31494e0 | 107 | for(i=0;i<(num_bits-1);++i) |
caaruizze | 3:4cc8e31494e0 | 108 | { // POR OSCILOSCOPIO se determina que llegan (num_bits),datos |
caaruizze | 3:4cc8e31494e0 | 109 | dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control |
caaruizze | 3:4cc8e31494e0 | 110 | num[i]=dato; |
caaruizze | 3:4cc8e31494e0 | 111 | wait_us(250); |
caaruizze | 3:4cc8e31494e0 | 112 | }// |
tony63 | 2:6a15ab0305c8 | 113 | wait(0.5); //espero un poquito antes de leer todo el arreglo y ponerlo en pantalla |
tony63 | 2:6a15ab0305c8 | 114 | pc.printf(",%d",header); |
caaruizze | 3:4cc8e31494e0 | 115 | for(i=0;i<(num_bits-1);++i) |
caaruizze | 3:4cc8e31494e0 | 116 | {// |
caaruizze | 3:4cc8e31494e0 | 117 | pc.printf(",%d",num[i]); |
caaruizze | 3:4cc8e31494e0 | 118 | }// |
tony63 | 2:6a15ab0305c8 | 119 | wait(0.1); //espero e imprimo en binario |
tony63 | 2:6a15ab0305c8 | 120 | pc.printf("\n\n"); |
caaruizze | 3:4cc8e31494e0 | 121 | for(i=0;i<(num_bits-1);++i) |
caaruizze | 3:4cc8e31494e0 | 122 | { // |
caaruizze | 3:4cc8e31494e0 | 123 | if(num[i] > ((T_alto+T_bajo)/2)) |
caaruizze | 3:4cc8e31494e0 | 124 | { |
caaruizze | 3:4cc8e31494e0 | 125 | pc.printf("1"); |
caaruizze | 3:4cc8e31494e0 | 126 | comp[i]=1; |
caaruizze | 3:4cc8e31494e0 | 127 | } |
caaruizze | 3:4cc8e31494e0 | 128 | else |
caaruizze | 3:4cc8e31494e0 | 129 | { |
caaruizze | 3:4cc8e31494e0 | 130 | pc.printf("0"); |
caaruizze | 3:4cc8e31494e0 | 131 | comp[i]=0; |
caaruizze | 3:4cc8e31494e0 | 132 | } |
caaruizze | 3:4cc8e31494e0 | 133 | if(comp[i]==dat[i]) |
caaruizze | 3:4cc8e31494e0 | 134 | { |
caaruizze | 3:4cc8e31494e0 | 135 | ++j; |
caaruizze | 3:4cc8e31494e0 | 136 | } |
caaruizze | 3:4cc8e31494e0 | 137 | if(j==32) led=!led; |
caaruizze | 3:4cc8e31494e0 | 138 | if(j!=32) led=1; |
caaruizze | 3:4cc8e31494e0 | 139 | |
caaruizze | 3:4cc8e31494e0 | 140 | if (v[i]==comp[i]) |
caaruizze | 3:4cc8e31494e0 | 141 | { |
caaruizze | 3:4cc8e31494e0 | 142 | ++x; |
caaruizze | 3:4cc8e31494e0 | 143 | } |
caaruizze | 3:4cc8e31494e0 | 144 | if(x==32) led2=!led2; |
caaruizze | 3:4cc8e31494e0 | 145 | if(x!=32) led2=1; |
caaruizze | 3:4cc8e31494e0 | 146 | |
caaruizze | 3:4cc8e31494e0 | 147 | if (r[i]==comp[i]) |
caaruizze | 3:4cc8e31494e0 | 148 | { |
caaruizze | 3:4cc8e31494e0 | 149 | ++k; |
caaruizze | 3:4cc8e31494e0 | 150 | } |
caaruizze | 3:4cc8e31494e0 | 151 | if(k==32) led3=!led3; |
caaruizze | 3:4cc8e31494e0 | 152 | if(k!=32) led3=1; |
caaruizze | 3:4cc8e31494e0 | 153 | |
caaruizze | 3:4cc8e31494e0 | 154 | }// |
caaruizze | 3:4cc8e31494e0 | 155 | j=x=k=0; |
caaruizze | 3:4cc8e31494e0 | 156 | |
caaruizze | 3:4cc8e31494e0 | 157 | |
caaruizze | 3:4cc8e31494e0 | 158 | }////while |
tony63 | 2:6a15ab0305c8 | 159 | } |