2016-11-23 75 views
3

我想提出一个红外短信发送者和接收者与一对夫妇二极管的帮助下中间冻结。为什么我的Arduino代码

它基本上可以看作两个diferent电路,发送所述一个,以及接收所述一个。

发送工作正常,在基座4号编码消息,并且发送该予设置为它此时,相应的频率的一个。

接收到的那个似乎有问题,当它计算到达波的周期时,它只是挂起,我必须重置arduino,以便它能够处理另一个命令。 (这个函数是calculator(),其他进入重建部分的函数是measure和reconstruye)

我已经做了一些调试,发现它在进入函数计算周期时挂起,但我不知道可能会做什么。

现在我不知道我能做些什么来解决的Arduino吊起来,在这里我旁边附上我做的代码,如果西班牙的意见是一个问题,我可以将它们变成英文。

感谢您的帮助!

#define Tonepin 11 //Pin where tone will play 
    #define Interrupt 2 //Pin to interrupt 
    #define maxNchars 8 //Array size 
    #define mil 1000 //Number 1000 in lettres 

    struct Alfabeto //We define an structure that contains both arrays for our alphabet 
    { 
     const char Mean[maxNchars] =   //Array of letters 
     { 'a', 'b', 'c', 'd', 'e', 'f', 'g' , 'k' }; 
     const int Meaning [maxNchars] =  //Array of letter to number 
     { 0, 1, 2, 3, 4, 5, 6, 7}; 
     const uint8_t Freq[maxNchars] = //Array of frequencies unmultiplied 
     { 2, 3, 4, 5, 6, 7, 9, 16 }; 
     const int ExpPeriods[maxNchars] = //Array of experimental periods in mS 
     { 19968, 13312, 9984, 8000, 6656, 5696, 4416, 2496}; 
    }; 

    Alfabeto abc; //Create the abc member of the structure 
    /* Frequency guide (Hz)for FreqFactor=25 
    * a = 50 
    * b = 75 
    * c = 100 
    * d = 125 
    * e = 150 
    * f = 175 
    * g = 225 
    * k = 400 
    */ 


    int toneDur = 10; //Duration of the tone in mS 
    int FreqFactor = 25; //Factor to be multiplied by frequency 
    char vectorenviar[4]; // vector donde conservar el arreglo de letras 
    int Vectorauxiliar[9]; //Vector para ayudar con la recepcion 
    volatile int Decriptadorauxiliar[4]; //Arreglo auxiliar para decriptar mensaje 
    bool decriptador = false; //Condicional auxiliar para decriptar 
    int r = 0;     //indice auxiliar para arreglo decriptador 

    volatile long time1 = 0; 
    volatile long time2 = 0; 
    volatile unsigned long intaux = false; 
    long period = 0; 

    String inputSerial = "";  //String to hold input data 
    String outputSerial = ""; //String to hold output data 
    bool finished = false;  //check is string is complete 


    String codigo4letras = ""; //string auxiliar para reconstruir 

    void reconstruye() 
    { 
    // YA TENGO 4 potencias, convertir de vuelta a numero y luego 
    // convertir dicho numero en la letraDelMensaje ex= codigo4letrsa=2310 
     int n=0; 
     for(int power=0; power<codigo4letras.length(); power++) 
     { 
     char o = codigo4letras[power]; 
     int u = (int)o; 
     u -= 48; 
     n += (u * (int)pow(4,power)); 
     } 
    char Letra= (char)n; 

     outputSerial+= Letra; 
     codigo4letras = ""; 

    Serial.print(outputSerial); 
    Serial.println(" Is what you received\n"); 

    } 

    int calculator() //Calcula el periodo de la onda recibida 
    { 
    // Serial.println("  *****Entering Calculator"); //calculator test 
     period = abs(time2 - time1); 
     Serial.print(period); 
     Serial.println(" us is the period"); //Period Test 

    int i=0; 

    double incerteza = 600; 

    for (i=0; i<sizeof(abc.ExpPeriods);i++) 
    { 

    double windowA = abc.ExpPeriods[i] + incerteza; 
    double windowB = abc.ExpPeriods[i] - incerteza; 
     if (period < windowA && period > windowB) break; 
    } 

    //Serial.print("indice "); //Indice test 
    //Serial.println(i); 

    int indiceasociado = i; 

     return indiceasociado; 
    } 

    void measure() //Mide la diferencia de tiempos entre las ondas recibidas 
    { 
     bool y = (bool)digitalRead(Interrupt); 

     if(y == true && intaux == false)  //Primera ves que se mete en el interrupt 
     { 
     time1=micros(); 
     intaux=true; 
     } 

     else if(y == true && intaux == true) //Segunda ves que se mete, calcula cosas. 
     { 
     time2=micros(); 
     intaux=false; 
     int indice = 6; //Reinicia el valor indice 
     indice = calculator(); //calcula el indice 
     for (int lalala=0;lalala<4;lalala++) 
     { 
     Serial.print(Decriptadorauxiliar[lalala]); 
     } 
     Serial.println("  *****Is The reconstructed number");   //Reconstructed number test 

     while (r < 4) 
     { 
      if (indice == 0 || indice == 1 || indice == 2 || indice == 3 && decriptador == false) //Si es un numero de 0 al 3, lo guarda en una matriz 
      { 
      Decriptadorauxiliar[r]=indice;   //Establece el contenido 
      decriptador=true; 
      break; 
      } 
     else if (indice == 7 && decriptador == true) //Si es el numero 7, no lo guarda pero aumenta a la siguiente posicion 
      { 
      r++; 
      decriptador=false; 
      break; 
      } 
     } 
       //Si el ultimo contenido de la matriz, es un numero valido definido por nosotros, empieza a reconstruir 
      if (Decriptadorauxiliar[3]== 0 || Decriptadorauxiliar[3]== 1 || Decriptadorauxiliar[3]== 2 || Decriptadorauxiliar[3]== 3) 
      { 
       for(int j=0;j<4;j++) 
       { 
       codigo4letras += Decriptadorauxiliar[j]; //Llena un string auxiliar. 
       } 
     reconstruye();   //Reconstruye la letra 
     for(int z=0;z<4;z++) //Reinicia la matriz 
     { 
     Decriptadorauxiliar[z]=6; 
     } 
     r=0;  //Reinicia un auxiliar para cuando venga la siguiente letra 
      } 
     // Serial.println("  *****Out of the else if"); //else if test 
     } 
    // Serial.println("  *****Out of the Interrupt"); //Interrupt test 
    } 

    void vectorizador(int numero, int base, char jey) //Guarda el numero en un arreglo de cuatro letras, que representa un numero en base 4 
    { 

     for(int i=0;i<4;i++) 
     { 
     vectorenviar[i]=abc.Mean[0]; 
     } 
    Serial.print(jey); 
    Serial.print(" "); 
    Serial.print(numero); 
    Serial.println(" Character and number in ASCII table"); 

     for(int indice=0; indice<4; indice++) 
     { 
     char a = abc.Mean[numero%base]; 
     Serial.print(a); 
     vectorenviar[indice]=a; 
     numero=numero/base; 
     } 
     Serial.println(" Is the number in letters"); 

    //Serial.println("SALIO"); 

    Vectorauxiliar[0]=vectorenviar[0]; 
    Vectorauxiliar[1]=abc.Mean[7]; 
    Vectorauxiliar[2]=vectorenviar[1]; 
    Vectorauxiliar[3]=abc.Mean[7]; 
    Vectorauxiliar[4]=vectorenviar[2]; 
    Vectorauxiliar[5]=abc.Mean[7]; 
    Vectorauxiliar[6]=vectorenviar[3]; 
    Vectorauxiliar[7]=abc.Mean[7]; 
    } 

    void tonesender() //Manda los tonos correspondientes a las 4 letras del vector y guarda el periodo correspondiente a esa letra 
    { 


     for(int i=0; i< sizeof(Vectorauxiliar); i++) 
     { 
     int j = 0; //Auxiliar int to link both arrays 
     char any = Vectorauxiliar[i]; //Selects the first character of the input array. 
    // int index = 0; //define the index, to link both arrays 
     for(int j=0; j < sizeof(abc.Mean) ;j++) 
     { 
      if (any == abc.Mean[j]) //Sets the index to link to Freq array 
      { 
      break; //if any == abc.Mean[index], break for loop 
      } 
     } 
      int freqM = abc.Freq[j]; //defines the freq to use 
      freqM *= FreqFactor; //applies freq factor 
      tone(Tonepin, freqM, toneDur); //Sends tone from pin(tonepin), with freq(FreqM) and duration(toneDur) 
      Serial.println("  *****Sending tone"); //tone test test 
      delay(2*toneDur); 
     } 
    } 


    void setup() { 
    Serial.begin(9600); //Inizialize serial comunications 
    inputSerial.reserve(100);  //reserves 100 bytes for string 
    pinMode(Tonepin, OUTPUT); 
    pinMode(Interrupt, INPUT); 
    attachInterrupt(digitalPinToInterrupt(Interrupt), measure, CHANGE); 
    Serial.print("Enter your Message to be transmitted: "); 
    Serial.println(""); 
    } 

    void loop() { 
     if(finished==true) 
     { 

     outputSerial = ""; 

     Serial.print(inputSerial); 
     Serial.print(" Is what you typed"); 
     Serial.println(""); 

    for(int i=0; i<(inputSerial.length()-2); i++) 
     { 

     for(int z=0;z<4;z++) 
       { 
       Decriptadorauxiliar[z]=6; 
       } 


     char h = inputSerial[i]; 
    // Serial.println(h); 
    int k = (int) h; 
    // Serial.println(k); 
     vectorizador(k, 4, h); 
     tonesender(); 
     } 

     finished= false; 
     inputSerial= ""; 
     } 
    } 

    void serialEvent() 
    { 
     if(Serial.available()) 
     { 
     //Get new byte: 
     char aux = (char)Serial.read(); 
     //add it to inputSerial: 
     inputSerial += aux; 
     // if incoming character is carriage return or newline, finish the string concatenation 
      if (aux == '\n' || aux == '\rn') 
      { 
      finished= true; 
      } 
     } 
    } 

回答

2

这段代码看起来很奇怪(甚至对于西班牙语的Arduino代码)。

我不知道,你可以做你与struct Alfabeto做了什么,还有,即显示默认的初始化的模板。

但这:

for (i=0; i<sizeof(abc.ExpPeriods);i++) 

必须是错误的,甚至在Arduino的土地sizeof应产生的字节大小,而不是数组的长度。由于这是一组int,这大大超越了阵列,带来了随机结果。它应该是:

for (i = 0; i < sizeof abs.ExpPeriods/sizeof *abs.ExpPeriods; ++i)' 

通过sizeof (int)划分是(在我看来)更糟糕的解决方案,因为这对硬编码的变量在第二位的类型,这将打破,如果类型不断变化的知识。

+0

是的,这是它,谢谢! – Ovog