programa para prender el led RGB por m,edio de un control y un sensor infrarojo

Dependencies:   Pulse1 mbed

Fork of irda1 by Gustavo Ramirez

Revision:
3:4cc8e31494e0
Parent:
2:6a15ab0305c8
diff -r 6a15ab0305c8 -r 4cc8e31494e0 main.cpp
--- a/main.cpp	Fri Nov 08 07:40:04 2013 +0000
+++ b/main.cpp	Fri Nov 22 14:44:50 2013 +0000
@@ -1,48 +1,159 @@
 #include "mbed.h"
 #include <Pulse1.h>
-
+int dec;
 PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo
 Serial pc(USBTX, USBRX);
 DigitalOut led(LED1);
 DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+
 int header =0; //tiempo de cabecera pulso abajo
-const int head_H = 9100; //+20% medida con osciloscopio en microsegundos
-const int head_L = 8500;//-20%  medida con osciloscopio
+const int head_H = 8470*1.2;; //+20% medida con osciloscopio en microsegundos
+const int head_L =8470*0.8;//-20%  medida con osciloscopio
 int i=0;
-const int T_alto=1680;//ponga su tiempo de la prueba
-const int T_bajo=526;//ponga su tiempo de la prueba
-const int num_bits = 32;//ponga su numero de bits
+const int T_alto=1530;//ponga su tiempo de la prueba
+const int T_bajo=500;//ponga su tiempo de la prueba
+const int num_bits = 33;//ponga su numero de bits
 int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos
+int dat[num_bits];
+int comp[num_bits];
+int v[num_bits];
+int r[num_bits];
 int dato; // tiempo de cada dato que se lee
-  
+ int j,x,k;
+ 
 int main(){
-while(1){
-ini1: header=0;
-      led2=1;
+
+ led2=1;
       led=1;
+      led3=1;
+while(1)
+{
+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;
+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; 
+v[0]=1;
+v[1]=0;
+v[2]=1;
+ v[3]=1;
+  v[4]=0;
+   v[5]=1; 
+   v[6]=0;
+    v[7]=1;
+     v[8]=0;
+      v[9]=1;
+       v[10]=0;
+        v[11]=0;
+         v[12]=1; 
+         v[13]=0; 
+         v[14]=1;
+          v[15]=0;
+           v[16]=1;
+            v[17]=0;
+             v[18]=0;
+              v[19]=1; 
+              v[20]=1; 
+              v[ 21]=0; 
+              v[22]=0;
+               v[23]=0;
+                v[24]=0;
+                 v[25]=1;
+                  v[26]=1;
+                   v[27]=0;
+                    v[28]=0; 
+                    v[29]=1;
+                     v[30]=1;
+                      v[31]=1;
+r[0]=1;
+ r[1]=0;
+  r[2]=1;
+   r[3]=1;
+    r[4]=0;
+     r[5]=1;
+      r[6]=0;
+       r[7]=1;
+        r[8]=0;
+         r[9]=1;
+          r[10]=0;
+           r[11]=0;
+            r[12]=1;
+             r[13]=0;
+              r[14]=1;
+               r[15]=0;
+                r[16]=1;
+                 r[17]=1;
+                  r[18]=0;
+                   r[19]=0;
+                    r[20]=0;
+                     r[21]=0;
+                      r[22]=1;
+                       r[23]=0;
+                        r[24]=0;
+                         r[25]=0;
+                          r[26]=1;
+                           r[27]=1;
+                            r[28]=1;
+                             r[29]=1;
+                              r[30]=0;
+                               r[31]=1;
+ini1: header=0;     
       header = irda.read_low_us();    //funcion para leer un pulso de caida o bajo
       if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20%
       else goto ini1;
       
 seguir:
        //leo los datos de la trama y se meten a un arreglo
-      wait_us(5000);
-      led2=0;
-      for(i=0;i<(num_bits-1);++i){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
-      dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control
-      num[i]=dato;
-      wait_us(300);   
-      }
+      wait_us(2100);
+      //led2=0;
+      for(i=0;i<(num_bits-1);++i)
+          { // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
+              dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control
+              num[i]=dato;
+              wait_us(250);   
+          }//
       wait(0.5); //espero un poquito antes de leer todo el arreglo y ponerlo en pantalla 
       pc.printf(",%d",header);
-      for(i=0;i<(num_bits-1);++i){  
-      pc.printf(",%d",num[i]);
-      }
+      for(i=0;i<(num_bits-1);++i)
+          {//  
+          pc.printf(",%d",num[i]);
+          }//
       wait(0.1);  //espero e imprimo en binario 
       pc.printf("\n\n");
-      for(i=0;i<(num_bits-1);++i){  
-      if(num[i] > ((T_alto+T_bajo)/2)) pc.printf("1");
-      else pc.printf("0");
-      }
-      }
+      for(i=0;i<(num_bits-1);++i)
+      {  //
+      if(num[i] > ((T_alto+T_bajo)/2))
+          { 
+          pc.printf("1");
+          comp[i]=1;
+          }
+      else 
+          {
+         pc.printf("0");
+          comp[i]=0;
+          }
+            if(comp[i]==dat[i])
+            {
+            ++j;
+            }
+               if(j==32) led=!led;
+               if(j!=32) led=1;
+            
+            if (v[i]==comp[i])
+                {
+                    ++x;
+                }
+                    if(x==32) led2=!led2;
+                    if(x!=32)  led2=1;
+                    
+            if (r[i]==comp[i])
+                {
+                    ++k;
+                }
+                    if(k==32) led3=!led3;
+                    if(k!=32)  led3=1;            
+           
+      }//
+      j=x=k=0;
+
+      
+      }////while
       }
\ No newline at end of file