the program have one sensor acs712 and moduleds1302

Dependencies:   FATFileSystem mbed

main.cpp

Committer:
RUBENTEC
Date:
2017-06-08
Revision:
0:b82abca089ac

File content as of revision 0:b82abca089ac:

#define SCLK    p13
#define IO      p12
#define CE      p11

#define INITIAL_RUN

#include "mbed.h"
#include <string.h>
#include "rtos.h"
#include "DS1302.h"
#include "ACS712.h""


DigitalOut led(LED1);
DigitalIn button(p21);
DigitalIn mold1(p22);
DigitalIn mold2(p23);
DigitalIn seguridad(p24);
DigitalIn operacion(p25);
DigitalIn molder(p26);
DigitalIn molder1(p27);
int molde;
int molde2;
int cont;
int conta;
int piezas;
int pieza;
int contador=0;
int espera;
int moldeacti=0;
int moldeactir=0;
float bajos=0;
float altos=0;
int tred;
float descanso;
float retardo=0;
float gf;
float promedio;
float promaltos;
float prombajos;
float trabajo;
char LED[100];
char Str[100];
 // ACS712 dev(p19);
Serial pc(USBTX, USBRX);
Serial blue(p9, p10);
DS1302 clk(SCLK, IO, CE);
Mutex stdio_mutex; 
 

 /*void mold_thread(void const *argument){
     while(1){
     if (mold1==1)
        {blue.printf("MOLDE DE 2 PIEZAS\n\r");wait(0.5);}
        }
    }

void moldt_thread(void const *argument){
     while(1){
     if (mold2==1)
        {blue.printf("MOLDE DE 7 PIEZAS\n\r");wait(0.5);}
        }
    }*/

void segur_thread(void const *argument){
     while(1){
         time_t seconds = clk.time(NULL);
     if (seguridad==1)
        {blue.printf("SEGURIDAD VIOLADA ");wait(0.5);blue.printf("EN TIEMPO Y FECHA:%s\r", ctime(&seconds));} 
        }   
    }  

/*void pro_thread(void const *argument){
     while(1){
     if (operacion==1)
        //{blue.printf("\rPROMEDIO DESCANSO=%f",promedio); blue.printf(" MINUTOS\n");wait(0.5);
        {blue.printf("\rPROMEDIO DE TIEMPOS ALTOS= %f",promaltos);blue.printf(" SEGUNDOS\n");wait(0.5);
        blue.printf("\rPROMEDIO DE TIEMPOS BAJOS= %f",prombajos);blue.printf(" SEGUNDOS\n\r");wait(0.5);}
        }   
    } */ 
    
void modacti_thread(void const *argument){
     while(1){
if (molder==1)
    {molde=1;pc.printf("CONT MOLD1=%d\n",moldeacti);}else{molde=0;}
    
    if (molde==1)
    {moldeacti++;wait(25);}
    }
}
void modacti1_thread(void const *argument){
     while(1){
if (molder1==1)
    {molde2=1;pc.printf("CONT MOLD2=%d\n",moldeactir);}else{molde2=0;}
    
    if (molde2==1)
    {moldeactir++;wait(25);}
    }
}

void prome_thread(void const *argument){
     while(1){
          if (tred==1)
           {altos++; wait(1); pc.printf("altos= %f\n",altos);}
    
         if (tred==0 && contador>=1&& espera<=24)
         {bajos++;wait(1); pc.printf("bajos= %f\n",bajos);}

    }
}

void trans_thread(void const *argument){
 while(1)
     {
         if (blue.readable())
         {
             blue.scanf("%s",&Str);
             pc.printf("%s\n\r",Str);
            strcpy(LED,   strtok(Str , ","));
            
             
             if(strcmp(LED,"ACEPTAR")==0){led=1;
            blue.printf("\rPROMEDIO DE TIEMPOS ALTOS= %f",promaltos);blue.printf(" SEGUNDOS\n");wait(0.5);
        blue.printf("\rPROMEDIO DE TIEMPOS BAJOS= %f",prombajos);blue.printf(" SEGUNDOS\n\r");wait(0.5);
        blue.printf("\rEL MOTOR SE ACTIVO= %d\n,",contador);blue.printf(" VECES\r\n");
        blue.printf("\rEL MOLDE DE 2 PZAS SE ACTIVO= %d\n,",moldeacti);blue.printf(" VECES\r\n");
         blue.printf("\rEL MOLDE DE 7 PZAS SE ACTIVO= %d\n,",moldeactir);blue.printf(" VECES\r\n");}
        }   
    }
    
    }

  
    
  
int main() {
   // Thread Hthread1 (mold_thread);
    //Thread Hthread2 (moldt_thread);
    Thread Hthread1 (segur_thread);
    //Thread Hthread4 (pro_thread);
    Thread Hthread2 (modacti_thread);
    Thread Hthread3 (modacti1_thread);
    Thread Hthread4 (prome_thread);
    Thread Hthread5 (trans_thread);
    blue.baud(9600);
  
      #ifdef INITIAL_RUN
    clk.set_time(1496802000);
    #endif
    
    char storedByte = clk.recallByte(0);
    pc.printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte);
    clk.storeByte(0, storedByte + 1);
    ACS712 dev(p18);
    while (1) 
    {
           time_t seconds = clk.time(NULL);
           //pc.printf("Time as a basic string = %s\r", ctime(&seconds));
           pc.printf("Sensor Value: %2.2f A\n\r", float(dev));
        
       
    if (cont==0 && dev >= 0.02)
    {cont=1; contador++;espera=0;tred=1;}
    
    if (cont==1 && dev <= -0.05)
    { cont=0;tred=0;}
    
    if (dev <= -0.05 && contador>=1)
    {espera++; pc.printf("espera=%d\n",espera);}
    
    //if (espera>=40)
    //{descanso++;pc.printf("descanso=%f\n",descanso);pc.printf("retardo=%f\n",retardo);}
    
    if( gf==0 && espera>=40)
      {gf=1;retardo++;}
      
    if (gf==1 && espera<=20)
    {gf=0;}
    
   // promedio=(descanso/60)/retardo;
    
    promaltos=(altos)/contador;
    
    prombajos=(bajos)/(contador+1);
      
    pc.printf("conta=%d\n\r",contador);
     wait(1);
        
    if (espera==20)
    {blue.printf("EL PROCESO SE DETUVO EN TIEMPO Y FECHA:%s\r", ctime(&seconds));}
    
    if (espera >= 20 && dev >0.02)
    {blue.printf("El PROCESO ARRANCO EN TIEMPO Y FECHA:%s\r", ctime(&seconds));}

    
    //if (contador==1 && dev < 0)
    //{blue.printf("INICIO DE PROCESO\n\r");}   
    
    }
}