# Arduino sketch not working (and i don't know why)

**URL:** <https://forum.arduino.cc/t/arduino-sketch-not-working-and-i-dont-know-why/1186468>\
**Category:** Programming\
**Created:** [November 7, 2023, 4:03pm UTC](https://forum.arduino.cc/t/arduino-sketch-not-working-and-i-dont-know-why/1186468 "2023-11-07T16:03:59Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [November 7, 2023, 7:30pm UTC](https://forum.arduino.cc/t/arduino-sketch-not-working-and-i-dont-know-why/1186468/15 "2023-11-07T19:30:43Z")

</div>

So here is a demo-code that shows how blinking is done non-blocking.

Non-blocking is a must to keep your code responsive to button-presses.  
An Arduino Uno or Arduino Mega has a small LED on the board.  
This LED is connected to IO-Pin 13  
This demo-code makes this onboard-led blink

```arduino
// demo-Code non-blocking timing

unsigned long MyBlinkTimer = 0; // Timer-variables MUST be of type unsigned long
const byte OnBoard_LED = 13;

byte ledState = LOW;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  pinMode(OnBoard_LED,OUTPUT);
}

void loop() {

  // check if 500 milliseconds have passed by since last time 
  // function TimePeriodIsOver resulted in "true"
  if ( TimePeriodIsOver(MyBlinkTimer,500) ) {
    // when REALLY 500 milliseconds have passed by
    if (ledState == LOW) {
      ledState = HIGH; // change to HIGH
    }
    else { // ledState is HIGH
      ledState = LOW; // change to LOW      
    }

    digitalWrite(OnBoard_LED,ledState);
  }  

}

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false; // actual TimePeriod is NOT yet over
}

```

Here as a WOWKI-Simulation

> **[non-blocking-blinking-001 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/380765580876721153)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

If you want to learn more about non-blocking timing

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017/1):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

best regards Stefan

---

_[View the full topic](https://forum.arduino.cc/t/arduino-sketch-not-working-and-i-dont-know-why/1186468)._
