# Arduino code Millis function help

**URL:** <https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908>\
**Category:** General Guidance\
**Created:** [August 20, 2022, 4:04am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908 "2022-08-20T04:04:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmedk2](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@ahmedk2](https://forum.arduino.cc/u/ahmedk2)\
**Post date:** [August 20, 2022, 4:04am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/1 "2022-08-20T04:04:11Z")

</div>

hello....  
i need help with this i need it with Millis function  
i need to make the Arduino if i press a button (button1) serial print "high" after 15minitues and every 15 minutes serial print "high" and doing this for 2 hours then stop doing it till i press the button again  
and if i press another button (button2)  
serial print "high" after 10minitues and every 5 minutes serial print "high" and doing this for 6 hours then stop doing it till i press the button again

doing this using millis not delay

LIKE THIS DELAY BUT MILLIS

```
if (button1== HIGH) 

```

{  
delay(900000);  
Serial.println("high");  
delay(900000);  
Serial.println("high");  
delay(900000);  
Serial.println("high");  
delay(900000);  
Serial.println("high");  
delay(900000);  
Serial.println("high");  
delay(900000);  
Serial.println("high");  
delay(900000);  
Serial.println("high");  
delay(900000);

}

```
if (button2== HIGH) 

```

{  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);

delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);

delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);

delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
delay(600000);  
Serial.println("high");  
}

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [August 20, 2022, 4:24am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/2 "2022-08-20T04:24:06Z")

</div>

Your topic has been moved to a more suitable location on the forum. _Introductory Tutorials_ is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem 😉

