Switch case para control error al selecionar estados con botones

saludos, analizando ejemplos he podido crear el siguiente codigo se me ha presentado los siguientes problemas
uso 4 botones 2 para increm y drecrem , 1 para selecionar el switc case y otro para guardar los datos

el boton para seleccionar los estado dentro del switch lo hace bien, cuando seleccione el case 1 entro y puedo incrementar y decrementar al entrar dentro de 2 case puedo incrementar la variable pero no lo hace independiente del primer se mexcla los valores en el primer case es para tiempo 1 a 3 seg , mientras q el segundo case es para numero entero de 0 a 40 , lo otro es q si seleciono cualquier swich los botones igual incremen y decrem , la ideas es

selecciono case 1 increme o decreme tiempo
selecciono case 2 increme o decrem numero 0 - 40
selecciones case 3 y asi sucesivamente

y las lecturas con el boton de selecion tiene un delay de 250ms
pero las lecturas de increm y decrem van muy rapidas

alguna sugerencia para corregir

const int U_BTN = 2;
const int D_BTN = 3;
const int M_BTN = 4;
const int G_BTN = 5;

int selector = 0;

unsigned long Tolerancia_T1 = 0;
unsigned long Tolerancia_T2 = 0;

float sensor_CT1 = 0.00f; //
float sensor_CT2 = 0.00f; //
float sensor_CT3 = 0.00f; //
float sensor_CT4 = 0.00f; //


int U_BTN_State = 0;
int D_BTN_State = 0;
int M_BTN_State = 0;
int G_BTN_State = 0;

int prestate = 0;

void setup() {

  Serial.begin(9600);
  pinMode(U_BTN, INPUT);
  pinMode(D_BTN, INPUT);
  pinMode(M_BTN, INPUT);
  pinMode(G_BTN, INPUT);

}

void loop() {

  M_BTN_State = digitalRead( M_BTN);

  if (M_BTN_State == HIGH && prestate == 0) {  // Pulsador menu selector
    Serial.println(selector);
    Selector_state();
    selector++;
    delay(250);
    if (selector > 7) {
      selector = 0;
    }
    prestate = 1;
  }
  else if ( M_BTN_State == LOW ) {
    prestate = 0;
  }

  Tolerancia_V1();
  Tolerancia_V2();
  Medicion_SCT1();

}

void Tolerancia_V1() {
  U_BTN_State = digitalRead( U_BTN);
  D_BTN_State = digitalRead( D_BTN);

  if (U_BTN_State == HIGH && prestate == 0) { // Pulsador incremento
    if ( Tolerancia_T1 > 0) {
      Tolerancia_T1 -= 1000;
      Serial.println(Tolerancia_T1);
      ///
      prestate = 1;
    }
  }

  else if (D_BTN_State == HIGH && prestate == 0) {  //Pulsador decremento
    if ( Tolerancia_T1 < 3000) {
      Tolerancia_T1 += 1000;
      Serial.println(Tolerancia_T1);
      ///
      prestate = 1;
    }
  }
  else if (U_BTN_State == LOW && D_BTN_State == LOW  ) {
    prestate = 0;
  }
}

void Tolerancia_V2() {

  U_BTN_State = digitalRead( U_BTN);
  D_BTN_State = digitalRead( D_BTN);

  if (U_BTN_State == HIGH && prestate == 0) { // Pulsador incremento
    if ( Tolerancia_T2 > 0) {
      Tolerancia_T2 -= 1000;
      Serial.println(Tolerancia_T2);
      ///
      prestate = 1;
    }
  }

  else if (D_BTN_State == HIGH && prestate == 0) {  //Pulsador decremento
    if ( Tolerancia_T2 < 3000) {
      Tolerancia_T2 += 1000;
      Serial.println(Tolerancia_T2);
      ///
      prestate = 1;
    }
  }
  else if (U_BTN_State == LOW && D_BTN_State == LOW  ) {
    prestate = 0;
  }
}


void Medicion_SCT1() {

  U_BTN_State = digitalRead( U_BTN);
  D_BTN_State = digitalRead( D_BTN);

  if (U_BTN_State == HIGH && prestate == 0) { // Pulsador incremento
    if ( sensor_CT1 > 0) {
      sensor_CT1 -= .50;
      Serial.println(sensor_CT1);
      ///
      prestate = 1;
    }
  }

  else if (D_BTN_State == HIGH && prestate == 0) {  //Pulsador decremento
    if ( sensor_CT1 < 40) {
      sensor_CT1 += .50;
      Serial.println(sensor_CT1);
      ///
      prestate = 1;
    }
  }
  else if (U_BTN_State == LOW && D_BTN_State == LOW  ) {
    prestate = 0;
  }
}




void Medicion_SCT2() {

}
void Medicion_SCT3() {

}

void Medicion_SCT4() {

}

void Guardar_datos() {
  
  if (G_BTN_State == HIGH && prestate == 0) {  // pulsador guardar
    //  Serial.println();
    ////
    prestate = 1;
  }
  else if (G_BTN_State == LOW  ) {
    prestate = 0;
  }
}

