Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <Stepper.h>
- #include <ADS1115.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- // define o número de passos e os pinos utilizados para o motor de passo
- // Stepper myStepper = Stepper(200, 8, 9, 10, 11);
- // Inicia a biblioteca LCD com o endereço 0x27 ou 0x3f; definição de matrizes, vetores e constentes
- LiquidCrystal_I2C lcd(0x27,16,02);
- ADS1115 adc0(ADS1115_DEFAULT_ADDRESS);
- 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;
- float volts = 0.0000, LV = 0.0000;
- const int f[9] = {0,3,0,1,3,2,4,1,2};
- const int g[3][9] ={{0,1,0,-1, 0,1,0,1,-1},
- {0,1,0,-1,-1,1,0,1,-1},
- {0,0,0, 0,-1,0,1,0, 0}};
- unsigned long T, LT;
- 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;
- String menu[3][4] = {{ "*---->Menu<----* ", "Medir/Desloc. ", "Medir/Tempo ", "Medir Pico " },
- { "Det. X: ", "Det. DX: ", "Det. T: ", "Det. DT: " },
- { "Start p/ medir ", "Medindo... ", "Concluido ", " Cancelado! " }};
- void setup() {
- // inicia o display e a porta serial
- lcd.init();
- Serial.begin(115200);
- //inicia o I2Cdev
- Wire.begin();
- adc0.initialize();
- adc0.setMode(ADS1115_MODE_CONTINUOUS);
- // adc0.setMode(ADS1115_MODE_SINGLESHOT);
- adc0.setRate(ADS1115_RATE_860);
- adc0.setGain(ADS1115_PGA_6P144);
- //configura a velocidade do motor [RPM]
- // myStepper.setSpeed(40);
- lcd.setBacklight(HIGH);
- V = 1;
- if(!Serial.available()){
- lcd.clear();
- lcd.print("Conecte a porta ");
- lcd.setCursor(0,1);
- lcd.print("USB e Terminal");
- }
- Serial.println("Envie algum caractere para checar a conexão");
- while(!Serial.available()){
- delay(500);
- }
- Serial.println("Conexão bem sucedida, não desconecte o cabo enquanto estiver realizando medidas. \n \n");
- change = 1;
- }
- //Limita os valores das variaveis entre 0 e o valor máximo
- void MaxMin(){
- for(int i = 0; i <= 2 ; i++){
- if (P[i] > PM[i]){
- P[i] = 0;
- }
- if (P[i] < 0){
- P[i] = PM[i];
- }
- for(int j = 0; j <= 1 && i <= 1; j++){
- if(Var[i][j] >= VarM[i][j]){
- Var[i][j] = VarM[i][j];
- }
- if(Var[i][j] < 1){
- Var[i][j] = 1;
- }
- }
- }
- for(int k = 3; k <= 4 ; k++){
- if (P[k] > PM[k]){
- P[k] = PM[k];
- }
- if (P[k] < 0){
- P[k] = 0;
- }
- }
- }
- /* Arranjo para o valor de key
- * back 4 5 6 start
- * 3 1 7
- * 8
- */
- //Transforma a tecla pressionada em um valor entre 0 e 8, exceto 2;
- void Keys(){
- change = 0;
- key =(analogRead(A0) + 10)/128;
- if(key != 0){
- shutD =0;
- }
- switch(key){
- case 2: key = 7; break;
- case 3: key = 8; break;
- case 8: key = 4; break;
- case 7: key = 4; break;
- case 4: key = 3; break;
- case 1: key = 5; break;
- case 5: key = 1; break;
- }
- }
- //Resolve as teclas
- void Actions(){
- change = 1;
- if(f[key] == 0 || g[P[p]][key] == 0){
- change = 0;
- }
- if(f[key] == 1){
- P[P[p]] += g[P[p]][key];
- }
- if(f[key] == 2 && P[p] == 1){
- Var[P[x]][P[y]] += g[P[p]][key];
- change = 2;
- }
- if(f[key] == 3){
- if(P[t] != 1){
- P[p] += g[P[p]][key];
- }
- if(P[x] == 2 && P[p] > 0 && key == 4 && P[t] != 1){
- P[p] = 0;
- }
- if(key == 4){
- P[y] = 0;
- P[t] = 0;
- }
- if(P[x] == 2 && P[p] > 0 && key == 1){
- P[p] = 2;
- }
- }
- if(f[key] == 4 && P[p] == 2){
- P[t] += g[P[p]][key];
- }
- MaxMin();
- }
- //Imprime os valores na tela
- void Menu(){
- if(change == 1){
- Update();
- lcd.clear();
- lcd.print(menu[0][a]);
- lcd.setCursor(0,1);
- lcd.print(menu[b][c]);
- if (P[p] == 1 && (key == 1 || key == 3 || key == 4 || key == 7) && P[x] < 2){
- change = 2;
- }
- }
- }
- //fornece os valores para o menu;
- void Update(){
- if(P[p] == 0){
- a = 0;
- }
- if(P[p] == 1 || P[p] == 2){
- a = P[x] + 1;
- }
- if(P[p] == 0){
- c = P[x] + 1;
- }
- if(P[p] == 1 && P[x] <=1){
- c = P[y] + (2*P[x]);
- }
- //if(P[p] == 1 && P[x] == 2 && key != 4){
- //P[p] = ;
- //}
- if(P[p] == 2){
- c = P[t];
- }
- b = P[p];
- }
- // muda o valor da variável na tela
- void VarChange(){
- if(change == 2){
- lcd.setCursor(9,1);
- lcd.print(" ");
- lcd.setCursor(9,1);
- lcd.print(Var[P[x]][P[y]]);
- if(P[x] == 0 && P[y] == 0){
- lcd.print("c");
- }
- for(int i = 0; i<=1; i++){
- if(P[i] == i){
- lcd.print("m");
- }
- }
- if(P[x] == 1){
- lcd.print("s");
- }
- }
- }
- void function(){
- //medida em função da distância (Dificuldades técnicas)
- if(P[x] == 0){
- lcd.clear();
- lcd.print("Atualize o prog.");
- lcd.setCursor(0,1);
- lcd.print("p/ obter a func.");
- 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");
- do{
- Keys();
- delay(100);
- }while(key == 0);
- }
- /*
- if(P[x] == 0){
- V = 0;
- steps = 10*Var[0][1]/0.89;
- lcd.clear();
- lcd.print("Posicione em 0cm");
- lcd.setCursor(0,1);
- lcd.print("e pres. <start> ");
- do{
- Keys();
- delay(30);
- }while(key != 6);
- delay(100);
- //ajusta à distância desejada e aguarda a estabilização da câmara por 120S
- myStepper.step(-(Var[0][0]*100)/0.89);
- for(int i = 0; i <= 40; i++){
- Serial.print("Distância(cm) Volts");
- lcd.clear();
- for(int j = 0; j < 3; j++){
- lcd.print("*");
- delay(200);
- }
- delay(400);
- }
- lcd.clear();
- lcd.print("Medindo...");
- for(int i = (Var[0][0]*100)/0.89; i >= 0; i -= steps){
- myStepper.step(steps);
- delay(20000);
- adc0.setMultiplexer(ADS1115_MUX_P0_NG);
- adc0.triggerConversion();
- volts = (adc0.getMilliVolts(true))/1000.0000;
- float j = (i*0.89)/10;
- Serial.print( j, 2);
- Serial.print("\t \t \t");
- Serial.println( volts, 4);
- }
- lcd.clear();
- lcd.print("Fim de medida");
- Serial.println("Concluído");
- }
- */
- // medida em função do tempo
- if(P[x] == 1){
- LT = millis();
- T = 0;
- long i = 0;
- int j = 0;
- Serial.println("Tempo \t Volts");
- while(T <= 1000 * Var[1][0]){
- T = millis() - LT;
- float time = T/1000.0000;
- if(T >= (i * Var[1][1])){
- i++;
- adc0.setMultiplexer(ADS1115_MUX_P0_NG);
- adc0.triggerConversion();
- volts = (adc0.getMilliVolts(true))/1000.0000;
- Serial.print(time,4);
- Serial.print("\t");
- Serial.println(volts,4);
- j++;
- if(j == 5){
- lcd.clear();
- lcd.print("*Voltagem*");
- lcd.setCursor(2,1);
- lcd.print(volts);
- j = 0;
- }
- Keys();
- if(key == 4 || time >= Var[1][0]){
- P[t] = 0;
- change = 1;
- T = 1001 * Var[1][0];
- }
- }
- }
- }
- //medida do pico de tensão
- if(P[x] == 2){
- Serial.println("Pico em volts");
- LV = 0.0000;
- for(int j = 0; j <= 60; j++){
- if(volts > LV){
- LV = volts;
- Serial.println(LV,4);
- lcd.clear();
- lcd.print("*Voltagem*");
- lcd.setCursor(2,1);
- lcd.print(LV);
- }
- V = 0;
- for(int i = 0; i<= 100; i++){
- Keys();
- if(key == 4){
- P[t] = 0;
- change = 1;
- j = 60;
- i = 100;
- }
- delay(10);
- }
- adc0.setMultiplexer(ADS1115_MUX_P0_NG);
- adc0.triggerConversion();
- volts = (adc0.getMilliVolts(true))/1000.0000;
- }
- }
- lcd.clear();
- Menu();
- lcd.setCursor(0,1);
- if(key != 4){
- lcd.print(menu[2][2]);
- Serial.println(menu[2][2]);
- }
- if(key == 4){
- lcd.print(menu[2][3]);
- Serial.println("Cancelado!");
- }
- }
- //Desliga automaticamente o sistema
- void autoShutdown(){
- shutD = shutD + 1;
- if(shutD > 200){
- lcd.clear();
- lcd.print( "<>Standby mode<>");
- lcd.setBacklight(LOW);
- while(key == 0){
- Keys();
- delay(100);
- }
- shutD = 0;
- change = 1;
- lcd.setBacklight(HIGH);
- delay(200);
- }
- }
- //Acelera a repetição automática de mudança da tecla
- void accel(){
- if(key == lastkey && key != 0){
- count++;
- }
- if(key == 0){
- count = 0;
- }
- if(count <= 10){
- delay(100);
- }
- if(count > 10){
- delay(10);
- }
- lastkey = key;
- }
- void loop(){
- autoShutdown();
- Menu();
- VarChange();
- if(f[key] == 4 && P[p] == 2){
- function();
- }
- Keys();
- accel();
- Actions();
- }
Advertisement
Add Comment
Please, Sign In to add comment