Pio IX 6ºTecnica - Integrantes: Facundo Carmona, Facundo Sussini - Controlador de cooler mediante un DS1820 y un potenciometro

Dependencies:   mbed DS1820

Committer:
Susso
Date:
Tue Jun 04 12:13:58 2019 +0000
Revision:
6:90322c14f783
Parent:
5:6515379eac2f
Child:
7:a74643e9de62
vvh;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:e069f9f26768 1
Sissors 0:e069f9f26768 2 #include "mbed.h"
Sissors 0:e069f9f26768 3 #include "DS1820.h"
Susso 5:6515379eac2f 4 #define DATA_PIN A0
Sissors 0:e069f9f26768 5
Susso 5:6515379eac2f 6 #define abierto 0
Susso 5:6515379eac2f 7 #define minimo 1
Susso 5:6515379eac2f 8 #define lineal 2
Susso 5:6515379eac2f 9 #define maximo 3
Susso 5:6515379eac2f 10 #define error 4
Susso 5:6515379eac2f 11
Susso 5:6515379eac2f 12 //ejemplo
Susso 5:6515379eac2f 13 PwmOut g(LED_GREEN);
Susso 5:6515379eac2f 14
Susso 5:6515379eac2f 15 /* defino la funcion principal */
Susso 5:6515379eac2f 16 void sensorsito();
Susso 5:6515379eac2f 17
Susso 5:6515379eac2f 18 /* defino las funciones, ticker y las variables de las vueltas */
Susso 5:6515379eac2f 19
Susso 5:6515379eac2f 20 void pulsos();
Susso 5:6515379eac2f 21
Susso 5:6515379eac2f 22
Susso 5:6515379eac2f 23 char hall=0, vueltas=0, arranque=0;
Susso 5:6515379eac2f 24 void contador2();
Susso 5:6515379eac2f 25 void pulsadorsito2();
Susso 5:6515379eac2f 26 char salida2=0,cuenta2=0, PIN2=0;
Susso 5:6515379eac2f 27 Ticker ti2;
Susso 5:6515379eac2f 28 DigitalIn pin2(A4,PullUp);
Susso 5:6515379eac2f 29
Susso 5:6515379eac2f 30 /* defino lo relacionado al sensor de temperatura y el cooler */
Susso 5:6515379eac2f 31 DS1820 probe(DATA_PIN);
Susso 5:6515379eac2f 32 AnalogIn ADC(A1); // definimos la entrada de ADC
Susso 5:6515379eac2f 33 PwmOut velocidad(A2);
Susso 5:6515379eac2f 34 int temperatura=0,sensor=1;
Susso 5:6515379eac2f 35 uint16_t adcValue; // nos da un rango entre 0-65535
Susso 5:6515379eac2f 36 float voltage, porcentageADC,porcentajetemperatura; // definimos el voltaje y el porcentaje
Susso 5:6515379eac2f 37
Susso 5:6515379eac2f 38 /* defino la fucion , ticker y las variables del pulsador1-- en este caso voy a hacer que la salida se invierta en vez de un pulso */
Susso 5:6515379eac2f 39 void pulsadorsito1();
Susso 5:6515379eac2f 40 void contador1();
Susso 5:6515379eac2f 41 DigitalIn pin1(A3,PullUp);
Susso 5:6515379eac2f 42 char salida1=0,cuenta1=0, PIN1=0;
Susso 5:6515379eac2f 43 Ticker ti1;
Susso 5:6515379eac2f 44
Susso 5:6515379eac2f 45 int main()
Susso 5:6515379eac2f 46 {
Susso 5:6515379eac2f 47 //ejemplo
Susso 5:6515379eac2f 48 g.period(0.0025f);
Susso 5:6515379eac2f 49
Susso 5:6515379eac2f 50 /* defino los tickers */
Susso 5:6515379eac2f 51 ti1.attach(&contador1, 0.001);
Susso 5:6515379eac2f 52 ti2.attach(&contador2, 0.001);
Susso 5:6515379eac2f 53
Susso 5:6515379eac2f 54 /* frecuencia de PWM del cooler */
Susso 5:6515379eac2f 55 velocidad.period(0.0025f); // 400hz
Susso 5:6515379eac2f 56
Susso 5:6515379eac2f 57 while(1) {
Susso 5:6515379eac2f 58 adcValue=ADC.read_u16(); // ponemos el valor del ADC en la variable tipo uint16_t
Susso 5:6515379eac2f 59 voltage=adcValue*3.3 / 65535; // convertimos el valor de ADC a volts
Susso 5:6515379eac2f 60 porcentageADC = voltage*100 / 3.3; // con el valor en volts podemos saber el porcentaje del ADC
Susso 5:6515379eac2f 61
Susso 6:90322c14f783 62 probe.convertTemperature(false, DS1820::all_devices); //Start temperature conversion, wait until ready
Susso 5:6515379eac2f 63 // probe.temperature() ---> temperatura en Cº
Susso 5:6515379eac2f 64 temperatura=probe.temperature();
Susso 5:6515379eac2f 65 porcentajetemperatura=(((float)temperatura-20)*2)/100;
Susso 5:6515379eac2f 66 //0.013*(float)temperatura+0.09
Susso 5:6515379eac2f 67 /* llamo a mis funciones */
Susso 5:6515379eac2f 68 pulsadorsito1();
Susso 5:6515379eac2f 69 pulsadorsito2();
Susso 5:6515379eac2f 70 sensorsito();
Susso 5:6515379eac2f 71 pulsos();
Susso 5:6515379eac2f 72 }
Susso 5:6515379eac2f 73 }
Susso 5:6515379eac2f 74 /* funcion principal------------------------------------------------------------ */
Susso 5:6515379eac2f 75 void sensorsito()
Susso 5:6515379eac2f 76 {
Susso 5:6515379eac2f 77 // arranco el cooler
Susso 5:6515379eac2f 78 if(arranque==0)
Susso 5:6515379eac2f 79 {
Susso 5:6515379eac2f 80 g=0.33;
Susso 5:6515379eac2f 81 velocidad=0.33;
Susso 5:6515379eac2f 82 arranque=1;
Susso 5:6515379eac2f 83 printf("Arranque \r\n");
Susso 5:6515379eac2f 84 }
Susso 5:6515379eac2f 85
Susso 5:6515379eac2f 86 switch(sensor) {
Susso 5:6515379eac2f 87 case abierto:
Susso 5:6515379eac2f 88 if(salida1==0 ) {
Susso 5:6515379eac2f 89 /* en el caso que quiera controlar el cooler con el potenciometro */
Susso 5:6515379eac2f 90 velocidad=(100-porcentageADC)/100;// el PWM cambia proporcionalmente a la variacion del ADC
Susso 5:6515379eac2f 91 g=(100-porcentageADC)/100.0;
Susso 5:6515379eac2f 92 printf("PWM : %.2f \r\n",porcentageADC);
Susso 5:6515379eac2f 93 /* cada segundo leo la cantidad de vueltas */
Susso 5:6515379eac2f 94 vueltas=hall/4;
Susso 5:6515379eac2f 95 printf("La velocidad es de: %d rpm \r\n",vueltas*60);
Susso 5:6515379eac2f 96 vueltas=0;
Susso 5:6515379eac2f 97 hall=0;
Susso 5:6515379eac2f 98 }
Susso 5:6515379eac2f 99 else
Susso 5:6515379eac2f 100 sensor=minimo;
Susso 5:6515379eac2f 101 break;
Susso 5:6515379eac2f 102 /* lo siguiente se ejecutara si quiero que el cooler lo controle el sensor de temperatura */
Susso 5:6515379eac2f 103
Susso 5:6515379eac2f 104 case minimo:
Susso 5:6515379eac2f 105 if(salida1==1) {
Susso 5:6515379eac2f 106 if(temperatura<0)
Susso 5:6515379eac2f 107 sensor=error;
Susso 5:6515379eac2f 108 else if(temperatura<=20) {
Susso 5:6515379eac2f 109 g=1.0;
Susso 5:6515379eac2f 110 velocidad=1.0;// PWM ---> 0%
Susso 5:6515379eac2f 111 printf("PWM : 0 \r\n");
Susso 5:6515379eac2f 112 printf("temperatura = %d \r\n",temperatura);
Susso 5:6515379eac2f 113 } else if(temperatura>20)
Susso 5:6515379eac2f 114 sensor=lineal;
Susso 5:6515379eac2f 115 } else
Susso 5:6515379eac2f 116 sensor=abierto;
Susso 5:6515379eac2f 117 break;
Susso 5:6515379eac2f 118
Susso 5:6515379eac2f 119 case lineal:
Susso 5:6515379eac2f 120 if(salida1==1) {
Susso 5:6515379eac2f 121 if(temperatura>20 && temperatura<=70) {
Susso 5:6515379eac2f 122 g=1-porcentajetemperatura;
Susso 5:6515379eac2f 123 velocidad=1-porcentajetemperatura;// el PWM varia proporcionalmente a ala variacion de temperatura
Susso 5:6515379eac2f 124 printf("PWM : %.3f , temperatura = %d \r\n",porcentajetemperatura,temperatura);
Susso 5:6515379eac2f 125 } else if(temperatura>70)
Susso 5:6515379eac2f 126 sensor=maximo;
Susso 5:6515379eac2f 127 else if(temperatura<20)
Susso 5:6515379eac2f 128 sensor=minimo;
Susso 5:6515379eac2f 129 } else
Susso 5:6515379eac2f 130 sensor=abierto;
Susso 5:6515379eac2f 131 break;
Susso 5:6515379eac2f 132
Susso 5:6515379eac2f 133 case maximo:
Susso 5:6515379eac2f 134 if(salida1==1) {
Susso 5:6515379eac2f 135 if(temperatura>70) {
Susso 5:6515379eac2f 136 g=0.0;
Susso 5:6515379eac2f 137 velocidad=0.0;// PWM ---> 100%
Susso 5:6515379eac2f 138 printf("PWM : 100 \r\n");
Susso 5:6515379eac2f 139 } else if(temperatura<=70)
Susso 5:6515379eac2f 140 sensor=lineal;
Susso 5:6515379eac2f 141 } else
Susso 5:6515379eac2f 142 sensor=abierto;
Susso 5:6515379eac2f 143 break;
Susso 5:6515379eac2f 144
Susso 5:6515379eac2f 145 case error:
Susso 5:6515379eac2f 146 if(salida1==1)
Susso 5:6515379eac2f 147 {
Susso 5:6515379eac2f 148 printf("Error \r\n");
Susso 5:6515379eac2f 149 if(temperatura>=0)
Susso 5:6515379eac2f 150 sensor=minimo;
Susso 5:6515379eac2f 151 }
Susso 5:6515379eac2f 152 else
Susso 5:6515379eac2f 153 sensor=abierto;
Susso 5:6515379eac2f 154 break;
Susso 5:6515379eac2f 155
Susso 5:6515379eac2f 156
Susso 5:6515379eac2f 157 }
Susso 5:6515379eac2f 158 }
Susso 5:6515379eac2f 159 /* cada vez que el efecto hall se activa cuento */
Susso 5:6515379eac2f 160
Susso 5:6515379eac2f 161 void pulsos()
Susso 5:6515379eac2f 162 {
Susso 5:6515379eac2f 163 if(salida2==1)
Susso 5:6515379eac2f 164 hall++;
Susso 5:6515379eac2f 165 }
Susso 5:6515379eac2f 166
Susso 5:6515379eac2f 167 //-----------funcion pulsador que invierte-------------------------------------------------------------------------
Susso 5:6515379eac2f 168 void pulsadorsito1()
Susso 5:6515379eac2f 169 {
Susso 5:6515379eac2f 170
Susso 5:6515379eac2f 171 switch(PIN1) {
Susso 5:6515379eac2f 172 default:
Susso 5:6515379eac2f 173 case 0:
Susso 5:6515379eac2f 174 if(pin1==1) {
Susso 5:6515379eac2f 175 cuenta1=0;
Susso 5:6515379eac2f 176 } else if(pin1==0) {
Susso 5:6515379eac2f 177 PIN1=1;
Susso 5:6515379eac2f 178 cuenta1=10;
Susso 5:6515379eac2f 179 }
Susso 5:6515379eac2f 180 break;
Susso 5:6515379eac2f 181
Susso 5:6515379eac2f 182 case 1:
Susso 5:6515379eac2f 183 if(cuenta1==0 && pin1==1) {
Susso 5:6515379eac2f 184 salida1=!salida1;
Susso 5:6515379eac2f 185 PIN1=0;
Susso 5:6515379eac2f 186 printf("Toque \r\n ");
Susso 5:6515379eac2f 187 } else if( pin1==1 && cuenta1>0) {
Susso 5:6515379eac2f 188 PIN1=0;
Susso 5:6515379eac2f 189 }
Sissors 0:e069f9f26768 190 break;
Sissors 0:e069f9f26768 191 }
Susso 5:6515379eac2f 192
Susso 5:6515379eac2f 193 }
Susso 5:6515379eac2f 194
Susso 5:6515379eac2f 195 void contador1()
Susso 5:6515379eac2f 196 {
Susso 5:6515379eac2f 197 if(cuenta1)
Susso 5:6515379eac2f 198 cuenta1--;
Sissors 0:e069f9f26768 199 }
Sissors 0:e069f9f26768 200
Susso 5:6515379eac2f 201 //--------------funcion de pulsador por pulsos-----------------------------------------------------------------
Susso 5:6515379eac2f 202
Susso 5:6515379eac2f 203 void pulsadorsito2()
Susso 5:6515379eac2f 204 {
Susso 5:6515379eac2f 205 switch(PIN2) {
Susso 5:6515379eac2f 206 default:
Susso 5:6515379eac2f 207 case 0:
Susso 5:6515379eac2f 208 if(pin2==1) {
Susso 5:6515379eac2f 209 salida2=0;
Susso 5:6515379eac2f 210 cuenta2=0;
Susso 5:6515379eac2f 211 } else if(pin2==0) {
Susso 5:6515379eac2f 212 PIN2=1;
Susso 5:6515379eac2f 213 cuenta2=10;
Susso 5:6515379eac2f 214 }
Susso 5:6515379eac2f 215 break;
Susso 5:6515379eac2f 216
Susso 5:6515379eac2f 217 case 1:
Susso 5:6515379eac2f 218 if(cuenta2==0 && pin2==0)
Susso 5:6515379eac2f 219 salida2=1;
Susso 5:6515379eac2f 220 else if(cuenta2==0 && pin2==1)
Susso 5:6515379eac2f 221 PIN2=0;
Susso 5:6515379eac2f 222 else if( pin2==1 && cuenta2>0)
Susso 5:6515379eac2f 223 PIN2=0;
Susso 5:6515379eac2f 224 break;
Sissors 0:e069f9f26768 225 }
Sissors 0:e069f9f26768 226 }
Sissors 0:e069f9f26768 227
Susso 5:6515379eac2f 228 void contador2()
Susso 5:6515379eac2f 229 {
Susso 5:6515379eac2f 230 if(cuenta2)
Susso 5:6515379eac2f 231 cuenta2--;
Susso 5:6515379eac2f 232 }