V

Dependencies:   mbed FatFileSystemCpp TextLCD

Committer:
bertonieto
Date:
Mon Aug 12 21:54:14 2019 +0000
Revision:
2:73cbcff90232
Parent:
1:93d8becc35f9
Export new

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oscarvzfz 0:376d7a150177 1 #include "mbed.h"
oscarvzfz 0:376d7a150177 2 #include "TextLCD.h"
bertonieto 1:93d8becc35f9 3 #include <string>
bertonieto 2:73cbcff90232 4 #include "MSCFileSystem.h"
oscarvzfz 0:376d7a150177 5
bertonieto 2:73cbcff90232 6 #define FSNAME "msc"
bertonieto 2:73cbcff90232 7
bertonieto 2:73cbcff90232 8 MSCFileSystem msc(FSNAME);
oscarvzfz 0:376d7a150177 9
bertonieto 2:73cbcff90232 10 LocalFileSystem local("local");
bertonieto 2:73cbcff90232 11
bertonieto 2:73cbcff90232 12 int saleBicho;
bertonieto 2:73cbcff90232 13 //definimos la bascula para despues obtener el peso
bertonieto 1:93d8becc35f9 14 Serial serialBascula(p9, p10); //Tx,Rx
bertonieto 2:73cbcff90232 15 Serial serialBt(p13, p14);
bertonieto 1:93d8becc35f9 16
bertonieto 2:73cbcff90232 17 Serial pc(USBTX, USBRX);
bertonieto 2:73cbcff90232 18
bertonieto 2:73cbcff90232 19 //Led que usamos para ver si leeemos
bertonieto 2:73cbcff90232 20 DigitalOut led3(LED3);
bertonieto 2:73cbcff90232 21
bertonieto 2:73cbcff90232 22 //Definimos los LED
bertonieto 2:73cbcff90232 23 DigitalOut ledAux1(p15);
bertonieto 2:73cbcff90232 24 DigitalOut ledAux2(p29);
bertonieto 2:73cbcff90232 25 DigitalOut ledAux3(p30);
bertonieto 2:73cbcff90232 26 DigitalOut ledManual(p16); //Led de estado manual por defecto apagado
bertonieto 2:73cbcff90232 27 DigitalOut ledEntrada(p17); //Led de puerta de entrada por defecto apagado
bertonieto 2:73cbcff90232 28 DigitalOut ledSalida(p18); //Led de puerta de salida
bertonieto 2:73cbcff90232 29 DigitalOut ledIzquierda(p19); //Led de puerta Izquierda
bertonieto 2:73cbcff90232 30 DigitalOut ledDerecha(p20); //Led de puerta Derecha
bertonieto 2:73cbcff90232 31
bertonieto 2:73cbcff90232 32 //Definimos las entradas
bertonieto 2:73cbcff90232 33 DigitalIn manual(p21); //Botón Manual
bertonieto 2:73cbcff90232 34 DigitalIn entrada(p5); //Botón Entrada
bertonieto 2:73cbcff90232 35 DigitalIn salida(p6); //Botón Salida
bertonieto 2:73cbcff90232 36 DigitalIn izquierda(p7); //Botón Izquierda
bertonieto 2:73cbcff90232 37 DigitalIn derecha(p8); //Botón Derecha
bertonieto 2:73cbcff90232 38 DigitalIn masMax(p22); //Botón Sumar Peso Max
bertonieto 2:73cbcff90232 39 DigitalIn menosMax(p23); //Botón Restar Peso Max
bertonieto 2:73cbcff90232 40 DigitalIn masMin(p24); //Botón Sumar Peso Min
bertonieto 2:73cbcff90232 41 DigitalIn menosMin(p25); //Botón Restar Peso Min
bertonieto 2:73cbcff90232 42
bertonieto 2:73cbcff90232 43 DigitalIn sensorPiston(p26); //Sensor Piston
bertonieto 2:73cbcff90232 44 DigitalIn presenciaEntrada(p11); //Sensor Entrada
bertonieto 2:73cbcff90232 45 DigitalIn btnBT(p12); //Se cambia por lo de ABAJO
bertonieto 2:73cbcff90232 46 //DigitalIn presenciaSalida(p12); //Sensor Salida --> se cambia a btnBT
bertonieto 2:73cbcff90232 47
bertonieto 2:73cbcff90232 48 int btnBTBorra=0;
bertonieto 2:73cbcff90232 49
bertonieto 2:73cbcff90232 50 //Escribimos los datos del archivo de los pesos en el MBED en el puerto serie para que pase por el BT
bertonieto 2:73cbcff90232 51 void esBT()
bertonieto 2:73cbcff90232 52 {
bertonieto 2:73cbcff90232 53 char buf[20480];
bertonieto 2:73cbcff90232 54 memset(buf,'\0',20480);
bertonieto 2:73cbcff90232 55
bertonieto 2:73cbcff90232 56 FILE *f = NULL;
bertonieto 2:73cbcff90232 57 f = fopen("/local/PESOS.txt", "r");
bertonieto 2:73cbcff90232 58
bertonieto 2:73cbcff90232 59 if(f==NULL) {
bertonieto 2:73cbcff90232 60 serialBt.printf("Problema con el BT\r\n");
bertonieto 2:73cbcff90232 61 }
bertonieto 2:73cbcff90232 62 size_t br;
bertonieto 2:73cbcff90232 63
bertonieto 2:73cbcff90232 64 while((br=fread(buf,1,20480,f))!=0) {
bertonieto 2:73cbcff90232 65 serialBt.printf(buf);
bertonieto 2:73cbcff90232 66 serialBt.printf("\r\n");
bertonieto 2:73cbcff90232 67 serialBt.printf("FIN PESOS------------\r\n");
bertonieto 2:73cbcff90232 68 }
bertonieto 2:73cbcff90232 69 fclose(f);
bertonieto 2:73cbcff90232 70
bertonieto 2:73cbcff90232 71 wait(1);
bertonieto 2:73cbcff90232 72
bertonieto 2:73cbcff90232 73 memset(buf,'\0',20480);
bertonieto 2:73cbcff90232 74
bertonieto 2:73cbcff90232 75 f = fopen("/local/TOTAL.txt", "r");
bertonieto 2:73cbcff90232 76
bertonieto 2:73cbcff90232 77 if(f==NULL) {
bertonieto 2:73cbcff90232 78 serialBt.printf("Problema con el BT\r\n");
bertonieto 2:73cbcff90232 79 }
bertonieto 2:73cbcff90232 80
bertonieto 2:73cbcff90232 81 while((br=fread(buf,1,20480,f))!=0) {
bertonieto 2:73cbcff90232 82 serialBt.printf("TOTAL: ");
bertonieto 2:73cbcff90232 83 serialBt.printf(buf);
bertonieto 2:73cbcff90232 84 serialBt.printf("\r\n");
bertonieto 2:73cbcff90232 85
bertonieto 2:73cbcff90232 86 }
bertonieto 2:73cbcff90232 87 fclose(f);
bertonieto 2:73cbcff90232 88
bertonieto 2:73cbcff90232 89 wait(1);
bertonieto 2:73cbcff90232 90
bertonieto 2:73cbcff90232 91 memset(buf,'\0',20480);
bertonieto 2:73cbcff90232 92
bertonieto 2:73cbcff90232 93 f = fopen("/local/PINTA.txt", "r");
bertonieto 2:73cbcff90232 94
bertonieto 2:73cbcff90232 95 if(f==NULL) {
bertonieto 2:73cbcff90232 96 serialBt.printf("Problema con el BT\r\n");
bertonieto 2:73cbcff90232 97 }
bertonieto 2:73cbcff90232 98
bertonieto 2:73cbcff90232 99 while((br=fread(buf,1,20480,f))!=0) {
bertonieto 2:73cbcff90232 100 serialBt.printf("PINTADOS: ");
bertonieto 2:73cbcff90232 101 serialBt.printf(buf);
bertonieto 2:73cbcff90232 102 serialBt.printf("\r\n");
bertonieto 2:73cbcff90232 103
bertonieto 2:73cbcff90232 104 }
bertonieto 2:73cbcff90232 105 fclose(f);
bertonieto 2:73cbcff90232 106
bertonieto 2:73cbcff90232 107 wait(1);
bertonieto 2:73cbcff90232 108
bertonieto 2:73cbcff90232 109
bertonieto 2:73cbcff90232 110 memset(buf,'\0',20480);
bertonieto 2:73cbcff90232 111
bertonieto 2:73cbcff90232 112 f = fopen("/local/CLASIF.txt", "r");
bertonieto 2:73cbcff90232 113
bertonieto 2:73cbcff90232 114 if(f==NULL) {
bertonieto 2:73cbcff90232 115 serialBt.printf("Problema con el BT\r\n");
bertonieto 2:73cbcff90232 116 }
bertonieto 2:73cbcff90232 117
bertonieto 2:73cbcff90232 118 while((br=fread(buf,1,20480,f))!=0) {
bertonieto 2:73cbcff90232 119 serialBt.printf("CLASIFICADOS: ");
bertonieto 2:73cbcff90232 120 serialBt.printf(buf);
bertonieto 2:73cbcff90232 121 serialBt.printf("\r\n");
bertonieto 2:73cbcff90232 122
bertonieto 2:73cbcff90232 123 }
bertonieto 2:73cbcff90232 124 fclose(f);
bertonieto 2:73cbcff90232 125 wait(1);
bertonieto 2:73cbcff90232 126 }
bertonieto 2:73cbcff90232 127 //escribir los pesos en el archivo correspondiente PESOS.txt --------------------------------------------------------
bertonieto 2:73cbcff90232 128 void escribirPesos(int i)
bertonieto 2:73cbcff90232 129 {
bertonieto 2:73cbcff90232 130 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 131 FILE *fp = fopen("/local/PESOS.txt", "a"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 132 fprintf(fp, "%d \r\n", i);
bertonieto 2:73cbcff90232 133 fclose(fp);
bertonieto 2:73cbcff90232 134 }
bertonieto 2:73cbcff90232 135
bertonieto 2:73cbcff90232 136 void borrarPesos()
bertonieto 2:73cbcff90232 137 {
bertonieto 2:73cbcff90232 138 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 139 FILE *fp = fopen("/local/PESOS.txt", "w"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 140 fprintf(fp, "");
bertonieto 2:73cbcff90232 141 fclose(fp);
bertonieto 2:73cbcff90232 142 }
bertonieto 2:73cbcff90232 143
bertonieto 2:73cbcff90232 144 int leeTotal()
bertonieto 2:73cbcff90232 145 {
bertonieto 2:73cbcff90232 146 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 147 FILE *fp = fopen("/local/TOTAL.txt", "r"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 148 char buffer [10];
bertonieto 2:73cbcff90232 149 int total;
bertonieto 2:73cbcff90232 150 fgets(buffer,10,fp);
bertonieto 2:73cbcff90232 151 fclose(fp);
bertonieto 2:73cbcff90232 152 total=atoi(buffer);
bertonieto 2:73cbcff90232 153 return total;
bertonieto 2:73cbcff90232 154 }
bertonieto 2:73cbcff90232 155
bertonieto 2:73cbcff90232 156
bertonieto 2:73cbcff90232 157 void escribeTotal(int i)
bertonieto 2:73cbcff90232 158 {
bertonieto 2:73cbcff90232 159 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 160 FILE *fp = fopen("/local/TOTAL.txt", "w"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 161 char buffer [10];
bertonieto 2:73cbcff90232 162 sprintf (buffer, "%d", i);
bertonieto 2:73cbcff90232 163 fprintf(fp, buffer);
bertonieto 2:73cbcff90232 164 fclose(fp);
bertonieto 2:73cbcff90232 165 }
bertonieto 2:73cbcff90232 166
bertonieto 2:73cbcff90232 167 int leeClasificados()
bertonieto 2:73cbcff90232 168 {
bertonieto 2:73cbcff90232 169 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 170 FILE *fp = fopen("/local/CLASIF.txt", "r"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 171 char buffer [10];
bertonieto 2:73cbcff90232 172 int clasificados;
bertonieto 2:73cbcff90232 173 fgets(buffer,10,fp);
bertonieto 2:73cbcff90232 174 fclose(fp);
bertonieto 2:73cbcff90232 175 clasificados=atoi(buffer);
bertonieto 2:73cbcff90232 176 return clasificados;
bertonieto 2:73cbcff90232 177 }
bertonieto 2:73cbcff90232 178
bertonieto 2:73cbcff90232 179
bertonieto 2:73cbcff90232 180 void escribeClasificados(int i)
bertonieto 2:73cbcff90232 181 {
bertonieto 2:73cbcff90232 182 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 183 FILE *fp = fopen("/local/CLASIF.txt", "w"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 184 char buffer [10];
bertonieto 2:73cbcff90232 185 sprintf (buffer, "%d", i);
bertonieto 2:73cbcff90232 186 fprintf(fp, buffer);
bertonieto 2:73cbcff90232 187 fclose(fp);
bertonieto 2:73cbcff90232 188 }
bertonieto 2:73cbcff90232 189
bertonieto 2:73cbcff90232 190 int leePintados()
bertonieto 2:73cbcff90232 191 {
bertonieto 2:73cbcff90232 192 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 193 FILE *fp = fopen("/local/PINTA.txt", "r"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 194 char buffer [10];
bertonieto 2:73cbcff90232 195 int pintados;
bertonieto 2:73cbcff90232 196 fgets(buffer,10,fp);
bertonieto 2:73cbcff90232 197 fclose(fp);
bertonieto 2:73cbcff90232 198 pintados=atoi(buffer);
bertonieto 2:73cbcff90232 199 return pintados;
bertonieto 2:73cbcff90232 200 }
bertonieto 2:73cbcff90232 201
bertonieto 2:73cbcff90232 202
bertonieto 2:73cbcff90232 203 void escribePintados(int i)
bertonieto 2:73cbcff90232 204 {
bertonieto 2:73cbcff90232 205 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 206 FILE *fp = fopen("/local/PINTA.txt", "w"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 207 char buffer [10];
bertonieto 2:73cbcff90232 208 sprintf (buffer, "%d", i);
bertonieto 2:73cbcff90232 209 fprintf(fp, buffer);
bertonieto 2:73cbcff90232 210 fclose(fp);
bertonieto 2:73cbcff90232 211 }
bertonieto 2:73cbcff90232 212
bertonieto 2:73cbcff90232 213 //Escribimos en el USB
bertonieto 2:73cbcff90232 214 void escribirUSB(int peso)
bertonieto 2:73cbcff90232 215 {
bertonieto 2:73cbcff90232 216 FILE *fp = fopen( "/" FSNAME "/basculapesos.txt", "a");
bertonieto 2:73cbcff90232 217 fprintf(fp, "%d \r\n", peso);
bertonieto 2:73cbcff90232 218 fclose(fp);
bertonieto 2:73cbcff90232 219 }
bertonieto 2:73cbcff90232 220
bertonieto 2:73cbcff90232 221 //Iniciamos los led auxiliares.
bertonieto 2:73cbcff90232 222 void iniciaAux()
bertonieto 2:73cbcff90232 223 {
bertonieto 2:73cbcff90232 224 ledAux1 = 0;
bertonieto 2:73cbcff90232 225 ledAux2 = 0;
bertonieto 2:73cbcff90232 226 ledAux3 = 0;
bertonieto 2:73cbcff90232 227 }
bertonieto 2:73cbcff90232 228 //leemos de memoria flash la variable del peso minimo de clasificacion.
bertonieto 2:73cbcff90232 229 int leeMinMem()
bertonieto 2:73cbcff90232 230 {
bertonieto 2:73cbcff90232 231 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 232 FILE *fp = fopen("/local/MIN", "r"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 233 char buffer [10];
bertonieto 2:73cbcff90232 234 int minim;
bertonieto 2:73cbcff90232 235 fgets(buffer,10,fp);
bertonieto 2:73cbcff90232 236 fclose(fp);
bertonieto 2:73cbcff90232 237 minim=atoi(buffer);
bertonieto 2:73cbcff90232 238 return minim;
bertonieto 2:73cbcff90232 239 }
bertonieto 2:73cbcff90232 240
bertonieto 2:73cbcff90232 241 //leemos de memoria flash la variable del peso maximo de clasificacion.
bertonieto 2:73cbcff90232 242 int leeMaxMem()
bertonieto 2:73cbcff90232 243 {
bertonieto 2:73cbcff90232 244 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 245 FILE *fp1 = fopen("/local/MAX", "r"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 246 char buffer1 [10];
bertonieto 2:73cbcff90232 247 int max;
bertonieto 2:73cbcff90232 248 fgets(buffer1,10,fp1);
bertonieto 2:73cbcff90232 249 fclose(fp1);
bertonieto 2:73cbcff90232 250 max=atoi(buffer1);
bertonieto 2:73cbcff90232 251 return max;
bertonieto 2:73cbcff90232 252 }
bertonieto 2:73cbcff90232 253
bertonieto 2:73cbcff90232 254 //Iniciamos pesos Max y Min
bertonieto 2:73cbcff90232 255 int pesoMax = leeMaxMem();
bertonieto 2:73cbcff90232 256 int pesoMin = leeMinMem();
bertonieto 2:73cbcff90232 257
bertonieto 2:73cbcff90232 258 //Iniciamos total de pesos
bertonieto 2:73cbcff90232 259 int total=leeTotal();
bertonieto 2:73cbcff90232 260 int clasificados=leeClasificados();
bertonieto 2:73cbcff90232 261 int pintados=leePintados();
bertonieto 2:73cbcff90232 262
bertonieto 2:73cbcff90232 263
bertonieto 2:73cbcff90232 264 //Iniciamos I2C Communication
bertonieto 2:73cbcff90232 265 I2C i2c_lcd(p28,p27); // SDA, SCL
bertonieto 2:73cbcff90232 266 TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD16x2, TextLCD::HD44780); // I2C bus, PCF8574 Slaveaddress, LCD Type, Device Type
oscarvzfz 0:376d7a150177 267
bertonieto 1:93d8becc35f9 268 //Iniciamos el LCD.
bertonieto 2:73cbcff90232 269 void iniciaLCD()
bertonieto 2:73cbcff90232 270 {
bertonieto 1:93d8becc35f9 271 lcd.setMode(TextLCD::DispOn); //DispOff, DispOn
bertonieto 1:93d8becc35f9 272 lcd.setBacklight(TextLCD::LightOff);//LightOff, LightOn
bertonieto 1:93d8becc35f9 273 lcd.setCursor(TextLCD::CurOff_BlkOff);//CurOff_BlkOff, CurOn_BlkOff, CurOff_BlkOn, CurOn_BlkOn
bertonieto 2:73cbcff90232 274 }
bertonieto 2:73cbcff90232 275
bertonieto 2:73cbcff90232 276 //Pintamos en el LCD linea 1.
bertonieto 2:73cbcff90232 277 void pintaLCD1(bool manual){
bertonieto 2:73cbcff90232 278 if (manual){
bertonieto 2:73cbcff90232 279 lcd.locate(0,0);
bertonieto 2:73cbcff90232 280 lcd.printf(" MANUAL ");
bertonieto 2:73cbcff90232 281
bertonieto 2:73cbcff90232 282 }
bertonieto 2:73cbcff90232 283 else{
bertonieto 2:73cbcff90232 284 lcd.locate(0,0);
bertonieto 2:73cbcff90232 285 lcd.printf("Salida1 Salida2");
bertonieto 2:73cbcff90232 286 }
bertonieto 1:93d8becc35f9 287 }
bertonieto 2:73cbcff90232 288
bertonieto 2:73cbcff90232 289 //Pintamos en el LCD linea 2.
bertonieto 2:73cbcff90232 290 void pintaLCD2(){
bertonieto 2:73cbcff90232 291 lcd.locate(0,1);
bertonieto 2:73cbcff90232 292 lcd.printf("<%dKg. ", pesoMin);
bertonieto 2:73cbcff90232 293 lcd.locate(7,1);
bertonieto 2:73cbcff90232 294 lcd.printf(" >%dKg.", pesoMax);
bertonieto 2:73cbcff90232 295 }
oscarvzfz 0:376d7a150177 296
bertonieto 1:93d8becc35f9 297 int leerPeso(){
bertonieto 2:73cbcff90232 298 char buffer1[30];
bertonieto 2:73cbcff90232 299 string pesoss ="";
bertonieto 2:73cbcff90232 300 int ipesoss =0;
bertonieto 2:73cbcff90232 301 serialBascula.printf("2005002D:<CR><LF>\n"); //\r\n 2005002D:<CR><LF>\n
bertonieto 2:73cbcff90232 302
bertonieto 2:73cbcff90232 303 pesoss = serialBascula.gets(buffer1,30);
bertonieto 2:73cbcff90232 304 if (buffer1[0]=='9'){
bertonieto 2:73cbcff90232 305 pesoss = pesoss.substr(11,3);
bertonieto 2:73cbcff90232 306 ipesoss = atoi(pesoss.c_str());
bertonieto 2:73cbcff90232 307 }
bertonieto 2:73cbcff90232 308
bertonieto 2:73cbcff90232 309 return ipesoss;
bertonieto 2:73cbcff90232 310 }
bertonieto 2:73cbcff90232 311
bertonieto 2:73cbcff90232 312 void escribeMaxMem(int i)
bertonieto 2:73cbcff90232 313 {
bertonieto 2:73cbcff90232 314 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 315 FILE *fp = fopen("/local/max", "w"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 316 char buffer [10];
bertonieto 2:73cbcff90232 317 sprintf (buffer, "%d", i);
bertonieto 2:73cbcff90232 318 fprintf(fp, buffer);
bertonieto 2:73cbcff90232 319 fclose(fp);
bertonieto 1:93d8becc35f9 320 }
bertonieto 1:93d8becc35f9 321
bertonieto 2:73cbcff90232 322 void escribeMinMem(int i)
bertonieto 2:73cbcff90232 323 {
bertonieto 2:73cbcff90232 324 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
bertonieto 2:73cbcff90232 325 FILE *fp = fopen("/local/min", "w"); // Open "out.txt" on the local file system for writing
bertonieto 2:73cbcff90232 326 char buffer [10];
bertonieto 2:73cbcff90232 327 sprintf (buffer, "%d", i);
bertonieto 2:73cbcff90232 328 fprintf(fp, buffer);
bertonieto 2:73cbcff90232 329 fclose(fp);
bertonieto 2:73cbcff90232 330 }
bertonieto 2:73cbcff90232 331 void BtBorraArchivos()
bertonieto 2:73cbcff90232 332 {
bertonieto 2:73cbcff90232 333 //TODO Borramos todos los archivos para que queden inicializados.
bertonieto 2:73cbcff90232 334 pc.printf("\r\nBORRAMOS TODOS ARCHIVOS\r\n");
bertonieto 2:73cbcff90232 335 escribeTotal(0);
bertonieto 2:73cbcff90232 336 escribePintados(0);
bertonieto 2:73cbcff90232 337 escribeClasificados(0);
bertonieto 2:73cbcff90232 338 borrarPesos();
bertonieto 2:73cbcff90232 339 }
bertonieto 2:73cbcff90232 340
bertonieto 2:73cbcff90232 341 void cambioManual()
bertonieto 2:73cbcff90232 342 {
bertonieto 2:73cbcff90232 343 if (ledManual == 1){
bertonieto 2:73cbcff90232 344 //reescribimos el dato en MAX y MIN al volver de manual por si se ha cambiado
bertonieto 2:73cbcff90232 345 escribeMaxMem(pesoMax);
bertonieto 2:73cbcff90232 346 escribeMinMem(pesoMin);
bertonieto 2:73cbcff90232 347 ledManual = 0;
bertonieto 2:73cbcff90232 348 if (btnBTBorra == 1)
bertonieto 2:73cbcff90232 349 {
bertonieto 2:73cbcff90232 350 BtBorraArchivos(); //borramos los archivos de pesos que hemos rellenado
bertonieto 2:73cbcff90232 351 btnBTBorra = 0;
bertonieto 2:73cbcff90232 352 }
bertonieto 2:73cbcff90232 353 pintaLCD1(0);
bertonieto 2:73cbcff90232 354 wait(0.50);
bertonieto 2:73cbcff90232 355 }
bertonieto 2:73cbcff90232 356 else{
bertonieto 2:73cbcff90232 357 ledManual = 1;
bertonieto 2:73cbcff90232 358 pintaLCD1(1);
bertonieto 2:73cbcff90232 359 wait(0.50);
bertonieto 2:73cbcff90232 360 }
bertonieto 2:73cbcff90232 361 }
bertonieto 2:73cbcff90232 362
bertonieto 2:73cbcff90232 363
bertonieto 1:93d8becc35f9 364 //Main, aqui ponemos todo el programa.
bertonieto 2:73cbcff90232 365 int main()
bertonieto 2:73cbcff90232 366 {
bertonieto 2:73cbcff90232 367
bertonieto 2:73cbcff90232 368 pc.printf("INICIANDO...");
bertonieto 2:73cbcff90232 369
bertonieto 2:73cbcff90232 370 wait(1);
bertonieto 2:73cbcff90232 371
bertonieto 1:93d8becc35f9 372 iniciaLCD(); //Iniciamos el LCD
bertonieto 2:73cbcff90232 373 pintaLCD1(0);
bertonieto 2:73cbcff90232 374 pintaLCD2();
bertonieto 1:93d8becc35f9 375 int pesoreal;
bertonieto 2:73cbcff90232 376
bertonieto 2:73cbcff90232 377 //int estaPesado; //si esta pesado 1, si no esta pesado 0
bertonieto 1:93d8becc35f9 378
bertonieto 2:73cbcff90232 379 while(1) {
bertonieto 2:73cbcff90232 380 //Modo manual: se pueden cambiar pesos y mover las puertas
bertonieto 2:73cbcff90232 381 while(ledManual == 1)
bertonieto 2:73cbcff90232 382 {
bertonieto 2:73cbcff90232 383 //Compruebo el BT
bertonieto 2:73cbcff90232 384 if(btnBT ==1){
bertonieto 2:73cbcff90232 385 esBT();
bertonieto 2:73cbcff90232 386 btnBTBorra=1; //bool para borrar si vuelve a manual.
bertonieto 2:73cbcff90232 387 wait(1);
bertonieto 2:73cbcff90232 388 }
bertonieto 2:73cbcff90232 389
bertonieto 2:73cbcff90232 390 //Fin de Compruebo el BT
bertonieto 2:73cbcff90232 391 if (manual == 1)
bertonieto 2:73cbcff90232 392 {
bertonieto 2:73cbcff90232 393 cambioManual();}
bertonieto 2:73cbcff90232 394 if (entrada == 1)
bertonieto 2:73cbcff90232 395 {
bertonieto 2:73cbcff90232 396 if(ledEntrada == 1)
bertonieto 2:73cbcff90232 397 {
bertonieto 2:73cbcff90232 398 ledEntrada = 0;
bertonieto 2:73cbcff90232 399 wait(0.50);
bertonieto 2:73cbcff90232 400 }
bertonieto 2:73cbcff90232 401 else
bertonieto 2:73cbcff90232 402 {
bertonieto 2:73cbcff90232 403 ledEntrada = 1;
bertonieto 2:73cbcff90232 404 wait(0.50);
bertonieto 2:73cbcff90232 405 }
bertonieto 2:73cbcff90232 406 }
bertonieto 2:73cbcff90232 407 if (salida == 1)
bertonieto 2:73cbcff90232 408 {
bertonieto 2:73cbcff90232 409 if(ledSalida == 1)
bertonieto 2:73cbcff90232 410 {
bertonieto 2:73cbcff90232 411 ledSalida = 0;
bertonieto 2:73cbcff90232 412 wait(0.50);
bertonieto 2:73cbcff90232 413 }
bertonieto 2:73cbcff90232 414 else
bertonieto 2:73cbcff90232 415 {
bertonieto 2:73cbcff90232 416 ledSalida = 1;
bertonieto 2:73cbcff90232 417 wait(0.50);
bertonieto 2:73cbcff90232 418 }
bertonieto 2:73cbcff90232 419 }
bertonieto 2:73cbcff90232 420 if (izquierda == 1)
bertonieto 2:73cbcff90232 421 {
bertonieto 2:73cbcff90232 422 ledIzquierda = 1;
bertonieto 2:73cbcff90232 423 wait(0.50);
bertonieto 2:73cbcff90232 424 ledIzquierda = 0;
bertonieto 2:73cbcff90232 425 }
bertonieto 2:73cbcff90232 426 if (derecha == 1)
bertonieto 2:73cbcff90232 427 {
bertonieto 2:73cbcff90232 428 if(ledDerecha == 1)
bertonieto 2:73cbcff90232 429 {
bertonieto 2:73cbcff90232 430 ledDerecha = 0;
bertonieto 2:73cbcff90232 431 wait(0.50);
bertonieto 2:73cbcff90232 432 }
bertonieto 2:73cbcff90232 433 else
bertonieto 2:73cbcff90232 434 {
bertonieto 2:73cbcff90232 435 ledDerecha = 1;
bertonieto 2:73cbcff90232 436 wait(0.50);
bertonieto 2:73cbcff90232 437 }
bertonieto 2:73cbcff90232 438 }
bertonieto 2:73cbcff90232 439 if (masMax == 1)
bertonieto 2:73cbcff90232 440 {
bertonieto 2:73cbcff90232 441 if (pesoMax < 2000)
bertonieto 2:73cbcff90232 442 {
bertonieto 2:73cbcff90232 443 pesoMax += 1;
bertonieto 2:73cbcff90232 444 wait(0.10);
bertonieto 2:73cbcff90232 445 pintaLCD2();
bertonieto 2:73cbcff90232 446 }
bertonieto 2:73cbcff90232 447 }
bertonieto 2:73cbcff90232 448 if (menosMax == 1)
bertonieto 2:73cbcff90232 449 {
bertonieto 2:73cbcff90232 450 if (pesoMax > (pesoMin + 1))
bertonieto 2:73cbcff90232 451 {
bertonieto 2:73cbcff90232 452 pesoMax -= 1;
bertonieto 2:73cbcff90232 453 wait(0.10);
bertonieto 2:73cbcff90232 454 pintaLCD2();
bertonieto 2:73cbcff90232 455 }
bertonieto 2:73cbcff90232 456 }
bertonieto 2:73cbcff90232 457 if (masMin == 1)
bertonieto 2:73cbcff90232 458 {
bertonieto 2:73cbcff90232 459 if(pesoMin < (pesoMax - 1))
bertonieto 2:73cbcff90232 460 {
bertonieto 2:73cbcff90232 461 pesoMin += 1;
bertonieto 2:73cbcff90232 462 wait(0.10);
bertonieto 2:73cbcff90232 463 pintaLCD2();
bertonieto 2:73cbcff90232 464 }
bertonieto 2:73cbcff90232 465 }
bertonieto 2:73cbcff90232 466 if (menosMin == 1)
bertonieto 2:73cbcff90232 467 {if(pesoMin > 10)
bertonieto 2:73cbcff90232 468 {
bertonieto 2:73cbcff90232 469 pesoMin -= 1;
bertonieto 2:73cbcff90232 470 wait(0.10);
bertonieto 2:73cbcff90232 471 pintaLCD2();
bertonieto 2:73cbcff90232 472 }
bertonieto 2:73cbcff90232 473 }
bertonieto 1:93d8becc35f9 474 }
bertonieto 2:73cbcff90232 475 //empezamos el programa
bertonieto 2:73cbcff90232 476 ledEntrada = 1;
bertonieto 2:73cbcff90232 477 ledSalida = 0;
bertonieto 2:73cbcff90232 478 ledDerecha = 0;
bertonieto 2:73cbcff90232 479 ledIzquierda = 0;
bertonieto 2:73cbcff90232 480 pesoreal = 0;
bertonieto 2:73cbcff90232 481 saleBicho = 0;
bertonieto 2:73cbcff90232 482 if (manual == 1){
bertonieto 2:73cbcff90232 483 cambioManual();
bertonieto 2:73cbcff90232 484 }
bertonieto 2:73cbcff90232 485
bertonieto 2:73cbcff90232 486 //Bucle de entrada a la báscula y funcionamiento real
bertonieto 2:73cbcff90232 487 wait(0.2);
bertonieto 2:73cbcff90232 488 int tiempoSinSensorPiston = 0;
bertonieto 2:73cbcff90232 489 while(presenciaEntrada == 1 && ledManual == 0){
bertonieto 2:73cbcff90232 490 pc.printf("UNO");
bertonieto 2:73cbcff90232 491 ledEntrada = 0;
bertonieto 2:73cbcff90232 492 //Se Comprueba que el bicho no ha salido para no mover la puerta de salida
bertonieto 2:73cbcff90232 493 if (saleBicho == 0){
bertonieto 2:73cbcff90232 494 ledSalida = 0;
bertonieto 2:73cbcff90232 495 }
bertonieto 2:73cbcff90232 496 //Se comprueba que no se cambia a manual
bertonieto 2:73cbcff90232 497 if (manual == 1){cambioManual();}
bertonieto 2:73cbcff90232 498 wait(1);
bertonieto 2:73cbcff90232 499 if(sensorPiston == 1){
bertonieto 2:73cbcff90232 500 pc.printf("DOS");
bertonieto 2:73cbcff90232 501 //Tiempo a esperar para que la bascula calcule el liveWeitgh
bertonieto 2:73cbcff90232 502 int tiempoLectura = 0;
bertonieto 2:73cbcff90232 503 //Bucle de espera para obtener el peso (3,5 s)
bertonieto 2:73cbcff90232 504 while(tiempoLectura < 40 && ledManual == 0 && presenciaEntrada == 1){
bertonieto 2:73cbcff90232 505 wait(0.1);
bertonieto 2:73cbcff90232 506 if (manual == 1){cambioManual();}
bertonieto 2:73cbcff90232 507 tiempoLectura++;
bertonieto 2:73cbcff90232 508 }
bertonieto 2:73cbcff90232 509 if (saleBicho == 0){
bertonieto 2:73cbcff90232 510 pc.printf("DOS Y MEDIO");
bertonieto 2:73cbcff90232 511 pesoreal = leerPeso();
bertonieto 2:73cbcff90232 512 //estaPesado = 1;
bertonieto 2:73cbcff90232 513 //AQUI GUARDO LOS BICHOS EN EL FICHERO PESOS -------------------------------------------------> guardo en el fichero
bertonieto 2:73cbcff90232 514 pc.printf("TRES");
bertonieto 2:73cbcff90232 515 wait(0.8);
bertonieto 2:73cbcff90232 516
bertonieto 2:73cbcff90232 517 //} CAMBIO EN ESTE FINAL DEL IF despues de la comprobacion de abajo, para que solo cambie puertas una vez.
bertonieto 2:73cbcff90232 518 //Comprobamos el pero obtenido para clasificar o salir
bertonieto 2:73cbcff90232 519 if (pesoreal < 50000 && ledManual == 0 && presenciaEntrada == 1){
bertonieto 2:73cbcff90232 520 pc.printf("CUATRo");
bertonieto 2:73cbcff90232 521
bertonieto 2:73cbcff90232 522 pc.printf("PESO: %d \r\n",pesoreal);
bertonieto 2:73cbcff90232 523 if (pesoreal > pesoMin && saleBicho == 0){
bertonieto 2:73cbcff90232 524 ledDerecha = 1;
bertonieto 2:73cbcff90232 525 clasificados += 1;
bertonieto 2:73cbcff90232 526 escribeClasificados(clasificados);
bertonieto 2:73cbcff90232 527 //escribimos el peso real de animales clasificados en el USB
bertonieto 2:73cbcff90232 528 //escribirUSB(pesoreal);
bertonieto 2:73cbcff90232 529 if (pesoreal < pesoMax){
bertonieto 2:73cbcff90232 530 ledIzquierda = 1;
bertonieto 2:73cbcff90232 531 wait(0.5);
bertonieto 2:73cbcff90232 532 ledIzquierda = 0;
bertonieto 2:73cbcff90232 533 pintados += 1;
bertonieto 2:73cbcff90232 534 escribePintados(pintados);
bertonieto 2:73cbcff90232 535 }
bertonieto 2:73cbcff90232 536 wait(0.50);
bertonieto 2:73cbcff90232 537 }
bertonieto 2:73cbcff90232 538 }//cierro el if de salebicho == 0
bertonieto 2:73cbcff90232 539 wait(0.1);
bertonieto 2:73cbcff90232 540 if(ledManual == 0){
bertonieto 2:73cbcff90232 541 ledSalida = 1;
bertonieto 2:73cbcff90232 542 }
bertonieto 2:73cbcff90232 543 saleBicho = 1;
bertonieto 2:73cbcff90232 544 total += 1;
bertonieto 2:73cbcff90232 545 escribeTotal(total);
bertonieto 2:73cbcff90232 546 escribirPesos(pesoreal);
bertonieto 2:73cbcff90232 547
bertonieto 2:73cbcff90232 548 //escribimos todos los pesos de los animales que pasan en el USB
bertonieto 2:73cbcff90232 549 //escribirUSB(pesoreal);
bertonieto 2:73cbcff90232 550 }
bertonieto 2:73cbcff90232 551 }
bertonieto 2:73cbcff90232 552 //Else del if del sensor del piston
bertonieto 2:73cbcff90232 553 else{
bertonieto 2:73cbcff90232 554 tiempoSinSensorPiston = tiempoSinSensorPiston +1;
bertonieto 2:73cbcff90232 555 wait(0.5);
bertonieto 2:73cbcff90232 556 if (tiempoSinSensorPiston > 10){
bertonieto 2:73cbcff90232 557 ledEntrada = 1;
bertonieto 2:73cbcff90232 558 wait(2);
bertonieto 2:73cbcff90232 559 }
bertonieto 2:73cbcff90232 560 }
bertonieto 2:73cbcff90232 561 }
bertonieto 2:73cbcff90232 562 }
bertonieto 2:73cbcff90232 563 }