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);
}
}