Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Formato del Comando:
- // S-Set, L-LVDT o T-Temp - 10 bits '0001010101' finalizar con un '.'
- // Ejemplo: SL-512 -> SL-1000000000.
- // C -> Calibrar Cero
- // A -> Actuadores Ej: A-1010.
- String Comando = ""; // String donde se guardan los comandos que se reciben
- bool RxComplete = false; // Variable para saber cuando termino la recepcion de datos
- const short int Pin_LVDT = A0, Pin_TEMP = A1; //Pines de lectura de las variables
- const short int COOLER = 2, MOTOR = 3, A_VIS = 4, ACT4 = 5; //Pines de los actuadores
- int SET_LVDT = 512, SET_TEMP = 512; //Set de cada variable en formato de 10 bits
- int LVDT,TEMP,CERO=512; //Variables globales
- bool ACT_COOLER=false,AV = false;
- unsigned long Tiempo=0;
- void setup()
- {
- pinMode(Pin_LVDT,INPUT);
- pinMode(Pin_TEMP,INPUT);
- pinMode(MOTOR ,OUTPUT);
- pinMode(COOLER,OUTPUT);
- pinMode(A_VIS ,OUTPUT);
- pinMode(ACT4 ,OUTPUT);
- pinMode(13,OUTPUT); //Led integrado en la placa
- Comando.reserve(20); // Reserva 50 bytes para el Comando a recibir
- Serial.begin(9600);
- //Serial.print("LVDT LISTO."); //Mensaje de que se termino el setup
- }
- int COM_CONV() //Funcion encargada de convertir a int el comando de seteo
- {
- int b = 1, Conv = 0; //Variables
- for(int i=12;i>=3;i--) //Ciclo for de comparacion
- {
- if (Comando[i] == '1') //si es = '1'
- {
- Conv += b; //Suma 2^i
- }
- b = b*2; //B = 2*b ej: 4*2=8 binario
- }
- return Conv; //Valor a retornar
- }
- void Sets_y_Cero()
- {
- Serial.print("SL-");
- Serial.print(SET_LVDT);
- Serial.print('.');
- Serial.print("ST-");
- Serial.print(SET_TEMP);
- Serial.print('.');
- Serial.print("C-");
- Serial.print(CERO);
- Serial.print('.');
- }
- void Estado_Actuadores() //Funcion para observar los actuadores
- {
- Serial.print("E-");
- Serial.print(digitalRead(COOLER));
- Serial.print(digitalRead(MOTOR));
- Serial.print(digitalRead(A_VIS));
- Serial.print(digitalRead(ACT4));
- Serial.print('.');
- }
- void Check_Condiciones()
- {
- if((TEMP>SET_TEMP)||(ACT_COOLER))
- digitalWrite(COOLER,HIGH);
- else
- digitalWrite(COOLER,LOW);
- Estado_Actuadores();
- }
- void Check_AV()
- {
- unsigned long TenSeg = 0;
- if((TEMP>SET_TEMP)&&(abs(LVDT-CERO)>SET_LVDT))
- TenSeg = millis() - Tiempo;
- else
- Tiempo = millis();
- if(TenSeg>10000)
- {
- digitalWrite(MOTOR,LOW);
- digitalWrite(A_VIS,HIGH);
- }
- if((TenSeg>10000)&&(!AV))
- {
- Serial.print("AV-ON.");
- AV=true;
- }
- if((TenSeg<10000)&&(AV))
- {
- Serial.print("AV-OFF.");
- AV=false;
- }
- }
- void Check_COMANDO() //Chequeo del comando recibido
- {
- if (RxComplete)
- {
- if (Comando[0] == 'S')
- {
- if (Comando[1] == 'L')
- {
- SET_LVDT = COM_CONV();
- Serial.print("SL-");
- Serial.print(SET_LVDT);
- Serial.print('.');
- }
- else //Comando [1] == 'T'
- {
- SET_TEMP = COM_CONV();
- Serial.print("ST-");
- Serial.print(SET_TEMP);
- Serial.print('.');
- }
- }
- else if(Comando[0] == 'A')
- {
- ACT_COOLER=(Comando[2] == '1');
- digitalWrite(COOLER,ACT_COOLER);
- digitalWrite(MOTOR,(Comando[3] == '1'));
- digitalWrite(A_VIS,(Comando[4] == '1'));
- digitalWrite(ACT4,(Comando[5] == '1'));
- }
- else if (Comando[0] == 'C')
- {
- CERO = LVDT;
- Sets_y_Cero();
- }
- else if (Comando[0] == 'P')
- {
- Sets_y_Cero();
- }
- //Cuando se termine el codigo correspondiente o si no coincide
- Comando = ""; //Limpiado de la variable
- RxComplete = false; //Se coloca en falso para que no entre de nuevo a esta funcion
- }
- }
- void loop() //Inicio de ciclo principal
- {
- Check_COMANDO();
- LVDT = analogRead(Pin_LVDT); //Lectura de los ADC
- TEMP = analogRead(Pin_TEMP);
- Serial.print("I-"); //Envio de los datos
- Serial.print(LVDT);
- Serial.print(",");
- Serial.print(TEMP);
- Serial.print('.');
- Check_AV();
- Check_Condiciones();
- delay(50);
- digitalWrite(13,!digitalRead(13)); //Conmutacion del led de la placa para saber que se esta en el bucle
- }
- void serialEvent() //Interruocion a la que se entra cada vez que se ejecuta un ciclo del loop y se tiene algo en Rx
- {
- //digitalWrite(13,!digitalRead(13)); //Indicador de comando recibido
- while (Serial.available())
- {
- char inChar = (char)Serial.read(); //Variable char de entrada
- if (inChar == '.') //Si el char es '.' finaliza la concatenacion
- {
- RxComplete = true;
- continue;
- }
- else //Sino se concatena el char recibido
- Comando += inChar;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement