Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.07 KB | None | 0 0
  1. //#include <Adafruit_GFX.h> incluido en Adafruit_SSD1306.h
  2. //#include <Adafruit_SPITFT.h>
  3. #include <Wire.h>
  4. //#include <Adafruit_SPITFT_Macros.h>
  5. //#include <gfxfont.h>
  6. #include <max6675.h>
  7. #include <Adafruit_SSD1306.h>
  8. #include <PID_v1.h>
  9. //TODO
  10. /*
  11. REVISAR que onda con los rele y los led
  12. PROBAR los max 6675
  13. */
  14. #define tiempoCiclo 2000
  15. unsigned long inicioTiempoVentana;
  16. double setPoint1,setPoint2, input1, input2, output1, output2;
  17. double kp1 = 10, ki1 = 3, kd1= 400;
  18.  
  19. unsigned long respuestaUltimaTemperatura = 0;
  20. unsigned long lastPIDCalculation = 0;
  21.  
  22. PID myPID1(&input1, &output1, &setPoint1, kp1, ki1, kd1, DIRECT);
  23. PID myPID2(&input2, &output2, &setPoint2, kp1, ki1, kd1, DIRECT);
  24. //------------
  25.  
  26. #define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  27. Adafruit_SSD1306 display(OLED_RESET);
  28.  
  29. #define tc_sck 12
  30. #define tc_so 11
  31. MAX6675 tc_1(tc_sck, 8, tc_so); //tc_1.readCelsius()
  32. MAX6675 tc_2(tc_sck, 7, tc_so);
  33.  
  34. #define rele1 5
  35. #define rele2 3
  36.  
  37. #define btn_up 6
  38. #define btn_down 2
  39. #define btn_set 4
  40. #define led_rojo 9// colentando
  41. #define led_verde 10// llego a temepratura
  42. #define buzzer 13
  43.  
  44.  
  45. byte setPoint = 35;//valor inicial
  46. byte posMenu =0;  // posicion actual del menu onda primer item, segundo item
  47. //bool inMenu=0;   // esta modificando el itema del menu ? 0 no .. 1 si
  48. byte contador=90; //90segundos
  49. bool alarma =0; // esta contando la alarma
  50. unsigned int millisActual =0;
  51. unsigned int millisAnterior =0;
  52.  
  53.  
  54. void setup() {
  55.   Serial.begin(9600);
  56.     delay(100);  // This delay is needed to let the display to initialize
  57.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize display with the I2C address of 0x3C
  58.   display.clearDisplay();  // Clear the buffer
  59.   display.setRotation(0);  // Set orientation. Goes from 0, 1, 2 or 3
  60.   display.setTextColor(WHITE);
  61.   display.setTextWrap(false);  // By default, long lines of text are set to automatically “wrap” back to the leftmost column.
  62.                                // To override this behavior (so text will run off the right side of the display - useful for
  63.                                // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
  64.                                // with setTextWrap(true).
  65.  
  66.   display.dim(0);  //Set brightness (0 is maximun and 1 is a little dim)
  67.   //------
  68.   myPID1.SetOutputLimits(0, tiempoCiclo);
  69.   myPID1.SetSampleTime(tiempoCiclo);
  70.   myPID1.SetMode(AUTOMATIC);
  71.  
  72.   myPID2.SetOutputLimits(0, tiempoCiclo);
  73.   myPID2.SetSampleTime(tiempoCiclo);
  74.   myPID2.SetMode(AUTOMATIC);
  75.  
  76.   inicioTiempoVentana=millis();
  77.   //-----
  78.   pinMode(led_verde,OUTPUT);
  79.   pinMode(led_rojo,OUTPUT);
  80.   pinMode(buzzer,OUTPUT);
  81.   pinMode(rele1,OUTPUT);
  82.   pinMode(rele2,OUTPUT);
  83.  
  84.  
  85. }
  86.  
  87. void loop() {
  88.   display.clearDisplay();
  89.  
  90.  
  91.   if(digitalRead(btn_up  )==LOW){   oledMenu_var( 1 );  }
  92.   if(digitalRead(btn_down)==LOW){   oledMenu_var( 2 );  }
  93.   if(digitalRead(btn_set )==LOW){   oledMenu_var( 3 );  }
  94.  
  95.   input1= tc_1.readCelsius();
  96.   input2= tc_2.readCelsius();
  97.   setPoint1= setPoint; //setPointn se refiere al detpoint de PID n y "setPoint" es el setpoint modificado desde el menu
  98.   setPoint2= setPoint;//ver deficion myPID2
  99.   myPID1.Compute();
  100.   myPID2.Compute();
  101.      
  102.   millisActual=millis();  
  103.     if (millisActual - inicioTiempoVentana > tiempoCiclo)
  104.   { //time to shift the Relay Window
  105.     inicioTiempoVentana += tiempoCiclo;
  106.   }
  107.   if (output1 > millisActual - inicioTiempoVentana){
  108.     Serial.println(F("RELE HiGH"));
  109.     digitalWrite(rele1,HIGH);
  110.     digitalWrite(rele2,HIGH);
  111.     digitalWrite(led_rojo,HIGH);    }    
  112.   else {
  113.     Serial.println(F("RELE low"));  
  114.     digitalWrite(rele1, LOW);
  115.     digitalWrite(rele2,LOW);
  116.     digitalWrite(led_rojo,LOW);
  117.    
  118. }
  119.   if(alarma==true && millisActual - millisAnterior >= 1000 -(75+5) ){//cuenta regresiva del contador
  120.     millisAnterior = millisActual;
  121.     contador--;
  122.     if(contador <=0){
  123.       alarma==false;
  124.       beep2();    }
  125.     }
  126.  
  127.  
  128.   oledDisplay(tc_1.readCelsius() ,tc_2.readCelsius()   );
  129.   oledMenu_items();
  130.  
  131.   if(tc_2.readCelsius() >= setPoint){
  132.     //beep1();
  133.     digitalWrite(led_verde, HIGH);
  134.     }
  135.   if(tc_2.readCelsius() < setPoint){
  136.     digitalWrite(led_verde,LOW);
  137.     }
  138.  
  139.   display.display();  
  140.   delay(75);
  141. }
  142.  
  143.  
  144. void oledDisplay(int pv1, int pv2){
  145.   display.setTextSize(1);
  146.   display.setCursor(5,0);
  147.   display.print(F("pv"));
  148.   display.setTextSize(3);
  149.   display.print(pv1);
  150.   display.print(F("c"));
  151.   display.print(pv2);
  152.   }
  153. void oledMenu_var(int btn){
  154.   switch(btn){
  155.     case 1://accion boton arriba
  156.         switch(posMenu%3){
  157.           case 0:
  158.             setPoint++;
  159.             break;
  160.           case 1:
  161.             contador+=5;
  162.             break;
  163.           case 2:
  164.             alarma= !alarma;
  165.           break;
  166.           }
  167.       break; // fin case 1
  168.      
  169.     case 2://accion boton abajo
  170.         switch(posMenu%3){
  171.           case 0:
  172.             setPoint--;
  173.             break;
  174.           case 1:
  175.             contador-=5;
  176.             break;
  177.           case 2:
  178.             alarma= !alarma;
  179.           break;
  180.           }
  181.      break; // fin case 2
  182.      
  183.     case 3://accion boton set
  184.       posMenu++;
  185.       break;
  186.     }//finswitch
  187.    
  188.  
  189.   }//fin oled menu_var
  190.  
  191. void oledMenu_items(void){
  192.   byte posY0=25; // esta varieable esta demas
  193.   byte posY1= posY0+7;
  194.  
  195.    display.setTextSize(1);
  196.    
  197.   if(posMenu%3==0){
  198.     display.setCursor(0,posY0);
  199.     display.print(F(">"));
  200.     posY1+=8;                   //como se agrandan los numeros hago mas espacio
  201.     }
  202.    
  203.   display.setCursor(10,posY0);
  204.   display.print(F("setPoint: "));
  205.  
  206.   if(posMenu%3==0){display.setTextSize(2);}
  207.  
  208.   display.print(setPoint);
  209.   display.setTextSize(1);
  210.   display.print(F("c"));
  211.  
  212.   byte posY2= posY1+7;        //
  213.   if(posMenu%3==1){
  214.     display.setCursor(0,posY1);
  215.     display.print(F(">"));
  216.     posY2+=8;
  217.     }  
  218.   display.setCursor(10,posY1);
  219.   display.print(F("Timer: "));
  220.  
  221.   if(posMenu%3==1){display.setTextSize(2); }  //agrande letra
  222.  
  223.   display.print(F("0"));
  224.   byte aux=(int)(contador/60);
  225.   display.print(aux);
  226.   display.print(F(":"));
  227.   if(contador%60<10){ //si el nuemro es de 1 dijito pone el 0 adelante
  228.     display.print(F("0"));
  229.     display.print(contador%60); }
  230.   else{display.print(contador%60); }
  231.   display.setTextSize(1);    //achico letra
  232.  
  233.   if(posMenu%3==2){
  234.     display.setCursor(0,posY2);
  235.     display.print(F(">"));  }
  236.    
  237.   display.setCursor(10,posY2);
  238.   display.print(F("Alarma: "));
  239.  
  240.   if(posMenu%3==2){display.setTextSize(2);}
  241.  
  242.   if(alarma==true){ display.print(F("activ.")); }  
  243.   else{display.print(F("desctiv."));  }
  244.   display.setTextSize(1);
  245.  
  246.   }//fin oledMunu_items  
  247.  
  248. void beep1(void){
  249.   //envia un solo pitido
  250.   }
  251. void beep2(void){
  252.   // suena como microondas beeeeep beeeeep beeeep
  253.   tone(buzzer,15000,1000);// tono en elpin buzzer, de tantos hz,dura tanto tiempo
  254.   tone(buzzer,0,750);
  255.   tone(buzzer,15000,1000);
  256.   tone(buzzer,0,750);
  257.   tone(buzzer,15000,1000);
  258.   }    
  259. void oledAnimaciones(bool alarma, bool estado){
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement