Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. //  Formato del Comando:
  4. //  S-Set, L-LVDT o T-Temp - 10 bits '0001010101' finalizar con un '.'
  5. //  Ejemplo: SL-512 -> SL-1000000000.
  6. //  C -> Calibrar Cero
  7. //  A -> Actuadores  Ej: A-1010.
  8.  
  9.  
  10. String Comando = "";         // String donde se guardan los comandos que se reciben
  11. bool RxComplete = false;  // Variable para saber cuando termino la recepcion de datos
  12.  
  13. const short int Pin_LVDT = A0, Pin_TEMP = A1; //Pines de lectura de las variables
  14. const short int COOLER = 2, MOTOR = 3, A_VIS = 4, ACT4 = 5;  //Pines de los actuadores
  15.  
  16.  
  17. int SET_LVDT = 512, SET_TEMP = 512; //Set de cada variable en formato de 10 bits
  18. int LVDT,TEMP,CERO=512;                 //Variables globales
  19. bool ACT_COOLER=false,AV = false;
  20. unsigned long Tiempo=0;
  21.  
  22.  
  23. void setup()
  24. {
  25.   pinMode(Pin_LVDT,INPUT);
  26.   pinMode(Pin_TEMP,INPUT);
  27.  
  28.   pinMode(MOTOR ,OUTPUT);
  29.   pinMode(COOLER,OUTPUT);
  30.   pinMode(A_VIS ,OUTPUT);
  31.   pinMode(ACT4  ,OUTPUT);
  32.  
  33.   pinMode(13,OUTPUT); //Led integrado en la placa
  34.  
  35.   Comando.reserve(20);  // Reserva 50 bytes para el Comando a recibir
  36.  
  37.   Serial.begin(9600);
  38.   //Serial.print("LVDT LISTO."); //Mensaje de que se termino el setup    
  39. }
  40.  
  41. int COM_CONV()   //Funcion encargada de convertir a int el comando de seteo
  42. {
  43.   int b = 1, Conv = 0;      //Variables
  44.   for(int i=12;i>=3;i--)   //Ciclo for de comparacion
  45.   {
  46.     if (Comando[i] == '1')  //si es = '1'
  47.     {
  48.       Conv += b;            //Suma 2^i
  49.     }
  50.     b = b*2;                //B = 2*b ej: 4*2=8 binario
  51.   }
  52.   return Conv;      //Valor a retornar
  53. }
  54.  
  55. void Sets_y_Cero()
  56. {
  57.   Serial.print("SL-");
  58.   Serial.print(SET_LVDT);
  59.   Serial.print('.');
  60.   Serial.print("ST-");
  61.   Serial.print(SET_TEMP);
  62.   Serial.print('.');
  63.   Serial.print("C-");
  64.   Serial.print(CERO);
  65.   Serial.print('.');
  66. }
  67.  
  68. void Estado_Actuadores()    //Funcion para observar los actuadores
  69. {
  70.   Serial.print("E-");
  71.   Serial.print(digitalRead(COOLER));
  72.   Serial.print(digitalRead(MOTOR));
  73.   Serial.print(digitalRead(A_VIS));
  74.   Serial.print(digitalRead(ACT4));
  75.   Serial.print('.');
  76. }
  77.  
  78. void Check_Condiciones()
  79. {
  80.   if((TEMP>SET_TEMP)||(ACT_COOLER))
  81.         digitalWrite(COOLER,HIGH);        
  82.      
  83.   else
  84.         digitalWrite(COOLER,LOW);
  85.  
  86.   Estado_Actuadores();
  87. }
  88.  
  89. void Check_AV()
  90. {
  91.   unsigned long TenSeg = 0;
  92.  
  93.   if((TEMP>SET_TEMP)&&(abs(LVDT-CERO)>SET_LVDT))
  94.     TenSeg = millis() - Tiempo;
  95.     else
  96.     Tiempo = millis();
  97.    
  98.   if(TenSeg>10000)  
  99.   {    
  100.     digitalWrite(MOTOR,LOW);        
  101.     digitalWrite(A_VIS,HIGH);
  102.   }
  103.  
  104.   if((TenSeg>10000)&&(!AV))
  105.   {
  106.     Serial.print("AV-ON.");
  107.     AV=true;
  108.   }
  109.   if((TenSeg<10000)&&(AV))
  110.   {
  111.     Serial.print("AV-OFF.");
  112.     AV=false;
  113.   }
  114. }
  115.  
  116. void Check_COMANDO()      //Chequeo del comando recibido
  117. {
  118.   if (RxComplete)
  119.     {
  120.       if (Comando[0] == 'S')
  121.       {
  122.         if (Comando[1] == 'L')
  123.         {
  124.           SET_LVDT = COM_CONV();
  125.           Serial.print("SL-");
  126.           Serial.print(SET_LVDT);
  127.           Serial.print('.');
  128.         }
  129.         else //Comando [1] == 'T'
  130.         {
  131.           SET_TEMP = COM_CONV();
  132.           Serial.print("ST-");
  133.           Serial.print(SET_TEMP);
  134.           Serial.print('.');
  135.         }
  136.       }
  137.      
  138.      else if(Comando[0] == 'A')
  139.       {
  140.         ACT_COOLER=(Comando[2] == '1');
  141.         digitalWrite(COOLER,ACT_COOLER);
  142.         digitalWrite(MOTOR,(Comando[3] == '1'));
  143.         digitalWrite(A_VIS,(Comando[4] == '1'));
  144.         digitalWrite(ACT4,(Comando[5] == '1'));
  145.       }
  146.      
  147.      else if (Comando[0] == 'C')
  148.       {
  149.         CERO = LVDT;
  150.         Sets_y_Cero();
  151.       }
  152.      else if (Comando[0] == 'P')
  153.      {
  154.       Sets_y_Cero();
  155.      }
  156.      
  157.      //Cuando se termine el codigo correspondiente o si no coincide
  158.    
  159.     Comando = "";   //Limpiado de la variable
  160.     RxComplete = false;   //Se coloca en falso para que no entre de nuevo a esta funcion
  161.     }
  162. }
  163.  
  164. void loop()     //Inicio de ciclo principal
  165. {
  166.     Check_COMANDO();
  167.  
  168.     LVDT = analogRead(Pin_LVDT);     //Lectura de los ADC
  169.     TEMP = analogRead(Pin_TEMP);
  170.  
  171.     Serial.print("I-");             //Envio de los datos
  172.     Serial.print(LVDT);
  173.     Serial.print(",");
  174.     Serial.print(TEMP);    
  175.     Serial.print('.');
  176.    
  177.     Check_AV();
  178.     Check_Condiciones();
  179.          
  180.     delay(50);
  181.     digitalWrite(13,!digitalRead(13));    //Conmutacion del led de la placa para saber que se esta en el bucle
  182. }
  183.  
  184. void serialEvent()        //Interruocion a la que se entra cada vez que se ejecuta un ciclo del loop y se tiene algo en Rx
  185. {
  186.   //digitalWrite(13,!digitalRead(13));  //Indicador de comando recibido
  187.   while (Serial.available())
  188.   {
  189.     char inChar = (char)Serial.read(); //Variable char de entrada
  190.    
  191.     if (inChar == '.') //Si el char es '.' finaliza la concatenacion
  192.     {
  193.       RxComplete = true;
  194.       continue;
  195.     }
  196.     else              //Sino se concatena el char recibido
  197.     Comando += inChar;    
  198.   }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement