# switch case with timer help

**URL:** <https://forum.arduino.cc/t/switch-case-with-timer-help/472244>\
**Category:** General Guidance\
**Created:** [July 26, 2017, 6:57pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244 "2017-07-26T18:57:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 6:57pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/1 "2017-07-26T18:57:10Z")

</div>

Hi

Im making myself a arduino watering system, and i ran into some small problems. the code works just fine and doing what i want. Except one thing, if i want to interupt/stop the current state. How can i do this?

Here is my code

```cpp
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial bluetooth(3, 4); // RX, TX
const int relayPin =  13;
unsigned long time = millis();
char bluetoothData; // the data given from mobile app
boolean newData = false;

void setup() {
  bluetooth.begin(9600);
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600); //uncomment for debugging
}

void loop() {

  recvOneChar();
  controlWater();
}

void recvOneChar() {

  if (bluetooth.available() > 0) {
    bluetoothData = bluetooth.read();
    newData = true;
  }
}

void controlWater() {
  if (newData == true) {

    switch (bluetoothData) {

      case '1': { // Start watering

          digitalWrite(13, HIGH);  // turn the water on
          Serial.println("Start watering");

        }
        break;

      case '2': { //Stop watering
          Serial.println("Stop watering");
          digitalWrite(13, LOW);    // turn the water off
        }
        break;

      case '3': { //Water for 1 hours
          Serial.println("Water for 1 hours");
          digitalWrite(13, HIGH);  // turn the water on
          delay(3600000);              // wait for x second
          digitalWrite(13, LOW);    // turn the water off

        }
        break;

      case '4': { //Water for 2hours
          Serial.println("Water for 2 hours");
          digitalWrite(13, HIGH);  // turn the water on
          delay(7200000);              // wait for x second
          digitalWrite(13, LOW);    // turn the water off

        }
        break;

      case '5': { //Water for 3hours
          Serial.println("Water for 3 hours");
          digitalWrite(13, HIGH);  // turn the water on
          delay(10800000);              // wait for x second
          digitalWrite(13, LOW);    // turn the water off
        }
        break;

    }
    newData = false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 7:00pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/2 "2017-07-26T19:00:04Z")

</div>

"Except one thing, if i want to interupt/stop the current state."

delay(10800000);

Then don't use delay()

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 7:02pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/3 "2017-07-26T19:02:56Z")

</div>

Can you follow this?

```cpp
/*
  GetRidOfDelay.ino
  LarryD
  Version YY/MM/DD
  1.00    17/02/01  Running code
 
*/
 
//LED wiring:
// +5V---220 ohm resistor---LED anode---LED cathode---Arduino pin
 
const byte heartBeatLED            = 13;    //used to see if the sketch is blocking
const unsigned long heartBeatDelay = 100UL;
const byte myLED                  = 12;    //wired so a LOW turns the LED on
 
//SRAM variables
unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long myLEDmillis;
unsigned long waitTime = 0;
unsigned long currentMicros;
 
byte LEDstate = 0;
 
//                          s e t u p ( )
// **********************************************************************
void setup()
{
  pinMode(heartBeatLED, OUTPUT);
 
  pinMode(myLED, OUTPUT);
  digitalWrite(myLED, HIGH);        //HIGH is LED off
 
} //                E n d  o f  s e t u p ( )
 
//                          l o o p ( )
// **********************************************************************
void loop()
{
  currentMillis = millis(); //for milli second timing
  currentMicros = micros(); //for mirco second timing
 
  // ***************************
  //HeartBeat LED, should toggle every heartBeatDelay milliseconds if code is nonblocking
  if (currentMillis - heartBeatMillis >= heartBeatDelay)
  {
    heartBeatMillis = heartBeatMillis + heartBeatDelay; //reset timing
 
    //Toggle heartBeatLED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }
 
  // ***************************
  //swap the comment marks // on the next two lines to see a comparison
 
  //usingDelay();
  usingMillis();
 
 
} //                    E n d  o f  l o o p ( )
 
//======================================================================
//                        F U N C T I O N S
//======================================================================
 
//                    u s i n g D e l a y ( )
// **********************************************************************
void usingDelay()
{
  digitalWrite(myLED, LOW);  //LOW  is LED on
  delay(2000);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(1000);
  digitalWrite(myLED, LOW);  //LOW  is LED on
  delay(500);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(250);
 
}//                E n d  o f  u s i n g D e l a y ( )
 
//                    u s i n g M i l l i s ( )
// **********************************************************************
void usingMillis()
{
  //is it time to process the next state code?
  if (currentMillis - myLEDmillis < waitTime)
  {
    //No, it is not time
    return;
  }
 
  //Yes, it is now time
  myLEDmillis = currentMillis;    //reset timing
 
  //state code
  switch (LEDstate)
  {
    case 0:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 1;                //next state
      waitTime = 2000UL;          //setup the wait value
      break;
 
    case 1:
      digitalWrite(myLED, HIGH);  //turn LED off
      LEDstate = 2;                //next state
      waitTime = 1000UL;          //setup the wait value
      break;
 
    case 2:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 3;                //next state      
      waitTime = 500UL;            //setup the wait value
      break;
 
    case 3:
      digitalWrite(myLED, HIGH);  //turn LED off
      LEDstate = 0;                //next state
      waitTime = 250UL;            //setup the wait value
      break;
 
  } //End of switch/case
 
}//                E n d  o f  u s i n g M i l l i s ( )
 
//======================================================================
//                        E N D  O F  C O D E
//======================================================================

```

.

---

<div class="post-metadata">

**Author:** ![INTP](https://avatars.discourse-cdn.com/v4/letter/i/a183cd/32.png) [@INTP](https://forum.arduino.cc/u/INTP)\
**Post date:** [July 26, 2017, 7:32pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/4 "2017-07-26T19:32:03Z")

</div>

I see an unsigned long time variable declared but not used, did you copy code and then pared it down to reduce functionality and threw in delay yourself?

---

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 7:35pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/5 "2017-07-26T19:35:23Z")

</div>

> INTP:  
> I see an unsigned long time variable declared but not used, did you copy code and then pared it down to reduce functionality and threw in delay yourself?

I tried to use some timers, but i dint get it to work as i wanted. Just forgot to delete it.

---

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 7:42pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/6 "2017-07-26T19:42:14Z")

</div>

> larryd:  
> "Except one thing, if i want to interupt/stop the current state."
> 
> delay(10800000);
> 
> Then don't use delay()

So something like this? I tried on case 3, just for checking.

```arduino
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial bluetooth(3, 4); // RX, TX
const int relayPin =  13;
unsigned long currentMillis;
unsigned long mywatermillis;
char bluetoothData; // the data given from mobile app
boolean newData = false;

void setup() {
  bluetooth.begin(9600);
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600); //uncomment for debugging
}

void loop() {

  currentMillis = millis(); //for milli second timing
  recvOneChar();
  controlWater();
}

void recvOneChar() {

  if (bluetooth.available() > 0) {
    bluetoothData = bluetooth.read();
    newData = true;
  }
}

void controlWater() {
  if (newData == true) {

    switch (bluetoothData) {

      case '1': { // Start watering

          digitalWrite(13, HIGH);   // turn the water on
          // wait for a second
          Serial.println("Start watering");

        }
        break;

      case '2': { //Stop watering
          Serial.println("Stop watering");
          digitalWrite(13, LOW);    // turn the water off
        }
        break;

      case '3': { //Water for 1 hours

          Serial.println("Water for 1 hours");
          digitalWrite(13, HIGH);   // turn the water on
          //is it time to process the next state code?
          if (currentMillis - mywatermillis < 5000)
          {
            //No, it is not time
            return;
          }

          //Yes, it is now time
          digitalWrite(13, LOW);    // turn the water off
          Serial.println("Done watering");
          mywatermillis = currentMillis;     //reset timing

        }
        break;

      case '4': { //Water for 2hours
          Serial.println("Water for 2 hours");
          digitalWrite(13, HIGH);   // turn the water on
          delay(7200000);              // wait for x second
          digitalWrite(13, LOW);    // turn the water off

        }
        break;

      case '5': { //Water for 3hours
          Serial.println("Water for 3 hours");
          digitalWrite(13, HIGH);   // turn the water on
          delay(10800000);              // wait for x second
          digitalWrite(13, LOW);    // turn the water off
        }
        break;

    }
    newData = false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![INTP](https://avatars.discourse-cdn.com/v4/letter/i/a183cd/32.png) [@INTP](https://forum.arduino.cc/u/INTP)\
**Post date:** [July 26, 2017, 7:50pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/7 "2017-07-26T19:50:19Z")

</div>

You may need to ditch the switch case since you'll need to run condition checks to use timing. I'm not sure what the limitations are within switch case, I could be wrong. But going to if statements might be more intuitive for letting the program loop and check instead of following the case and breaking each time.

To time, you set a variable such as startTime, when the pump turns on, equal to millis() that always returns the current count. Then your program is allowed to run until you interrupt it or if an interval variable such as elapsedTime, which will equal millis() minus startTime exceeds whatever duration you wanted- the number currently in delay.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 7:59pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/8 "2017-07-26T19:59:50Z")

</div>

You are getting close.

Can you follow this part of the sketch?

```cpp
void usingMillis()
{
  //is it time to process the next state code?
  if (currentMillis - myLEDmillis < waitTime)
  {
    //No, it is not time
    return;
  }
 
  //Yes, it is now time
  myLEDmillis = currentMillis;     //reset timing
 
  //state code
  switch (LEDstate)
  {
    case 0:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 1;                //next state
      waitTime = 2000UL;           //setup the wait value
      break;
 
    case 1:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 2;                //next state
      waitTime = 1000UL;           //setup the wait value
      break;
 
    case 2:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 3;                //next state      
      waitTime = 500UL;            //setup the wait value
      break;
 
    case 3:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 0;                //next state
      waitTime = 250UL;            //setup the wait value
      break;
 
  } //End of switch/case
 
}//                E n d  o f  u s i n g M i l l i s ( )

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 8:25pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/9 "2017-07-26T20:25:17Z")

</div>

This should be close:

~~My mistake, changes coming.~~

Phone call, I think this will work.

Do you understand how it works?

```cpp
void controlWater()
{
  if (newData == true)
  {
    //cancel the timer as a new command has come in
    waitTime = 0;
    newData = false;
  }

  //is it time to process the next state code?
  if (currentMillis - myLEDmillis < waitTime)
  {
    //No, it is not time
    return;
  }

  switch (bluetoothData)
  {
    case '1':
      {
        // Start watering

        digitalWrite(13, HIGH);   // turn the water on
        Serial.println("Start watering");
        waitTime = 0;
      }
      break;

    case '2':
      {
        //Stop watering
        Serial.println("Stop watering");
        digitalWrite(13, LOW);    // turn the water off
        waitTime = 0;
      }
      break;

    case '3':
      {
        //Water for 1 hour
        Serial.println("Water for 1 hour");
        waitTime = 60 * 60 * 1000ul;
        digitalWrite(13, HIGH);   // turn the water on
        //delay(3600000);              // wait for x second
        //digitalWrite(13, LOW);    // turn the water off

        //going to stop watering after time out
        bluetoothData = 2;
      }
      break;

    case '4':
      {
        //Water for 2 hours
        Serial.println("Water for 2 hours");
        waitTime = 2 * 60 * 60 * 1000ul;
        digitalWrite(13, HIGH);   // turn the water on
        //delay(7200000);              // wait for x second
        //digitalWrite(13, LOW);    // turn the water off

        //going to stop watering after time out
        bluetoothData = 2;
      }
      break;

    case '5':
      {
        //Water for 3hours
        Serial.println("Water for 3 hours");
        digitalWrite(13, HIGH);   // turn the water on
        waitTime = 3 * 60 * 60 * 1000ul;
        //delay(10800000);              // wait for x second
        //digitalWrite(13, LOW);    // turn the water off

        //going to stop watering after time out
        bluetoothData = 2;
      }
      break;
      
  } //END of switch/case
  
} //END of function

```

---

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 9:18pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/10 "2017-07-26T21:18:14Z")

</div>

> larryd:  
> "Except one thing, if i want to interupt/stop the current state."
> 
> delay(10800000);
> 
> Then don't use delay()

So something like this? I tried on case 3, just for checking.

```arduino
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial bluetooth(3, 4); // RX, TX
const int relayPin =  13;
unsigned long currentMillis;
unsigned long mywatermillis;
char bluetoothData; // the data given from mobile app
boolean newData = false;

void setup() {
  bluetooth.begin(9600);
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600); //uncomment for debugging
}

void loop() {

  currentMillis = millis(); //for milli second timing
  recvOneChar();
  controlWater();
}

void recvOneChar() {

  if (bluetooth.available() > 0) {
    bluetoothData = bluetooth.read();
    newData = true;
  }
}

void controlWater() {
  if (newData == true) {

    switch (bluetoothData) {

      case '1': { // Start watering

          digitalWrite(13, HIGH);   // turn the water on
          // wait for a second
          Serial.println("Start watering");

        }
        break;

      case '2': { //Stop watering
          Serial.println("Stop watering");
          digitalWrite(13, LOW);    // turn the water off
        }
        break;

      case '3': { //Water for 1 hours

          Serial.println("Water for 1 hours");
          digitalWrite(13, HIGH);   // turn the water on
          //is it time to process the next state code?
          if (currentMillis - mywatermillis < 5000)
          {
            //No, it is not time
            return;
          }

          //Yes, it is now time
          digitalWrite(13, LOW);    // turn the water off
          Serial.println("Done watering");
          mywatermillis = currentMillis;     //reset timing

        }
        break;

      case '4': { //Water for 2hours
          Serial.println("Water for 2 hours");
          digitalWrite(13, HIGH);   // turn the water on
          delay(7200000);              // wait for x second
          digitalWrite(13, LOW);    // turn the water off

        }
        break;

      case '5': { //Water for 3hours
          Serial.println("Water for 3 hours");
          digitalWrite(13, HIGH);   // turn the water on
          delay(10800000);              // wait for x second
          digitalWrite(13, LOW);    // turn the water off
        }
        break;

    }
    newData = false;
  }
}

```

> larryd:  
> This should be close:
> 
> ~~My mistake, changes coming.~~
> 
> Phone call, I think this will work.
> 
> Do you understand how it works?
> 
> ```arduino
> void controlWater()
> 
> ```

{  
&nbsp; if (newData == true)  
&nbsp; {  
&nbsp; &nbsp; //cancel the timer as a new command has come in  
&nbsp; &nbsp; waitTime = 0;  
&nbsp; &nbsp; newData = false;  
&nbsp; }

//is it time to process the next state code?  
&nbsp; if (currentMillis - myLEDmillis \< waitTime)  
&nbsp; {  
&nbsp; &nbsp; //No, it is not time  
&nbsp; &nbsp; return;  
&nbsp; }

switch (bluetoothData)  
&nbsp; {  
&nbsp; &nbsp; case '1':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; // Start watering

digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Start watering");  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 0;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '2':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Stop watering  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Stop watering");  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, LOW);&nbsp; &nbsp; // turn the water off  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 0;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '3':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 1 hour  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 1 hour");  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 60 \* 60 \* 1000ul;  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; //delay(3600000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // wait for x second  
&nbsp; &nbsp; &nbsp; &nbsp; //digitalWrite(13, LOW);&nbsp; &nbsp; // turn the water off

//going to stop watering after time out  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = 2;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '4':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 2 hours  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 2 hours");  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 2 \* 60 \* 60 \* 1000ul;  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; //delay(7200000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // wait for x second  
&nbsp; &nbsp; &nbsp; &nbsp; //digitalWrite(13, LOW);&nbsp; &nbsp; // turn the water off

//going to stop watering after time out  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = 2;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '5':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 3hours  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 3 hours");  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 3 \* 60 \* 60 \* 1000ul;  
&nbsp; &nbsp; &nbsp; &nbsp; //delay(10800000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // wait for x second  
&nbsp; &nbsp; &nbsp; &nbsp; //digitalWrite(13, LOW);&nbsp; &nbsp; // turn the water off

//going to stop watering after time out  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = 2;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;  
&nbsp; &nbsp; &nbsp;  
&nbsp; } //END of switch/case  
&nbsp;  
} //END of function

> ```arduino
> 
> ```

Just tried this, but it does not turn the relayPin of after waitTime.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 9:23pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/11 "2017-07-26T21:23:07Z")

</div>

> Just tried this, but it does not turn the relayPin of after waitTime.

How did you test the time out as it is set to 1, 2, 3 hours?

.

---

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 9:23pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/12 "2017-07-26T21:23:53Z")

</div>

> larryd:  
> How did you test the time out as it is set to 1, 2, 3 hours?
> 
> .

Just changed it to 10sec

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 9:30pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/13 "2017-07-26T21:30:46Z")

</div>

Please show us your current sketch.

.

---

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 9:48pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/14 "2017-07-26T21:48:51Z")

</div>

> larryd:  
> Please show us your current sketch.
> 
> .

```arduino
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial bluetooth(3, 4); // RX, TX
const int relayPin =  13;
unsigned long currentMillis;
unsigned long myLEDmillis;
unsigned long waitTime = 0;
char bluetoothData; // the data given from mobile app
boolean newData = false;

void setup() {
  bluetooth.begin(9600);
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600); //uncomment for debugging
}

void loop() {

  currentMillis = millis(); //for milli second timing
  recvOneChar();
  controlWater();
}

void recvOneChar() {

  if (bluetooth.available() > 0) {
    bluetoothData = bluetooth.read();
    newData = true;
  }
}

void controlWater()
{
  if (newData == true)
  {
    //cancel the timer as a new command has come in
    waitTime = 0;
    newData = false;
  }

  //is it time to process the next state code?
  if (currentMillis - myLEDmillis < waitTime)
  {
    //No, it is not time
    return;
  }

  switch (bluetoothData)
  {
    case '1':
      {
        // Start watering

        digitalWrite(13, HIGH);  // turn the water on
        Serial.println("Start watering");
        waitTime = 0;
      }
      break;

    case '2':
      {
        //Stop watering
        Serial.println("Stop watering");
        digitalWrite(13, LOW);    // turn the water off
        waitTime = 0;
      }
      break;

    case '3':
      {
        //Water for 1 hour
        Serial.println("Water for 1 hour");
        waitTime = 10000ul;
        digitalWrite(13, HIGH);  // turn the water on
        //delay(3600000);              // wait for x second
        //digitalWrite(13, LOW);    // turn the water off

        Serial.println("Watering done");
        //stop watering
        bluetoothData = 2;
      }
      break;

    case '4':
      {
        //Water for 2 hours
        Serial.println("Water for 2 hours");
        waitTime = 2 * 60 * 60 * 1000ul;
        digitalWrite(13, HIGH);  // turn the water on
        //delay(7200000);              // wait for x second
        //digitalWrite(13, LOW);    // turn the water off

        //stop watering
        bluetoothData = 2;
      }
      break;

    case '5':
      {
        //Water for 3hours
        Serial.println("Water for 3 hours");
        digitalWrite(13, HIGH);  // turn the water on
        waitTime = 3 * 60 * 60 * 1000ul;
        //delay(10800000);              // wait for x second
        //digitalWrite(13, LOW);    // turn the water off

        //stop watering
        bluetoothData = 2;
      }
      break;

  } //END of switch/case

} //END of function

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 9:54pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/15 "2017-07-26T21:54:15Z")

</div>

A few problems 8)

bluetoothData = 2;  
Should have been:  
bluetoothData = '2';

Not able to check this here, but try:

```cpp
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial bluetooth(3, 4); // RX, TX
const int relayPin =  13;
unsigned long currentMillis;
unsigned long myRelayMillis;
unsigned long waitTime = 0;
char bluetoothData; // the data given from mobile app
boolean newData = false;

void setup() {
  bluetooth.begin(9600);
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600); //uncomment for debugging
}

void loop() {

  currentMillis = millis(); //for milli second timing
  
  recvOneChar();
  controlWater();
}

void recvOneChar() {

  if (bluetooth.available() > 0) {
    bluetoothData = bluetooth.read();
    newData = true;
  }
}

void controlWater()
{
  if (newData == true)
  {
    //cancel the timer as a new command has come in
    waitTime = 0;
    newData = false;
  }

  //is it time to process the next state code?
  if (currentMillis - myRelayMillis < waitTime)
  {
    //No, it is not time
    return;
  }

  switch (bluetoothData)
  {
    case '1':
      {
        // Start watering

        digitalWrite(13, HIGH);   // turn the water on
        Serial.println("Start watering");
        myRelayMillis = millis();
        waitTime = 0;
      }
      break;

    case '2':
      {
        //Stop watering
        Serial.println("Stop watering");
        digitalWrite(13, LOW);    // turn the water off
        myRelayMillis = millis();
        waitTime = 0;
      }
      break;

    case '3':
      {
        //Water for 1 hour
        Serial.println("Water for 1 hour");
        myRelayMillis = millis();
        waitTime = 10000ul;
        digitalWrite(13, HIGH);   // turn the water on
        Serial.println("Watering done");
        
        //stop watering
        bluetoothData = '2';
      }
      break;

    case '4':
      {
        //Water for 2 hours
        Serial.println("Water for 2 hours");
        myRelayMillis = millis();
        waitTime = 2 * 60 * 60 * 1000ul;
        digitalWrite(13, HIGH);   // turn the water on

        //stop watering
        bluetoothData = '2';
      }
      break;

    case '5':
      {
        //Water for 3hours
        Serial.println("Water for 3 hours");
        digitalWrite(13, HIGH);   // turn the water on
        myRelayMillis = millis();
        waitTime = 3 * 60 * 60 * 1000ul;

        //stop watering
        bluetoothData = '2';
      }
      break;

  } //END of switch/case

} //END of function

```

---

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 10:06pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/16 "2017-07-26T22:06:00Z")

</div>

> larryd:  
> A few problems 8)
> 
> bluetoothData = 2;  
> Should have been:  
> bluetoothData = '2';
> 
> Not able to check this here, but try:
> 
> ```arduino
> #include <SoftwareSerial.h>// import the serial library
> 
> ```

SoftwareSerial bluetooth(3, 4); // RX, TX  
const int relayPin =&nbsp; 13;  
unsigned long currentMillis;  
unsigned long myRelayMillis;  
unsigned long waitTime = 0;  
char bluetoothData; // the data given from mobile app  
boolean newData = false;

void setup() {  
&nbsp; bluetooth.begin(9600);  
&nbsp; pinMode(relayPin, OUTPUT);  
&nbsp; Serial.begin (9600); //uncomment for debugging  
}

void loop() {

currentMillis = millis(); //for milli second timing  
&nbsp;  
&nbsp; recvOneChar();  
&nbsp; controlWater();  
}

void recvOneChar() {

if (bluetooth.available() \> 0) {  
&nbsp; &nbsp; bluetoothData = bluetooth.read();  
&nbsp; &nbsp; newData = true;  
&nbsp; }  
}

void controlWater()  
{  
&nbsp; if (newData == true)  
&nbsp; {  
&nbsp; &nbsp; //cancel the timer as a new command has come in  
&nbsp; &nbsp; waitTime = 0;  
&nbsp; &nbsp; newData = false;  
&nbsp; }

//is it time to process the next state code?  
&nbsp; if (currentMillis - myRelayMillis \< waitTime)  
&nbsp; {  
&nbsp; &nbsp; //No, it is not time  
&nbsp; &nbsp; return;  
&nbsp; }

switch (bluetoothData)  
&nbsp; {  
&nbsp; &nbsp; case '1':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; // Start watering

digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Start watering");  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 0;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '2':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Stop watering  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Stop watering");  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, LOW);&nbsp; &nbsp; // turn the water off  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 0;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '3':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 1 hour  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 1 hour");  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 10000ul;  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Watering done");  
&nbsp; &nbsp; &nbsp; &nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; //stop watering  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = '2';  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '4':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 2 hours  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 2 hours");  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 2 \* 60 \* 60 \* 1000ul;  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on

//stop watering  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = '2';  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '5':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 3hours  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 3 hours");  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 3 \* 60 \* 60 \* 1000ul;

//stop watering  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = '2';  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

} //END of switch/case

} //END of function

> ```arduino
> 
> ```

just tried it, but same as before. Does not stop timer.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 10:21pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/17 "2017-07-26T22:21:46Z")

</div>

Again, I cannot check this, but try try again:

```cpp
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial bluetooth(3, 4); // RX, TX
const int relayPin =  13;
unsigned long currentMillis;
unsigned long myRelayMillis;
unsigned long waitTime = 0;
char bluetoothData; // the data given from mobile app
boolean newData = false;

void setup()
{
  bluetooth.begin(9600);
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600); //uncomment for debugging
}

void loop()
{

  currentMillis = millis(); //for milli second timing

  recvOneChar();
  controlWater();
}

void recvOneChar()
{
  if (bluetooth.available() > 0)
  {
    bluetoothData = bluetooth.read();
    newData = true;
  }
}

void controlWater()
{
  if (newData == true)
  {
    //cancel the timer as a new command has come in
    waitTime = 0;
    newData = false;
    myRelayMillis = millis();
  }

  //is it time to process the next state code?
  if (currentMillis - myRelayMillis < waitTime)
  {
    //No, it is not time
    return;
  }

  switch (bluetoothData)
  {
    case '1':
      {
        // Start watering

        digitalWrite(13, HIGH);  // turn the water on
        Serial.println("Start watering");

        waitTime = 5000;
      }
      break;

    case '2':
      {
        //Stop watering when timing is over
        Serial.println("Stop watering");
        digitalWrite(13, LOW);    // turn the water off

        waitTime = 5000;
      }
      break;

    case '3':
      {
        //Water for 1 hour
        Serial.println("Water for 1 hour");
        myRelayMillis = millis();
        waitTime = 10000ul;
        digitalWrite(13, HIGH);  // turn the water on

        //Stop watering when timing is over
        bluetoothData = '2';
      }
      break;

    case '4':
      {
        //Water for 2 hours
        Serial.println("Water for 2 hours");
        myRelayMillis = millis();
        waitTime = 2 * 60 * 60 * 1000ul;
        digitalWrite(13, HIGH);  // turn the water on

        //Stop watering when timing is over
        bluetoothData = '2';
      }
      break;

    case '5':
      {
        //Water for 3hours
        Serial.println("Water for 3 hours");
        digitalWrite(13, HIGH);  // turn the water on
        myRelayMillis = millis();
        waitTime = 3 * 60 * 60 * 1000ul;

        //Stop watering when timing is over
        bluetoothData = '2';
      }
      break;

  } //END of switch/case

} //END of function

```

---

<div class="post-metadata">

**Author:** ![Memnon](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Memnon](https://forum.arduino.cc/u/Memnon)\
**Post date:** [July 26, 2017, 10:37pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/18 "2017-07-26T22:37:23Z")

</div>

> larryd:  
> Again, I cannot check this, but try try again:
> 
> ```arduino
> #include <SoftwareSerial.h>// import the serial library
> 
> ```

SoftwareSerial bluetooth(3, 4); // RX, TX  
const int relayPin =&nbsp; 13;  
unsigned long currentMillis;  
unsigned long myRelayMillis;  
unsigned long waitTime = 0;  
char bluetoothData; // the data given from mobile app  
boolean newData = false;

void setup()  
{  
&nbsp; bluetooth.begin(9600);  
&nbsp; pinMode(relayPin, OUTPUT);  
&nbsp; Serial.begin (9600); //uncomment for debugging  
}

void loop()  
{

currentMillis = millis(); //for milli second timing

recvOneChar();  
&nbsp; controlWater();  
}

void recvOneChar()  
{  
&nbsp; if (bluetooth.available() \> 0)  
&nbsp; {  
&nbsp; &nbsp; bluetoothData = bluetooth.read();  
&nbsp; &nbsp; newData = true;  
&nbsp; }  
}

void controlWater()  
{  
&nbsp; if (newData == true)  
&nbsp; {  
&nbsp; &nbsp; //cancel the timer as a new command has come in  
&nbsp; &nbsp; waitTime = 0;  
&nbsp; &nbsp; newData = false;  
&nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; }

//is it time to process the next state code?  
&nbsp; if (currentMillis - myRelayMillis \< waitTime)  
&nbsp; {  
&nbsp; &nbsp; //No, it is not time  
&nbsp; &nbsp; return;  
&nbsp; }

switch (bluetoothData)  
&nbsp; {  
&nbsp; &nbsp; case '1':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; // Start watering

digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Start watering");

waitTime = 5000;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '2':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Stop watering when timing is over  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Stop watering");  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, LOW);&nbsp; &nbsp; // turn the water off

waitTime = 5000;  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '3':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 1 hour  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 1 hour");  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 10000ul;  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on

//Stop watering when timing is over  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = '2';  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '4':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 2 hours  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 2 hours");  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 2 \* 60 \* 60 \* 1000ul;  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on

//Stop watering when timing is over  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = '2';  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

case '5':  
&nbsp; &nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; //Water for 3hours  
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("Water for 3 hours");  
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);&nbsp; // turn the water on  
&nbsp; &nbsp; &nbsp; &nbsp; myRelayMillis = millis();  
&nbsp; &nbsp; &nbsp; &nbsp; waitTime = 3 \* 60 \* 60 \* 1000ul;

//Stop watering when timing is over  
&nbsp; &nbsp; &nbsp; &nbsp; bluetoothData = '2';  
&nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; break;

} //END of switch/case

} //END of function

> ```arduino
> 
> ```

Not working 😭

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 10:39pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/19 "2017-07-26T22:39:02Z")

</div>

I think we are close, I'll power up the equipment and try things here.  
Hold on. . . .

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 26, 2017, 10:57pm UTC](https://forum.arduino.cc/t/switch-case-with-timer-help/472244/20 "2017-07-26T22:57:51Z")

</div>

Made some cosmetic changes.  
However, I tried it here, things work with using simple button switches to change the cases as I don't have a Bluetooth module right now.

Try this again, make sure you upload it to the Arduino.

```cpp
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial bluetooth(3, 4); // RX, TX
const int relayPin =  13;
unsigned long currentMillis;
unsigned long myRelayMillis;
unsigned long waitTime = 0;
char bluetoothData; // the data given from mobile app
boolean newData = false;

// ***************************************************
void setup()
{
  bluetooth.begin(9600);
  pinMode(relayPin, OUTPUT);
  Serial.begin (9600); //uncomment for debugging
}

// ***************************************************
void loop()
{

  currentMillis = millis(); //for milli second timing

  recvOneChar();
  controlWater();
}

// ***************************************************
void recvOneChar()
{
  if (bluetooth.available() > 0)
  {
    bluetoothData = bluetooth.read();
    newData = true;
  }
}

// ***************************************************
void controlWater()
{
  if (newData == true)
  {
    //cancel the timer as a new command has come in
    waitTime = 0;
    newData = false;
    myRelayMillis = millis();
  }

  //is it time to process the next state code?
  if (currentMillis - myRelayMillis < waitTime)
  {
    //No, it is not time
    return;
  }

  switch (bluetoothData)
  {
// *************
    case '1':
      {
        //Start watering
        digitalWrite(13, HIGH);  // turn the water on
        Serial.println("Start watering");
        myRelayMillis = millis();
        waitTime = 5000;
      }
      break;

// *************
    case '2':
      {
        //Stop watering when timing is over
        Serial.println("Stop watering");
        digitalWrite(13, LOW);    // turn the water off
        myRelayMillis = millis();
        waitTime = 5000;
      }
      break;

// *************
    case '3':
      {
        //Water for 1 hour
        Serial.println("Water for 1 hour");
        myRelayMillis = millis();
        waitTime = 5000ul;  //testing
        //waitTime = 1 * 60 * 60 * 1000ul;
        digitalWrite(13, HIGH);  // turn the water on

        //Stop watering when timing is over
        bluetoothData = '2';
      }
      break;

// *************
    case '4':
      {
        //Water for 2 hours
        Serial.println("Water for 2 hours");
        myRelayMillis = millis();
        //waitTime = 10000;  //testing
        waitTime = 2 * 60 * 60 * 1000ul;
        digitalWrite(13, HIGH);  // turn the water on

        //Stop watering when timing is over
        bluetoothData = '2';
      }
      break;

// *************
    case '5':
      {
        //Water for 3hours
        Serial.println("Water for 3 hours");
        digitalWrite(13, HIGH);  // turn the water on
        myRelayMillis = millis();
        //waitTime = 15000;  //testing
        waitTime = 3 * 60 * 60 * 1000ul;

        //Stop watering when timing is over
        bluetoothData = '2';
      }
      break;

  } //END of switch/case

} //END of function

// ***************************************************

```

[Next page](https://forum.arduino.cc/t/switch-case-with-timer-help/472244.md?page=2)