Please edit your post, select all code and click the \</\> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.  
[![](https://europe1.discourse-cdn.com/arduino/original/4X/7/6/0/76068b67427cb74e7360e74c52e2dd4455071975.png) ](https://europe1.discourse-cdn.com/arduino/original/4X/7/6/0/76068b67427cb74e7360e74c52e2dd4455071975.png)

Maybe [Using millis() for timing. A beginners guide](https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573) can be of help. And there are probably a few thousand other articles on the web.

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [August 20, 2022, 4:28am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/3 "2022-08-20T04:28:48Z")

</div>

Hello

Here is an example, it will print "high" every 5 seconds after releasing the button, during 15 seconds : [t1023908.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/340483884651119186)

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [August 20, 2022, 7:18am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/4 "2022-08-20T07:18:21Z")

</div>

> [@ahmedk2](#):
>
> LIKE THIS DELAY BUT MILLIS

I hope not.  
Have you ever come across **for** loops?  
That reduces all those repeated commands to just two lines.

What advantage do you see from using millis?

It helps to post **ALL** your code, as it stands some of it is missing.

---

<div class="post-metadata">

**Author:** ![abcda](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abcda/32/1127772_2.png) [@abcda](https://forum.arduino.cc/u/abcda)\
**Post date:** [August 20, 2022, 7:46am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/5 "2022-08-20T07:46:10Z")

</div>

unsigned long startMillis;  
unsigned long currentMillis;  
const unsigned long period = 900000;  
const byte BUTTON1 = 13; // You can change  
const byte BUTTON2 = 12; // you can change  
void setup()  
{  
Serial.begin(115200);  
pinMode(BUTTON1, OUTPUT);  
pinMode(BUTTON2, OUTPUT);  
startMillis = millis();  
}

void loop()  
{  
currentMillis = millis();  
if (currentMillis - startMillis \>= period)  
{  
if (BUTTON1==HIGH) {  
digitalWrite(BUTTON1, digitalRead(BUTTON1));  
startMillis = currentMillis;  
}  
if (BUTTON2==HIGH) {  
digitalWrite(BUTTON2, digitalRead(BUTTON2));  
startMillis = currentMillis;  
}  
}

}

```cpp
kodu buraya gir veya yapıştır

```

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [August 20, 2022, 8:18am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/6 "2022-08-20T08:18:09Z")

</div>

> [@abcda](#):
>
> pinMode(BUTTON1, OUTPUT);  
> pinMode(BUTTON2, OUTPUT);

😮 🙃 🕶

---

<div class="post-metadata">

**Author:** ![red\_car](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@red\_car](https://forum.arduino.cc/u/red_car)\
**Post date:** [August 20, 2022, 8:45am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/7 "2022-08-20T08:45:58Z")

</div>

> [@ahmedk2](#):
>
> ```arduino
> if (button1== HIGH) 
> 
> ```

What do you think this does?

> [@abcda](#):
>
> digitalWrite(BUTTON1, digitalRead(BUTTON1));

What do you think this does?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [August 20, 2022, 9:33am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/8 "2022-08-20T09:33:15Z")

</div>

> [@red\_car](#):
>
> > [@ahmedk2](#):
> >
> > ```arduino
> > if (button1== HIGH) 
> > 
> > ```
> 
> What do you think this does?

He thinks it does  
if (digitalRead(BUTTON1) == HIGH)

> [@red\_car](#):
>
> > [@abcda](#):
> >
> > digitalWrite(BUTTON1, digitalRead(BUTTON1));
> 
> What do you think this does?

He thinks it does  
digitalWrite(LED1, ! digitalRead(BUTTON1)

But not having declared LED1 as an output, and totally messed up the deceleration BUTTON he is no doubt floundering.

Or maybe he doesn't know what they mean. Nor know that variable names are case sensitive. Like that button1 is not the same variable as BUTTON1

---

<div class="post-metadata">

**Author:** ![abcda](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abcda/32/1127772_2.png) [@abcda](https://forum.arduino.cc/u/abcda)\
**Post date:** [August 20, 2022, 10:48am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/11 "2022-08-20T10:48:29Z")

</div>

unsigned long startMillis;  
unsigned long currentMillis;  
const unsigned long period = 900000;  
const byte BUTTON1 = 13; // You can change  
const byte BUTTON2 = 12; // you can change  
void setup()  
{  
Serial.begin(115200);  
pinMode(BUTTON1, OUTPUT);  
pinMode(BUTTON2, OUTPUT);  
startMillis = millis();  
}

void loop()  
{  
currentMillis = millis();  
if (currentMillis - startMillis \>= period)  
{  
if (BUTTON1==HIGH) {  
Serial.println(“BUTTON1 high”);  
startMillis = currentMillis;  
}  
if (BUTTON2==HIGH) {  
Serial.println(“BUTTON2 high”);  
startMillis = currentMillis;  
}  
}

}

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [August 20, 2022, 10:59am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/12 "2022-08-20T10:59:36Z")

</div>

`<OT>`  
Sometimes I admire the audacity of the "do my homework for me" people.  
`</OT>`

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [August 20, 2022, 11:43am UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/13 "2022-08-20T11:43:13Z")

</div>

abcda you might want to use the code tags \<|\>

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [August 20, 2022, 2:07pm UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/14 "2022-08-20T14:07:30Z")

</div>

He was told so in post #2 but keeps on ignoring.

> [@pmagowan](#):
>
> abcda you might want to use the code tags \<|\>

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [August 20, 2022, 7:35pm UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/15 "2022-08-20T19:35:03Z")

</div>

@abcda [irony] just keep on posting the same old stuff with the same mistakes and ignoring everything you have been told, you will be fine [\irony]

Alternatively you could start to change the code in response to what you have been told to do and perhaps maybe you might learn something. You might even get more than 0% for your assignment, you never know what you can do if you start to engage your brain.

> [@abcda](#):
>
> if (BUTTON1==HIGH)

> [@abcda](#):
>
> const byte BUTTON1 = 13;

So as BUTTON1 can not be changed and has the value of 13 how could it not equal HIGH?

You need to read the value of a pin using the digitalRead function. However if BUTTON1 is an output there is no point in reading it.

You should give your variables meaningful names, buttons should be connected to inputs not outputs. Outputs are for things like LEDs, so give the. Appropriate names. Your BUTTON **can not** be both an input and an output, it can only be one thing.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 16, 2023, 7:35pm UTC](https://forum.arduino.cc/t/arduino-code-millis-function-help/1023908/16 "2023-02-16T19:35:33Z")

</div>

Closed on February 16, 2023, 7:35pm UTC

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