Advertisement
KenFalco

v2

Sep 15th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.16 KB | None | 0 0
  1.  
  2. const int ledPinEV_1 = 22;
  3. const int ledPinEV_2 = 23;
  4. const int ledPinEV_3 = 24;
  5. const int ledPinEV_4 = 25;
  6.  
  7. const int ledPinPompa = 26;
  8. const int ledPinResistenza = 27;
  9.  
  10. const int pinTemp = A0;
  11.  
  12. const int EV1 = A1;
  13. const int EV2 = A2;
  14. const int EV3 = A3;
  15. const int EV4 = A4;
  16.  
  17. //Interrupts
  18. const int flussometro1 = 2;
  19. const int flussometro2 = 3;
  20. const int flussometro3 = 18;
  21. const int flussometro4 = 19;
  22.  
  23. volatile unsigned int count1 = 0;
  24. volatile unsigned int count2 = 0;
  25. volatile unsigned int count3 = 0;
  26. volatile unsigned int count4 = 0;
  27.  
  28.  
  29. bool enableFluxCount1 = false;
  30. bool enableFluxCount2 = false;
  31. bool enableFluxCount3 = false;
  32. bool enableFluxCount4 = false;
  33.  
  34.  
  35. unsigned int stato = 0; //Stato iniziale della normale esecuzione
  36. unsigned int valueNTC = 0;
  37. double flusso1 = 0.0;
  38. double flusso2 = 0.0;
  39. double flusso3 = 0.0;
  40. double flusso4 = 0.0;
  41. double temp = 0.0;
  42.  
  43. //Stati delle elettrovalvole, solo via software; nessun controllo hardware (non sicuro)
  44. bool statoEV1 = false;
  45. bool statoEV2 = false;
  46. bool statoEV3 = false;
  47. bool statoEV4 = false;
  48.  
  49.  
  50. void isr_flussometro1() {
  51.   if (enableFluxCount1)
  52.     count1++;
  53. }
  54. void isr_flussometro2() {
  55.   if (enableFluxCount2)
  56.     count2++;
  57. }
  58. void isr_flussometro3() {
  59.   if (enableFluxCount3)
  60.     count3++;
  61. }
  62. void isr_flussometro4() {
  63.   if (enableFluxCount4)
  64.     count4++;
  65. }
  66.  
  67.  
  68. //Dichiarazione funzioni
  69. void testDiagnostico();
  70. String readSensors();
  71. void funzionamentoNormale();
  72.  
  73. void setup() {
  74.  
  75.   Serial.begin(9600);
  76.  
  77.   pinMode(ledPinEV_1, OUTPUT);
  78.   pinMode(ledPinEV_2, OUTPUT);
  79.   pinMode(ledPinEV_3, OUTPUT);
  80.   pinMode(ledPinEV_4, OUTPUT);
  81.  
  82.   digitalWrite(ledPinEV_1, LOW);
  83.   digitalWrite(ledPinEV_2, LOW);
  84.   digitalWrite(ledPinEV_3, LOW);
  85.   digitalWrite(ledPinEV_4, LOW);
  86.  
  87.   pinMode(ledPinPompa, OUTPUT);
  88.   pinMode(ledPinResistenza, OUTPUT);
  89.  
  90.   digitalWrite(ledPinPompa, LOW);
  91.   digitalWrite(ledPinResistenza, LOW);
  92.  
  93.   //Input non necessari, lo sono per default in Arduino
  94.  
  95.   attachInterrupt(digitalPinToInterrupt(flussometro1), isr_flussometro1, RISING);
  96.   attachInterrupt(digitalPinToInterrupt(flussometro2), isr_flussometro2, RISING);
  97.   attachInterrupt(digitalPinToInterrupt(flussometro3), isr_flussometro3, RISING);
  98.   attachInterrupt(digitalPinToInterrupt(flussometro4), isr_flussometro4, RISING);
  99. }
  100.  
  101. void loop() {
  102.  
  103.   testDiagnostico();
  104.  
  105.   funzionamentoNormale();
  106.  
  107. }
  108.  
  109. void testDiagnostico() {
  110.   char fromSerial = '-';
  111.  
  112.   String toSend;
  113.  
  114.   if (Serial.available())
  115.     fromSerial = Serial.read();
  116.  
  117.   if (fromSerial != '-') {
  118.  
  119.     switch (fromSerial) {
  120.       case 'a':
  121.         digitalWrite(ledPinEV_1, HIGH);
  122.         break;
  123.       case 'h':
  124.         digitalWrite(ledPinEV_1, LOW);
  125.         break;
  126.       case 'b':
  127.         digitalWrite(ledPinEV_2, HIGH);
  128.         break;
  129.       case 'i':
  130.         digitalWrite(ledPinEV_2, LOW);
  131.         break;
  132.       case 'd':
  133.         digitalWrite(ledPinEV_3, HIGH);
  134.         break;
  135.       case 'l':
  136.         digitalWrite(ledPinEV_3, LOW);
  137.         break;
  138.       case 'e':
  139.         digitalWrite(ledPinEV_4, HIGH);
  140.         break;
  141.       case 'm':
  142.         digitalWrite(ledPinEV_4, LOW);
  143.         break;
  144.       case 'f':
  145.         digitalWrite(ledPinResistenza, HIGH);
  146.         break;
  147.       case 'n':
  148.         digitalWrite(ledPinResistenza, LOW);
  149.         break;
  150.       case 'g':
  151.         digitalWrite(ledPinPompa, HIGH);
  152.         break;
  153.       case 'o':
  154.         digitalWrite(ledPinPompa, LOW);
  155.         break;
  156.       case 'p':
  157.         toSend = readSensors();
  158.  
  159.         Serial.write(toSend.c_str());
  160.         Serial.flush();
  161.         break;
  162.       case 'q':
  163.         digitalWrite(ledPinEV_4, LOW);
  164.         break;
  165.       default:
  166.         fromSerial = '-';
  167.         break;
  168.     }
  169.   }
  170. }
  171.  
  172. String readSensors() {
  173.   String str;
  174.  
  175.   //stringa = "statoEV1,statoEV2,statoEV3,statoEV4,flusso1,flusso2,flusso3,flusso4,valueNTC,temp"
  176.  
  177.   str += String(statoEV1) + ",";
  178.   str += String(statoEV2) + ",";
  179.   str += String(statoEV3) + ",";
  180.   str += String(statoEV4) + ",";
  181.  
  182.   str += String(flusso1) + ",";
  183.   str += String(flusso2) + ",";
  184.   str += String(flusso3) + ",";
  185.   str += String(flusso4) + ",";
  186.  
  187.   str += String(valueNTC) + ",";
  188.   str += String(temp);
  189.  
  190.   return str;
  191. }
  192.  
  193. void funzionamentoNormale() {
  194.  
  195.   unsigned int previousMillis = 0;
  196.  
  197.   switch (stato) {
  198.     case 0:
  199.       digitalWrite(ledPinEV_1, HIGH); //Attivo la prima EV
  200.       statoEV1 = true;
  201.       digitalWrite(ledPinPompa, HIGH); //Attivo la pompa
  202.       stato++; //Passo allo stato successivo
  203.       break;
  204.     case 1:
  205.       count1 = 0;
  206.       flusso1 = 0.0;
  207.       enableFluxCount1 = true;
  208.  
  209.       while (flusso1 < 1.5) {
  210.         if (millis() - previousMillis >= 1000) {
  211.  
  212.           previousMillis = millis();
  213.  
  214.           flusso1 = (double(count1) * 1.12);
  215.           flusso1 *= 60; //Trasforma i secondi in minuti
  216.           flusso1 /= 1000; //Trasforma i mL in L (litri)
  217.         }
  218.       }
  219.  
  220.       enableFluxCount1 = false;
  221.       stato++;
  222.       break;
  223.     case 2:
  224.       digitalWrite(ledPinEV_1, LOW); //Disattivo la prima EV
  225.       statoEV1 = false;
  226.       digitalWrite(ledPinPompa, LOW); //Disattivo la pompa
  227.       delay(1000);
  228.       stato++;
  229.       break;
  230.     case 3:
  231.       //Attivazione resistenza e verifica temperatura
  232.       digitalWrite(ledPinResistenza, HIGH);
  233.  
  234.       valueNTC = analogRead(pinTemp);
  235.       delay(5);
  236.  
  237.       while (temp < 90) {
  238.         valueNTC = analogRead(pinTemp);
  239.  
  240.         // FIXME !!!!
  241.  
  242.       }
  243.  
  244.       stato++;
  245.       break;
  246.     case 4:
  247.       //Spengo la resistenza
  248.       digitalWrite(ledPinResistenza, LOW);
  249.       stato++;
  250.       break;
  251.     case 5:
  252.       //Attivazione pompa
  253.       digitalWrite(ledPinPompa, HIGH);
  254.       stato++;
  255.       break;
  256.     case 6:
  257.       enableFluxCount1 = true;
  258.       enableFluxCount2 = true;
  259.       enableFluxCount3 = true;
  260.       enableFluxCount4 = true;
  261.       //Reset dei conteggi
  262.       count1 = 0;
  263.       count2 = 0;
  264.       count3 = 0;
  265.       count4 = 0;
  266.       //Inizio sequenza di attivazione
  267.       digitalWrite(ledPinEV_1, HIGH);
  268.       statoEV1 = true;
  269.       delay(5000);
  270.       digitalWrite(ledPinEV_2, HIGH);
  271.       statoEV2 = true;
  272.       delay(5000);
  273.       digitalWrite(ledPinEV_3, HIGH);
  274.       statoEV3 = true;
  275.       delay(5000);
  276.       digitalWrite(ledPinEV_4, HIGH);
  277.       statoEV4 = true;
  278.       delay(5000);
  279.  
  280.       flusso1 = (double(count1) * 1.12);
  281.       flusso1 *= 60;
  282.       flusso1 /= 1000;
  283.  
  284.       flusso2 = (double(count2) * 1.12);
  285.       flusso2 *= 60;
  286.       flusso2 /= 1000;
  287.  
  288.       flusso3 = (double(count3) * 1.12);
  289.       flusso3 *= 60;
  290.       flusso3 /= 1000;
  291.  
  292.       flusso4 = (double(count4) * 1.12);
  293.       flusso4 *= 60;
  294.       flusso4 /= 1000;
  295.  
  296.       stato++;
  297.       break;
  298.     case 7:
  299.       //Disattivo tutte le elettrovalvole
  300.       digitalWrite(ledPinEV_1, LOW);
  301.       statoEV1 = false;
  302.       digitalWrite(ledPinEV_2, LOW);
  303.       statoEV2 = false;
  304.       digitalWrite(ledPinEV_3, LOW);
  305.       statoEV3 = false;
  306.       digitalWrite(ledPinEV_4, LOW);
  307.       statoEV4 = false;
  308.       enableFluxCount1 = false;
  309.       enableFluxCount2 = false;
  310.       enableFluxCount3 = false;
  311.       enableFluxCount4 = false;
  312.  
  313.       stato = 0;
  314.       break;
  315.     default:
  316.       stato = 0;
  317.       break;
  318.   }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement