alle_x_andy

Arduino_Menu_Ionization_chamber

Aug 22nd, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.77 KB | None | 0 0
  1. //#include <Stepper.h>
  2. #include <ADS1115.h>
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6.   // define o número de passos e os pinos utilizados para o motor de passo
  7. //  Stepper myStepper = Stepper(200, 8, 9, 10, 11);
  8.  
  9.   // Inicia a biblioteca LCD com o endereço 0x27 ou 0x3f; definição de matrizes, vetores e constentes
  10. LiquidCrystal_I2C lcd(0x27,16,02);
  11. ADS1115 adc0(ADS1115_DEFAULT_ADDRESS);
  12.  
  13. int key = 0, lastkey = 0, count = 0, P[5] = {0,0,0,0,0}, change = 0, Var[2][2] = {{15,10},{300,500}},steps = 0, V = 0, a = 0, b = 0, c = 1, shutD = 0;
  14. float volts = 0.0000, LV = 0.0000;
  15. const int f[9] = {0,3,0,1,3,2,4,1,2};
  16. const int g[3][9] ={{0,1,0,-1, 0,1,0,1,-1},
  17.                     {0,1,0,-1,-1,1,0,1,-1},
  18.                     {0,0,0, 0,-1,0,1,0, 0}};
  19. unsigned long T, LT;
  20. const int PM[5] = {2,1,1,1,2}, VarM[2][2] = {{30,20},{1000,1000}}, x = 0, y = 1, z = 2, t = 3, p = 4;
  21.  
  22. String menu[3][4] = {{  "*---->Menu<----* ", "Medir/Desloc.   ",  "Medir/Tempo     ",  "Medir Pico      "  },
  23.                     {   "Det. X:          ", "Det. DX:        ",  "Det. T:         ",  "Det. DT:        "  },
  24.                     {   "Start p/ medir   ", "Medindo...      ",  "Concluido       ",  "   Cancelado!   "  }};
  25.  
  26. void setup() {
  27.   // inicia o display e a porta serial
  28.   lcd.init();
  29.   Serial.begin(115200);
  30.  
  31.   //inicia o I2Cdev
  32.   Wire.begin();
  33.   adc0.initialize();
  34.   adc0.setMode(ADS1115_MODE_CONTINUOUS);
  35.  // adc0.setMode(ADS1115_MODE_SINGLESHOT);
  36.   adc0.setRate(ADS1115_RATE_860);
  37.   adc0.setGain(ADS1115_PGA_6P144);
  38.  
  39.   //configura a velocidade do motor [RPM]
  40.  //  myStepper.setSpeed(40);
  41.  
  42.   lcd.setBacklight(HIGH);
  43.   V = 1;
  44.   if(!Serial.available()){
  45.     lcd.clear();
  46.     lcd.print("Conecte a porta ");
  47.     lcd.setCursor(0,1);
  48.     lcd.print("USB e Terminal");
  49.   }
  50.      Serial.println("Envie algum caractere para checar a conexão");
  51.   while(!Serial.available()){
  52.     delay(500);
  53.   }
  54.    Serial.println("Conexão bem sucedida, não desconecte o cabo enquanto estiver realizando medidas. \n \n");
  55.     change = 1;
  56. }
  57.  
  58. //Limita os valores das variaveis entre 0 e o valor máximo
  59. void MaxMin(){
  60.   for(int i = 0; i <= 2 ; i++){
  61.    if (P[i] > PM[i]){
  62.     P[i] = 0;
  63.    }
  64.    if (P[i] < 0){
  65.     P[i] = PM[i];
  66.    }
  67.    for(int j = 0; j <= 1 && i <= 1; j++){
  68.     if(Var[i][j] >= VarM[i][j]){
  69.       Var[i][j] = VarM[i][j];
  70.     }
  71.     if(Var[i][j] < 1){
  72.       Var[i][j] = 1;
  73.     }
  74.    }
  75.   }
  76.   for(int k = 3; k <= 4 ; k++){
  77.    if (P[k] > PM[k]){
  78.     P[k] = PM[k];
  79.    }
  80.    if (P[k] < 0){
  81.     P[k] = 0;
  82.    }
  83. }
  84. }
  85. /* Arranjo para o valor de key
  86.  * back 4 5 6 start
  87.  *      3 1 7
  88.  *        8
  89.  */
  90.  
  91. //Transforma a tecla pressionada em um valor entre 0 e 8, exceto 2;
  92. void Keys(){
  93.   change = 0;
  94. key =(analogRead(A0) + 10)/128;
  95. if(key != 0){
  96.   shutD =0;  
  97. }
  98. switch(key){
  99.   case 2:  key = 7;  break;
  100.   case 3:  key = 8;  break;
  101.   case 8:  key = 4;  break;
  102.   case 7:  key = 4;  break;
  103.   case 4:  key = 3;  break;
  104.   case 1:  key = 5;  break;
  105.   case 5:  key = 1;  break;
  106. }
  107. }
  108.  
  109. //Resolve as teclas
  110. void Actions(){
  111.   change = 1;
  112.    if(f[key] == 0 || g[P[p]][key] == 0){
  113.     change = 0;
  114.    }
  115.    
  116.   if(f[key] == 1){
  117.     P[P[p]] += g[P[p]][key];
  118.   }
  119.  
  120.    if(f[key] == 2 && P[p] == 1){
  121.     Var[P[x]][P[y]] += g[P[p]][key];
  122.     change = 2;
  123.   }
  124.  
  125.    if(f[key] == 3){
  126.    
  127.     if(P[t] != 1){
  128.       P[p] += g[P[p]][key];
  129.       }
  130.     if(P[x] == 2 && P[p] > 0 && key == 4 && P[t] != 1){
  131.       P[p] = 0;
  132.       }
  133.     if(key == 4){
  134.       P[y] = 0;
  135.       P[t] = 0;
  136.       }
  137.     if(P[x] == 2 && P[p] > 0 && key == 1){
  138.       P[p] = 2;
  139.       }
  140.   }
  141.  
  142.    if(f[key] == 4 && P[p] == 2){
  143.     P[t] += g[P[p]][key];
  144.   }
  145.   MaxMin();
  146. }
  147.  
  148. //Imprime os valores na tela
  149. void Menu(){
  150.  if(change == 1){
  151.  
  152.  Update();
  153.  lcd.clear();
  154.  lcd.print(menu[0][a]);
  155.  lcd.setCursor(0,1);
  156.  lcd.print(menu[b][c]);
  157.  if (P[p] == 1 && (key == 1 || key == 3 || key == 4 || key == 7) && P[x] < 2){
  158.  change = 2;
  159. }
  160. }
  161. }
  162.  
  163. //fornece os valores para o menu;
  164. void Update(){
  165.   if(P[p] == 0){
  166.     a = 0;
  167.   }
  168.   if(P[p] == 1 || P[p] == 2){
  169.     a = P[x] + 1;
  170.   }
  171.   if(P[p] == 0){
  172.     c = P[x] + 1;
  173.   }
  174.   if(P[p] == 1 && P[x] <=1){
  175.     c = P[y] + (2*P[x]);
  176.   }
  177.   //if(P[p] == 1 && P[x] == 2 && key != 4){
  178.     //P[p] = ;
  179.   //}
  180.   if(P[p] == 2){
  181.     c = P[t];
  182.   }
  183.   b = P[p];
  184. }
  185.  
  186. // muda o valor da variável na tela
  187. void VarChange(){
  188.   if(change == 2){
  189.   lcd.setCursor(9,1);
  190.   lcd.print("         ");
  191.   lcd.setCursor(9,1);
  192.   lcd.print(Var[P[x]][P[y]]);
  193.   if(P[x] == 0 && P[y] == 0){
  194.     lcd.print("c");
  195.   }
  196.   for(int i = 0; i<=1; i++){
  197.     if(P[i] == i){
  198.       lcd.print("m");
  199.     }
  200.   }
  201.   if(P[x] == 1){
  202.     lcd.print("s");
  203.   }
  204. }
  205. }
  206.  
  207. void function(){
  208.  
  209.   //medida em função da distância (Dificuldades técnicas)
  210.   if(P[x] == 0){
  211.     lcd.clear();
  212.     lcd.print("Atualize o prog.");
  213.     lcd.setCursor(0,1);
  214.     lcd.print("p/ obter a func.");
  215.     Serial.println("Esta função não se encontra disponível no momento por apresentar falhas técnicas, se desejar pode editar a parte do código em que ela se encontra para o seu trabalho");
  216.  
  217.   do{
  218.   Keys();
  219.   delay(100);
  220.   }while(key == 0);
  221.   }
  222.  
  223.   /*
  224.   if(P[x] == 0){
  225.     V = 0;
  226.     steps = 10*Var[0][1]/0.89;
  227.     lcd.clear();
  228.     lcd.print("Posicione em 0cm");
  229.     lcd.setCursor(0,1);
  230.     lcd.print("e pres. <start>  ");
  231.    do{
  232.     Keys();
  233.     delay(30);
  234.    }while(key != 6);
  235.    delay(100);
  236.    //ajusta à distância desejada e aguarda a estabilização da câmara por 120S
  237.     myStepper.step(-(Var[0][0]*100)/0.89);
  238.    
  239.    for(int i = 0; i <= 40; i++){
  240.     Serial.print("Distância(cm)   Volts");
  241.     lcd.clear();
  242.     for(int j = 0; j < 3; j++){
  243.       lcd.print("*");
  244.       delay(200);
  245.     }
  246.     delay(400);
  247.    }
  248.     lcd.clear();
  249.     lcd.print("Medindo...");
  250.     for(int i = (Var[0][0]*100)/0.89; i >= 0; i -= steps){
  251.        myStepper.step(steps);
  252.        delay(20000);
  253.        adc0.setMultiplexer(ADS1115_MUX_P0_NG);
  254.        adc0.triggerConversion();
  255.        volts = (adc0.getMilliVolts(true))/1000.0000;
  256.        
  257.        float j = (i*0.89)/10;
  258.        Serial.print( j, 2);
  259.        Serial.print("\t \t \t");
  260.        Serial.println( volts, 4);      
  261.     }
  262.     lcd.clear();
  263.     lcd.print("Fim de medida");
  264.     Serial.println("Concluído");
  265.    
  266.   }
  267. */
  268.  
  269.   // medida em  função do tempo
  270.   if(P[x] == 1){
  271.     LT = millis();
  272.     T = 0;
  273.     long i = 0;
  274.     int j = 0;
  275.     Serial.println("Tempo \t Volts");
  276.     while(T <= 1000 * Var[1][0]){
  277.      
  278.       T = millis() - LT;
  279.    
  280.     float time = T/1000.0000;
  281.    
  282.       if(T >= (i * Var[1][1])){
  283.         i++;
  284.          adc0.setMultiplexer(ADS1115_MUX_P0_NG);
  285.          adc0.triggerConversion();
  286.          volts = (adc0.getMilliVolts(true))/1000.0000;
  287.        
  288.         Serial.print(time,4);
  289.         Serial.print("\t");
  290.         Serial.println(volts,4);
  291.         j++;
  292.         if(j == 5){
  293.         lcd.clear();
  294.         lcd.print("*Voltagem*");
  295.         lcd.setCursor(2,1);
  296.         lcd.print(volts);
  297.         j = 0;
  298.         }
  299.        
  300.        Keys();
  301.        if(key == 4 || time >= Var[1][0]){
  302.         P[t] = 0;
  303.         change = 1;
  304.         T = 1001 * Var[1][0];
  305.        }
  306.       }
  307.     }
  308.   }
  309.  
  310.  
  311.   //medida do pico de tensão
  312.   if(P[x] == 2){
  313.     Serial.println("Pico em volts");
  314.     LV = 0.0000;
  315.     for(int j = 0; j <= 60; j++){
  316.      
  317.        if(volts > LV){
  318.         LV = volts;
  319.         Serial.println(LV,4);
  320.         lcd.clear();
  321.         lcd.print("*Voltagem*");
  322.         lcd.setCursor(2,1);
  323.         lcd.print(LV);
  324.        }
  325.        V = 0;
  326.      
  327.     for(int i = 0; i<= 100; i++){
  328.       Keys();
  329.        if(key == 4){
  330.         P[t] = 0;
  331.         change = 1;
  332.         j = 60;
  333.         i = 100;
  334.       }
  335.        delay(10);
  336.         }
  337.          adc0.setMultiplexer(ADS1115_MUX_P0_NG);
  338.          adc0.triggerConversion();
  339.          volts = (adc0.getMilliVolts(true))/1000.0000;
  340.     }
  341.    
  342.  }
  343.  
  344.     lcd.clear();
  345.     Menu();
  346.     lcd.setCursor(0,1);
  347.    
  348.   if(key != 4){
  349.    lcd.print(menu[2][2]);
  350.    Serial.println(menu[2][2]);
  351.  }
  352.   if(key == 4){
  353.   lcd.print(menu[2][3]);
  354.   Serial.println("Cancelado!");
  355.  }
  356. }
  357.  
  358. //Desliga automaticamente o sistema
  359. void autoShutdown(){
  360.   shutD = shutD + 1;
  361.   if(shutD > 200){
  362.     lcd.clear();
  363.     lcd.print( "<>Standby mode<>");
  364.      lcd.setBacklight(LOW);
  365.     while(key == 0){
  366.       Keys();
  367.       delay(100);
  368.     }
  369.      shutD = 0;
  370.      change = 1;
  371.      lcd.setBacklight(HIGH);
  372.    delay(200);
  373.   }
  374.   }
  375.  
  376. //Acelera a repetição automática de mudança da tecla
  377. void accel(){
  378.   if(key == lastkey && key != 0){
  379.     count++;
  380.   }
  381.   if(key == 0){
  382.     count = 0;
  383.   }
  384.   if(count <= 10){
  385.     delay(100);
  386.   }
  387.   if(count > 10){
  388.     delay(10);
  389.   }
  390.   lastkey = key;
  391. }
  392. void loop(){
  393.   autoShutdown();
  394.   Menu();
  395.   VarChange();
  396.  
  397. if(f[key] == 4 && P[p] == 2){
  398. function();
  399. }
  400. Keys();
  401. accel();
  402. Actions();
  403. }
Advertisement
Add Comment
Please, Sign In to add comment