void Selector_state() {
  switch (selector) {

    case 0:
      Serial.println("Case 0");
      ////
      break;

    case 1:
      Serial.println("Case 1"); //V1
      Tolerancia_V1();
      break;

    case 2:
      Serial.println("Case 2"); //V2
      Tolerancia_V2();
      break;
    case 3:
      Serial.println("Caso 3");  //CT1
      Medicion_SCT1();
      break;

    case 4:
      Serial.println ("Case 4");  //CT2
      //  Medicion_SCT2();
      break;

    case 5 :
      Serial.println("Case 5");  //CT3
      // Medicion_SCT3();
      break;

    case 6 :
      Serial.println("Case 6");  //CT4
      // Medicion_SCT4();
      break;

    case 7 :
      Serial.println("Case 7");  //Guardar datos
      //Guardar_datos();
      break;

  }
}


adjunto una imagen de como se mezclan los valores
case 1 deberia ser 1000 , 2000 , 3000
case 2 deberia se 0 , 0.50 , 1 , 1.50 asi sucesivamente


No te pondré todo el código (para no hacerte la tarea completa y que aprendas), pero esta es la función que yo uso y para lo que quieres hacer a mi me resulta muy muy útil y funcional, si alguien dice que se puede mejorar el codigo o comentan algo asi, bueno no interesa, como dicen en mi pueblo JALA Y SE ACABO:

void SeleccionVariablesPID(){//BOTONERA
  //variablesPID();
  if(digitalRead(botonSelect)==HIGH){
  if(millisActual-t_select_valor>=500){
  t_select_valor=millisActual;
  select_valor=select_valor+1;}
  }
  if(select_valor==4){
    select_valor=0;
    }
  if(digitalRead(botonArriba)==HIGH&&select_valor==1){
    if(millisActual-t_select_arribaAbajo>=500){
      t_select_arribaAbajo=millisActual;
      auxP=auxP+1;}
      }
  else if(digitalRead(botonAbajo)==HIGH&&select_valor==1){
        if(millisActual-t_select_arribaAbajo>=500){
          t_select_arribaAbajo=millisActual;
      auxP=auxP-1;}
    }
    else if(digitalRead(botonArriba)==HIGH&&select_valor==2){
       if(millisActual-t_select_arribaAbajo>=500){
         t_select_arribaAbajo=millisActual;
      auxI=auxI+0.1;}
    }
      else if(digitalRead(botonAbajo)==HIGH&&select_valor==2){
         if(millisActual-t_select_arribaAbajo>=500){
           t_select_arribaAbajo=millisActual;
        auxI=auxI-0.1;}
      }
          else if(digitalRead(botonArriba)==HIGH&&select_valor==3){
           if(millisActual-t_select_arribaAbajo>=500){
             t_select_arribaAbajo=millisActual;
          auxD=auxD+1;}
          }
              else if(digitalRead(botonAbajo)==HIGH&&select_valor==3){
               if(millisActual-t_select_arribaAbajo>=500){
                 t_select_arribaAbajo=millisActual;
              auxD=auxD-1;}
            }
                  else if(digitalRead(botonArriba)==HIGH&&select_valor==0){
               if(millisActual-t_select_arribaAbajo>=500){
                 t_select_arribaAbajo=millisActual;
              auxSetpoint=auxSetpoint+0.010;}
            }
                else if(digitalRead(botonAbajo)==HIGH&&select_valor==0){
               if(millisActual-t_select_arribaAbajo>=500){
                 t_select_arribaAbajo=millisActual;
              auxSetpoint=auxSetpoint-0.010;}
            }
}

Saludos.

Dejo ejemplo de seleccion switch que igualmente funciona ;):

void SeleccionLCD(){ //PANTALLA LCD
  if(digitalRead(botonLCD)==HIGH){
  if(millisActual-t_select_LCD>=500){
    t_select_LCD=millisActual;
    lcd.clear();
    select_LCD=select_LCD+1;}
  }
    if(select_LCD==2){
    select_LCD=0;
    }

  switch (select_LCD){
  case 1:
  SetINT=setpoint*100.0; ///PesoINT= 1234;  Estas 3 lineas trabajan el float para Enteros
  SetENT=(SetINT/100); //PesoENT=12
  SetDEC=(SetINT%1000);
  PesoINT=(Peso*100.0);
  PesoENT=(PesoINT/100);
  PesoDEC=(PesoINT%100);
  lcd.setCursor(0,0);
  lcd.print("RECETA");
  lcd.setCursor(6,1);
  lcd.print("Kg");

  lcd.setCursor(0,2);
  big.writeint(1,0,SetENT,2,true);

  lcd.setCursor(10,0);
  lcd.print("LLenado");
  lcd.setCursor(16,1);
  lcd.print("Kg");
  big.writeint(1,10,PesoENT,2,true);

  lcd.setCursor(8,3);
  lcd.print("NPS");
  break;

  case 0:
  lcd.setCursor(0,0);
  lcd.print("Receta: ");
  lcd.print(setpoint);
  lcd.setCursor(0,1);
  lcd.print("llenado: ");
  lcd.print(Peso);
  lcd.setCursor(0,2);
  lcd.print("Peso aux:");
  lcd.print(auxSetpoint);
  lcd.setCursor(0,3);
  lcd.print(porcentaje);
  lcd.print(" ");
  //lcd.print(output);
  lcd.setCursor(10,3);
  lcd.print("PID:");
  lcd.print(OCR1A);
  lcd.setCursor(14,0);
  lcd.print("Lat:");
  lcd.print(digitalRead(sw_latigo));
  break;
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.