nano, push button, 2 relays and a led. Timer problem

Hello everyone
I made a timer using switch - case that controls 2 relays an a led. For the control of timing I use 2 potentiometers and a push button. The push button counter starts with 0. When the button is pressed it goes 1,2 etc. For the values 1, 2, 3 I made a switch-case in which controls the relays and the led. When the counter is 1 and the switch uses the case 1, everything is fine. But when I push button again, the counter goes 2, but the case 2 is not working well. I think that is something wrong with the timers I made.
Can anyone help?

int PotSlowTimer = A0;
int PotFastTimer = A2;

int PowerRelay = 2;
int SpeedPotRelay = 4;
int Led = 6;
int BUTTON = 8;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

unsigned long valSlow = 0;
unsigned long valFast = 0;
unsigned long SlowSpinTime = 0;
unsigned long FastSpinTime = 0;

unsigned long lastslowTime = 0;
unsigned long lastfastTime = 0;



//byte buttonState = HIGH; // variable for reading the pushbutton status

void setup() {

  Serial.begin(9600);

  pinMode(PowerRelay, OUTPUT);
  pinMode(SpeedPotRelay, OUTPUT);
  pinMode(Led, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);

  digitalWrite(PowerRelay, LOW);
  digitalWrite(SpeedPotRelay, LOW);
  digitalWrite(Led, LOW);

}

void loop() {

valSlow = analogRead(PotSlowTimer);
valFast = analogRead(PotFastTimer);

SlowSpinTime = map(valSlow, 0, 804, 0, 10) * 60000;
FastSpinTime = map(valFast, 0, 804, 0, 10) * 60000;

//Serial.print(valFast);
//delay(1000);
//Serial.print(buttonPushCounter);
//delay(1000);
//Serial.print(buttonPushCounter, 2);
//delay(1000);

buttonState = digitalRead(BUTTON);  // read the pushbutton input pin:
if (buttonState != lastButtonState)    // compare the buttonState to its previous state
  { 
    if (buttonState == LOW)
     {
       buttonPushCounter++;
       lastslowTime = millis();
       lastfastTime = millis();
       
     }
  delay(100);
  }

  lastButtonState = buttonState;  // save the current state as the last state for next time through the loop

  
switch (buttonPushCounter){
  
  case 1:
   slowTurn();
    break; 
    
   case 2:
     slowfastTurn();
      break;

  case 3:
      fastTurn();
        break;
}
}

void slowTurn(){

   unsigned long currentslowTime = millis();
   digitalWrite(PowerRelay, HIGH);
   digitalWrite(SpeedPotRelay, HIGH);
//Serial.print(currentslowTime - lastslowTime);
//delay(1000);
    if (currentslowTime - lastslowTime >= SlowSpinTime)
     {
       digitalWrite(PowerRelay, LOW);
       digitalWrite(Led, HIGH);
     }
 
  
}

void slowfastTurn(){
  
   digitalWrite(Led, LOW);
   digitalWrite(PowerRelay, HIGH);
   digitalWrite(SpeedPotRelay, HIGH);
   
unsigned long currentslowTime2 = millis();

   if (currentslowTime2 - lastslowTime >= SlowSpinTime)
     {
      midfastTurn();
     }
}

void midfastTurn(){
  digitalWrite(Led, LOW);
  digitalWrite(PowerRelay, HIGH);
  digitalWrite(SpeedPotRelay, LOW);

  unsigned long currentfastTime = millis();
    Serial.print(currentfastTime - (SlowSpinTime + lastfastTime));
        delay(1000);
       if (currentfastTime - (SlowSpinTime + lastfastTime) >= FastSpinTime)
       {
        digitalWrite(PowerRelay, LOW);
        digitalWrite(Led, HIGH);
       }
}

void fastTurn(){
  
  unsigned long currentfastTime2 = millis();
  digitalWrite(Led, LOW);
   digitalWrite(PowerRelay, HIGH);
   digitalWrite(SpeedPotRelay, LOW);
    Serial.print(currentfastTime2 - lastfastTime);
delay(1000);
   if (currentfastTime2 - lastfastTime >= FastSpinTime)
       {
        digitalWrite(PowerRelay, LOW);
        digitalWrite(Led, HIGH);
       }
 }

I see an unholy mixture of millis() and delay() used for timing

A 1 second delay in a function named fastTurn() is particularly ironic

I used 1 sec delay for the serial printing. Is affecting somehow the code?

dimtsam:
I used 1 sec delay for the serial printing. Is affecting somehow the code?

You don't actually say what you mean by

case 2 is not working well.

I would like to recommend you making it simple by using the this button library. It supports counting, debouncing without delay. See button count example

IoT_hobbyist:
I would like to recommend you making it simple by using the this button library. It supports counting, debouncing without delay. See button count example

I dont think I have issues with the button. I think it works ok, so the counter. But I cant understand why the case 2 doesnt finish the step, as the case 1 does.

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