# Code for arduino

**URL:** <https://forum.arduino.cc/t/code-for-arduino/974227>\
**Category:** Programming\
**Created:** [March 28, 2022, 12:00am UTC](https://forum.arduino.cc/t/code-for-arduino/974227 "2022-03-28T00:00:29Z")\
**Posts on this page:** 20\
**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:** [March 28, 2022, 12:00am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/1 "2022-03-28T00:00:29Z")

</div>

I need code for arduino to make led on for 6 hour then turn it off for half an hour then repeat it again to turn it on for 6 hours then turn it off for half an hour and repeat it again and again for ever

---

<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:** [March 28, 2022, 12:17am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/2 "2022-03-28T00:17:57Z")

</div>

Welcome to the group.

We can look at the sketch you have written and give you some pointers.

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [March 28, 2022, 12:26am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/3 "2022-03-28T00:26:14Z")

</div>

Good to see you!  
If you have external hardware you might post a schematic, not a frizzy picture with links to technical information on each of the hardware items.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [March 28, 2022, 12:30am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/4 "2022-03-28T00:30:08Z")

</div>

modify a blink sketch. if you find one

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [March 28, 2022, 1:26am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/5 "2022-03-28T01:26:28Z")

</div>

> [@ahmedk2](#):
>
> I need code for arduino to make led on for 6 hour then turn it off for half an hour then repeat it

```arduino
void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
}

const unsigned long SECOND = 1000;
const unsigned long MINUTE = SECOND * 60;
const unsigned long HOUR = MINUTE * 60;

void loop()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(HOUR * 6);
  digitalWrite(LED_BUILTIN, LOW);
  delay(MINUTE * 30);
}

```

---

<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:** [March 29, 2022, 12:58pm UTC](https://forum.arduino.cc/t/code-for-arduino/974227/6 "2022-03-29T12:58:04Z")

</div>

Thanks but can i have the code with millis function not delay because there is another actions i need to add work at the same time

---

<div class="post-metadata">

**Author:** ![anon46966594](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@anon46966594](https://forum.arduino.cc/u/anon46966594)\
**Post date:** [March 29, 2022, 1:59pm UTC](https://forum.arduino.cc/t/code-for-arduino/974227/7 "2022-03-29T13:59:47Z")

</div>

> [@ahmedk2](#):
>
> can i have the code with millis function

```arduino
const unsigned long SECOND = 1000;
const unsigned long MINUTE = SECOND * 60;
const unsigned long HOUR = MINUTE * 60;

const unsigned long OFF_TIME = MINUTE * 30;
const unsigned long ON_TIME = HOUR * 6;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  static uint32_t timeCapture;
  uint32_t now = millis();

  if ((now - timeCapture) >= (digitalRead(LED_BUILTIN) ? ON_TIME : OFF_TIME)) {
    timeCapture = now;
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
}

```

---

<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:** [April 9, 2022, 11:46pm UTC](https://forum.arduino.cc/t/code-for-arduino/974227/8 "2022-04-09T23:46:19Z")

</div>

#1

I need code for arduino to make relay on for 6 hour while that happening turn blue led on then turn them off for half an hour while it turned off  
Then repeat it again for ever  
P. S  
I need the code with millis not delay please  
Thx

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [April 9, 2022, 11:52pm UTC](https://forum.arduino.cc/t/code-for-arduino/974227/9 "2022-04-09T23:52:41Z")

</div>

You need help or someone to write it? Help is free, custom coding is $75/hr - UPFRONT!

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [April 10, 2022, 12:28am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/10 "2022-04-10T00:28:08Z")

</div>

Your price is very good, I go for $250.00/hr + with a 1 week minimum guarantee if I am interested.

---

<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:** [April 10, 2022, 3:33am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/11 "2022-04-10T03:33:27Z")

</div>

@ahmedk2, 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 😉

Something to read:

1. [Using millis() for timing. A beginners guide](https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573)
2. [Demonstration code for several things at the same time](https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158)
3. [pinMode() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/) and [digitalWrite() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/)

For time calculations, make sure that you use variables of type _unsigned long_.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [April 10, 2022, 4:18am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/12 "2022-04-10T04:18:33Z")

</div>

> [@ahmedk2](#):
>
> I need the code with millis not delay please

Why? 🤨

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [April 10, 2022, 5:02am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/13 "2022-04-10T05:02:15Z")

</div>

You can get a timer in any department stores'.

---

<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:** [April 10, 2022, 5:17am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/14 "2022-04-10T05:17:38Z")

</div>

Can they do multiple things "at the same time"? Or do you have to buy a few?

> [@paulpaulson](#):
>
> You can get a timer in any department stores'.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [April 10, 2022, 5:23am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/15 "2022-04-10T05:23:28Z")

</div>

That fits the description. That's all that's required, isn't it?

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [April 10, 2022, 8:22am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/16 "2022-04-10T08:22:24Z")

</div>

There appear to be two actions required here, but the actual requirement is garbled.

I have requested this cross posting to be joined to the original for clarity. (Hopefully it will remain in this section as I do not routinely look at "Programming Questions" 🥺)

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [April 10, 2022, 8:31am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/17 "2022-04-10T08:31:02Z")

</div>

@ahmedk2

TOPIC MERGED.

Could you take a few moments to [Learn How To Use The Forum](https://forum.arduino.cc/index.php?topic=710766.0).

Other general [help and troubleshooting advice can be found here.](https://forum.arduino.cc/t/please-check-the-troubleshooting-guide-before-posting-here/52209/2)  
It will help you get the best out of the forum in the future.

Please do not keep posting the same question, if you do you will be banned from posting anything else.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [April 10, 2022, 8:32am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/18 "2022-04-10T08:32:15Z")

</div>

you can adapt the blink example to do this.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [April 10, 2022, 8:32am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/19 "2022-04-10T08:32:27Z")

</div>

> [@Paul\_B](#):
>
> (Hopefully it will remain in this section as I do not routinely look at "Programming Questions" 🥺)

It's in programming questions! Partly because it's a programming question and partly because that's where the original was.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [April 10, 2022, 8:58am UTC](https://forum.arduino.cc/t/code-for-arduino/974227/20 "2022-04-10T08:58:18Z")

</div>

> [@johnerrington](#):
>
> you can adapt the blink example to do this.

If and when he returns - perhaps inside 11 days - I wanted to see whether he knows exactly **why** he does or does not need to use millis(). 🤨

[Next page](https://forum.arduino.cc/t/code-for-arduino/974227.md?page=2)
