RUBEN ELISEO VAZQUEZ / Mbed 2 deprecated ds1302-bluetooth

Dependencies:   FATFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #define SCLK    p13
00002 #define IO      p12
00003 #define CE      p11
00004 
00005 #define INITIAL_RUN
00006 
00007 #include "mbed.h"
00008 #include <string.h>
00009 #include "rtos.h"
00010 #include "DS1302.h"
00011 #include "ACS712.h""
00012 
00013 
00014 DigitalOut led(LED1);
00015 DigitalIn button(p21);
00016 DigitalIn mold1(p22);
00017 DigitalIn mold2(p23);
00018 DigitalIn seguridad(p24);
00019 DigitalIn operacion(p25);
00020 DigitalIn molder(p26);
00021 DigitalIn molder1(p27);
00022 int molde;
00023 int molde2;
00024 int cont;
00025 int conta;
00026 int piezas;
00027 int pieza;
00028 int contador=0;
00029 int espera;
00030 int moldeacti=0;
00031 int moldeactir=0;
00032 float bajos=0;
00033 float altos=0;
00034 int tred;
00035 float descanso;
00036 float retardo=0;
00037 float gf;
00038 float promedio;
00039 float promaltos;
00040 float prombajos;
00041 float trabajo;
00042 char LED[100];
00043 char Str[100];
00044  // ACS712 dev(p19);
00045 Serial pc(USBTX, USBRX);
00046 Serial blue(p9, p10);
00047 DS1302 clk(SCLK, IO, CE);
00048 Mutex stdio_mutex; 
00049  
00050 
00051  /*void mold_thread(void const *argument){
00052      while(1){
00053      if (mold1==1)
00054         {blue.printf("MOLDE DE 2 PIEZAS\n\r");wait(0.5);}
00055         }
00056     }
00057 
00058 void moldt_thread(void const *argument){
00059      while(1){
00060      if (mold2==1)
00061         {blue.printf("MOLDE DE 7 PIEZAS\n\r");wait(0.5);}
00062         }
00063     }*/
00064 
00065 void segur_thread(void const *argument){
00066      while(1){
00067          time_t seconds = clk.time(NULL);
00068      if (seguridad==1)
00069         {blue.printf("SEGURIDAD VIOLADA ");wait(0.5);blue.printf("EN TIEMPO Y FECHA:%s\r", ctime(&seconds));} 
00070         }   
00071     }  
00072 
00073 /*void pro_thread(void const *argument){
00074      while(1){
00075      if (operacion==1)
00076         //{blue.printf("\rPROMEDIO DESCANSO=%f",promedio); blue.printf(" MINUTOS\n");wait(0.5);
00077         {blue.printf("\rPROMEDIO DE TIEMPOS ALTOS= %f",promaltos);blue.printf(" SEGUNDOS\n");wait(0.5);
00078         blue.printf("\rPROMEDIO DE TIEMPOS BAJOS= %f",prombajos);blue.printf(" SEGUNDOS\n\r");wait(0.5);}
00079         }   
00080     } */ 
00081     
00082 void modacti_thread(void const *argument){
00083      while(1){
00084 if (molder==1)
00085     {molde=1;pc.printf("CONT MOLD1=%d\n",moldeacti);}else{molde=0;}
00086     
00087     if (molde==1)
00088     {moldeacti++;wait(25);}
00089     }
00090 }
00091 void modacti1_thread(void const *argument){
00092      while(1){
00093 if (molder1==1)
00094     {molde2=1;pc.printf("CONT MOLD2=%d\n",moldeactir);}else{molde2=0;}
00095     
00096     if (molde2==1)
00097     {moldeactir++;wait(25);}
00098     }
00099 }
00100 
00101 void prome_thread(void const *argument){
00102      while(1){
00103           if (tred==1)
00104            {altos++; wait(1); pc.printf("altos= %f\n",altos);}
00105     
00106          if (tred==0 && contador>=1&& espera<=24)
00107          {bajos++;wait(1); pc.printf("bajos= %f\n",bajos);}
00108 
00109     }
00110 }
00111 
00112 void trans_thread(void const *argument){
00113  while(1)
00114      {
00115          if (blue.readable())
00116          {
00117              blue.scanf("%s",&Str);
00118              pc.printf("%s\n\r",Str);
00119             strcpy(LED,   strtok(Str , ","));
00120             
00121              
00122              if(strcmp(LED,"ACEPTAR")==0){led=1;
00123             blue.printf("\rPROMEDIO DE TIEMPOS ALTOS= %f",promaltos);blue.printf(" SEGUNDOS\n");wait(0.5);
00124         blue.printf("\rPROMEDIO DE TIEMPOS BAJOS= %f",prombajos);blue.printf(" SEGUNDOS\n\r");wait(0.5);
00125         blue.printf("\rEL MOTOR SE ACTIVO= %d\n,",contador);blue.printf(" VECES\r\n");
00126         blue.printf("\rEL MOLDE DE 2 PZAS SE ACTIVO= %d\n,",moldeacti);blue.printf(" VECES\r\n");
00127          blue.printf("\rEL MOLDE DE 7 PZAS SE ACTIVO= %d\n,",moldeactir);blue.printf(" VECES\r\n");}
00128         }   
00129     }
00130     
00131     }
00132 
00133   
00134     
00135   
00136 int main() {
00137    // Thread Hthread1 (mold_thread);
00138     //Thread Hthread2 (moldt_thread);
00139     Thread Hthread1 (segur_thread);
00140     //Thread Hthread4 (pro_thread);
00141     Thread Hthread2 (modacti_thread);
00142     Thread Hthread3 (modacti1_thread);
00143     Thread Hthread4 (prome_thread);
00144     Thread Hthread5 (trans_thread);
00145     blue.baud(9600);
00146   
00147       #ifdef INITIAL_RUN
00148     clk.set_time(1496802000);
00149     #endif
00150     
00151     char storedByte = clk.recallByte(0);
00152     pc.printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte);
00153     clk.storeByte(0, storedByte + 1);
00154     ACS712 dev(p18);
00155     while (1) 
00156     {
00157            time_t seconds = clk.time(NULL);
00158            //pc.printf("Time as a basic string = %s\r", ctime(&seconds));
00159            pc.printf("Sensor Value: %2.2f A\n\r", float(dev));
00160         
00161        
00162     if (cont==0 && dev >= 0.02)
00163     {cont=1; contador++;espera=0;tred=1;}
00164     
00165     if (cont==1 && dev <= -0.05)
00166     { cont=0;tred=0;}
00167     
00168     if (dev <= -0.05 && contador>=1)
00169     {espera++; pc.printf("espera=%d\n",espera);}
00170     
00171     //if (espera>=40)
00172     //{descanso++;pc.printf("descanso=%f\n",descanso);pc.printf("retardo=%f\n",retardo);}
00173     
00174     if( gf==0 && espera>=40)
00175       {gf=1;retardo++;}
00176       
00177     if (gf==1 && espera<=20)
00178     {gf=0;}
00179     
00180    // promedio=(descanso/60)/retardo;
00181     
00182     promaltos=(altos)/contador;
00183     
00184     prombajos=(bajos)/(contador+1);
00185       
00186     pc.printf("conta=%d\n\r",contador);
00187      wait(1);
00188         
00189     if (espera==20)
00190     {blue.printf("EL PROCESO SE DETUVO EN TIEMPO Y FECHA:%s\r", ctime(&seconds));}
00191     
00192     if (espera >= 20 && dev >0.02)
00193     {blue.printf("El PROCESO ARRANCO EN TIEMPO Y FECHA:%s\r", ctime(&seconds));}
00194 
00195     
00196     //if (contador==1 && dev < 0)
00197     //{blue.printf("INICIO DE PROCESO\n\r");}   
00198     
00199     }
00200 }