Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. #include <max6675.h>
  2. #include <LiquidCrystal.h>
  3.  
  4. //
  5. // VARIABLES
  6. //
  7.  
  8. // Machine's phisical state
  9. int temperatura = 0; // en °C
  10. int temperatura_prev;
  11. // int humedad;
  12. bool maquina_en_funcionamiento = false;
  13.  
  14. // User's variables (con la maquina no funcionando)
  15. int array_de_valores[] = {20,10}; // valores editables por el usuario
  16. int valor_seleccionado = 0; // valor que se esta editando (por defecto, temperatura): 0=Temperatura, 1=Tiempo, 2=Humedad
  17. // Variables visibles para el usuario (con la maquina funcionando)
  18. int array_de_opciones[] = {0, 0}; // opciones visibles para el usuario
  19. int opcion_seleccionada = 0; // opcion visible (por defecto, maquina): 0=Maquina, 1=Operacion
  20.  
  21. // Process' variables
  22. int temp_bus, tiempo_bus, hum_bus; // copias de array_de_valores que se mantendran fijas hasta el final del proceso
  23. int reloj = 0; // tiempo transcurrido en minutos desde que comenzo el proceso
  24. int reloj_prev;
  25. unsigned long reloj_milis; // tiempo transcurrido en milisegundos desde que comenzo el proceso
  26. unsigned long inicio_milis; // milisegundos al comienzo de la operacion
  27. int tiempo_restante;
  28. bool resistencia_en_funcionamiento = false; // estado actual de la resistencia (por defecto, apagada)
  29.  
  30. // Phisical components
  31. LiquidCrystal lcd(6,7, 2,3,4,5);
  32. MAX6675 sensor_temp(16,15,14);
  33. #define RELE 13
  34. // botones
  35. #define AUMENTAR 8 // Aumentar valor
  36. #define DISMINUIR 9 // Disminuir valor
  37. #define SIGUIENTE 10 // Cambiar valor
  38. bool sig_pres = false;
  39. #define INI_DET 11 // Iniciar o Detener maquina
  40. bool ini_det_pres = false;
  41.  
  42. //
  43. // FUNCTIONS
  44. //
  45.  
  46. // Funciones para control de lcd
  47. void actualizarLCD() {
  48. if (!maquina_en_funcionamiento) { // si la maquina no esta en funcionamiento
  49. switch (valor_seleccionado){
  50. case 0: // temperatura
  51. lcd.home();
  52. lcd.print("»TEMP TIEMPO HUM");
  53. lcd.setCursor(5,1);
  54. lcd.print(array_de_valores[0]);
  55. lcd.setCursor(8,1);
  56. lcd.print("°C");
  57. break;
  58. case 1: // tiempo
  59. lcd.home();
  60. lcd.print(" TEMP»TIEMPO HUM");
  61. lcd.setCursor(5,1);
  62. lcd.print(array_de_valores[1]);
  63. lcd.setCursor(8,1);
  64. lcd.print("min");
  65. break;
  66. case 2: // humedad
  67. lcd.home();
  68. lcd.print(" TEMP TIEMPO»HUM");
  69. lcd.setCursor(5,1);
  70. lcd.print(array_de_valores[2]);
  71. lcd.setCursor(8,1);
  72. lcd.print("%");
  73. break;
  74. }
  75. }
  76. else { // si la maquina esta en funcionamiento
  77. switch (opcion_seleccionada){
  78. case 0:
  79. lcd.home();
  80. lcd.print("»MAQUINA OPERAC");
  81. lcd.setCursor(0,1);
  82. lcd.print("TEMP");
  83. lcd.setCursor(5,1);
  84. lcd.print(temperatura);
  85. lcd.setCursor(9,1);
  86. lcd.print("TIE");
  87. lcd.setCursor(13,1);
  88. lcd.print(tiempo_restante);
  89. break;
  90. case 1:
  91. lcd.home();
  92. lcd.print(" MAQUINA »OPERAC");
  93. lcd.setCursor(0,1);
  94. lcd.print("TEMP");
  95. lcd.setCursor(5,1);
  96. lcd.print(temp_bus);
  97. lcd.setCursor(9,1);
  98. lcd.print("TIE");
  99. lcd.setCursor(13,1);
  100. lcd.print(tiempo_bus);
  101. break;
  102. }
  103. }
  104. }
  105.  
  106. // Funciones de variables editables por el usuario
  107. void aumentarValor(int cantidad) {
  108. array_de_valores[valor_seleccionado] += cantidad;
  109. actualizarLCD();
  110. }
  111. void cambiarValor(){
  112. switch (valor_seleccionado){
  113. case 0:
  114. valor_seleccionado = 1;
  115. break;
  116. case 1:
  117. valor_seleccionado = 2;
  118. break;
  119. case 2:
  120. valor_seleccionado = 0;
  121. break;
  122. }
  123. actualizarLCD();
  124. }
  125.  
  126. // Funciones de control de proceso
  127. void iniciarResistencia() {
  128. digitalWrite(RELE, HIGH);
  129. resistencia_en_funcionamiento = true;
  130. }
  131. void detenerResistencia() {
  132. digitalWrite(RELE, LOW);
  133. resistencia_en_funcionamiento = false;
  134. }
  135.  
  136. void iniciarProc() {
  137. // realizar una copia de los valores en array_de_valores
  138. temp_bus = array_de_valores[0];
  139. tiempo_bus = array_de_valores[1];
  140. inicio_milis = millis();
  141.  
  142. // iniciar la maquina (cuando continue el ciclo)
  143. maquina_en_funcionamiento = true;
  144.  
  145. actualizarLCD();
  146. }
  147. void detenerProc() {
  148. detenerResistencia();
  149.  
  150. // borrar variables
  151. temp_bus = 0;
  152. tiempo_bus = 0;
  153. reloj_prev = 0;
  154. reloj_milis = 0;
  155. inicio_milis = 0;
  156. tiempo_restante = 0;
  157.  
  158. maquina_en_funcionamiento = false;
  159.  
  160. actualizarLCD();
  161. }
  162.  
  163. //
  164. // MAIN PROGRAM
  165. //
  166.  
  167. void setup() {
  168. // codigo que se ejecuta una unica vez:
  169. Serial.begin(9600);
  170. Serial.println("INICIANDO PROGRAMA");
  171.  
  172. // relay
  173. pinMode(RELE, OUTPUT);
  174. detenerResistencia();
  175.  
  176. // buttons
  177. pinMode(AUMENTAR, INPUT);
  178. pinMode(DISMINUIR, INPUT);
  179. pinMode(SIGUIENTE, INPUT);
  180. pinMode(INI_DET, INPUT);
  181.  
  182. //iniciar lcd
  183. lcd.begin(16,2);
  184. actualizarLCD();
  185. }
  186.  
  187. void loop() { // codigo que se ejecuta en bucle:
  188. // buttons
  189. if (digitalRead(AUMENTAR) == HIGH) { // if button AUMENTAR is pressed
  190. if (!maquina_en_funcionamiento) { // and the machine is not working
  191. iniciarResistencia();
  192. aumentarValor(5); // increase the value
  193. delay(500); // and wait
  194. detenerResistencia();
  195. }
  196. }
  197. else if (digitalRead(DISMINUIR) == HIGH) { // si se presiona el boton DISMINUIR
  198. if (!maquina_en_funcionamiento) { // IDEM AUMENTAR
  199. aumentarValor(-5);
  200. delay(500);
  201. }
  202. }
  203. else if (digitalRead(SIGUIENTE) == HIGH) { // si se presiona el boton SIGUIENTE
  204. if (!sig_pres) { // verificar si no se lo presiono antes
  205. sig_pres = true; // poner la var sig_pres en true para bloquear el boton
  206. cambiarValor(); // y cambiar la variable
  207. }
  208. }
  209. else if (digitalRead(INI_DET) == HIGH) { // si se presiona el boton INI_DET
  210. if (!ini_det_pres) { // verificar si no se lo presiono antes
  211. ini_det_pres = true; // poner la var ini_det_pres en true para bloquear el boton
  212. if (!maquina_en_funcionamiento) {
  213. iniciarProc();
  214. }
  215. else {
  216. detenerProc();
  217. }
  218. }
  219. }
  220.  
  221. if (digitalRead(SIGUIENTE) == LOW) { // si el boton SIGUIENTE no esta siendo presionado
  222. if (sig_pres) {
  223. sig_pres = false; // desbloquear el boton
  224. }
  225. }
  226. if (digitalRead(INI_DET) == LOW) { // si el boton INI_DET no esta siendo presionado
  227. if (ini_det_pres) {
  228. ini_det_pres = false; // desbloquear el boton
  229. }
  230. }
  231.  
  232. // Some output of data to the Serial monitor
  233. Serial.print("Valor seleccionado: ");
  234. Serial.println(valor_seleccionado);
  235. Serial.print(array_de_valores[0]);
  236. Serial.print(", ");
  237. Serial.println(array_de_valores[1]);
  238. Serial.print("Maquina en funcionamiento: ");
  239. Serial.println(maquina_en_funcionamiento);
  240.  
  241. // PROCESS CONTROL
  242. if(maquina_en_funcionamiento) {
  243.  
  244. // TEMPERATURE CONTROL
  245. temperatura_prev = temperatura;
  246. temperatura = sensor_temp.readCelsius();
  247.  
  248. if (temperatura >= temp_bus) { // si la temperatura es igual o exede la deseada
  249. detenerResistencia(); // apagar la resistencia
  250. }
  251. else { // si la temperatura es menor que la deseada
  252. if (!resistencia_en_funcionamiento) { // y la resistencia esta apagada
  253. iniciarResistencia();
  254. }
  255. }
  256.  
  257. if (temperatura_prev != temperatura) {
  258. actualizarLCD();
  259. }
  260.  
  261. // TIME CONTROL
  262. unsigned long milis_ahora = millis();
  263. milis_ahora -= inicio_milis;
  264. if ( (milis_ahora - reloj_milis) >= 1000){
  265. reloj_milis = reloj_milis + 1000;
  266. }
  267.  
  268. reloj_prev = reloj;
  269. reloj = reloj_milis/60000;
  270.  
  271. tiempo_restante = tiempo_bus - reloj;
  272. /*
  273. Serial.print("Reloj de milisegundos=");
  274. Serial.println(reloj_milis);
  275. Serial.print("Reloj=");
  276. Serial.println(reloj);
  277. Serial.print("Tiempo restante=");
  278. Serial.println(tiempo_restante);
  279. */
  280. if (reloj >= tiempo_bus) { // si se llego al tiempo deseado
  281. detenerProc(); // detener
  282. }
  283.  
  284. if (reloj_prev != reloj) {
  285. actualizarLCD();
  286. }
  287.  
  288. }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement