Skip to main content
added 84 characters in body
Source Link
jsotola
  • 1.6k
  • 2
  • 13
  • 22

The code is being slowed down by the delay() function because it is a blocking function.

The delay function does not exit until delay time passes, so no other functions can run, such as sensor reading.

The simplest solution for your code may be an FSM library (finite state machine). It would look after all of the timing for you.

You could also replace the delay() functions with your own delay function, something like


void myDelay(int totalDelay) {

   for (i = 0; i < totalDelay/100; i++) {

                           // do some housekeeping here, like reading sensors

      delay(100);          // this blocks for only 1/10 of a second
   }                       // adjust the delay value if housekeeping takes a lot of time
}

The delay gets broken into a lot of small delays with time to do other things in between.

Another way is to check at the begining of loop() to see how much time has elapsed. See blinkWithoutDelay example code in the Arduino IDE.

Here is an example in a simulator that may help you.

https://wokwi.com/projects/362275278547275777

The code is being slowed down by the delay() function because it is a blocking function.

The delay function does not exit until delay time passes, so no other functions can run, such as sensor reading.

The simplest solution for your code may be an FSM library (finite state machine). It would look after all of the timing for you.

You could also replace the delay() functions with your own delay function, something like


void myDelay(int totalDelay) {

   for (i = 0; i < totalDelay/100; i++) {

                           // do some housekeeping here, like reading sensors

      delay(100);          // this blocks for only 1/10 of a second
   }
}

The delay gets broken into a lot of small delays with time to do other things in between.

Another way is to check at the begining of loop() to see how much time has elapsed. See blinkWithoutDelay example code in the Arduino IDE.

Here is an example in a simulator that may help you.

https://wokwi.com/projects/362275278547275777

The code is being slowed down by the delay() function because it is a blocking function.

The delay function does not exit until delay time passes, so no other functions can run, such as sensor reading.

The simplest solution for your code may be an FSM library (finite state machine). It would look after all of the timing for you.

You could also replace the delay() functions with your own delay function, something like


void myDelay(int totalDelay) {

   for (i = 0; i < totalDelay/100; i++) {

                           // do some housekeeping here, like reading sensors

      delay(100);          // this blocks for only 1/10 of a second
   }                       // adjust the delay value if housekeeping takes a lot of time
}

The delay gets broken into a lot of small delays with time to do other things in between.

Another way is to check at the begining of loop() to see how much time has elapsed. See blinkWithoutDelay example code in the Arduino IDE.

Here is an example in a simulator that may help you.

https://wokwi.com/projects/362275278547275777

added 1 character in body
Source Link
jsotola
  • 1.6k
  • 2
  • 13
  • 22

The code is being slowed down by the delay() function because it is a blocking function.

The delay function does not exit until delay time passes, so no other functions can run, such as sensor reading.

The simplest solution for your code may be an FSM library (finite state machine). It would look after all of the timing for you.

You could also replace the delay() functions with your own delay function, something like


void myDelay(int totalDelay) {

   for (i = 0; i < totalDelay/100; i++) {

                           // do some housekeeping here, like reading sensors

      delay(100);          // this blocks for only 1/10 of a second
   }
}

The delay gets broken into a lot of small delays with time to do other things in between.

Another way is to check at the begining of `loop()loop() to see how much time has elapsed. See blinkWithoutDelay example code in the Arduino IDE.

Here is an example in a simulator that may help you.

https://wokwi.com/projects/362275278547275777

The code is being slowed down by the delay() function because it is a blocking function.

The delay function does not exit until delay time passes, so no other functions can run, such as sensor reading.

The simplest solution for your code may be an FSM library (finite state machine). It would look after all of the timing for you.

You could also replace the delay() functions with your own delay function, something like


void myDelay(int totalDelay) {

   for (i = 0; i < totalDelay/100; i++) {

                           // do some housekeeping here, like reading sensors

      delay(100);          // this blocks for only 1/10 of a second
   }
}

The delay gets broken into a lot of small delays with time to do other things in between.

Another way is to check at the begining of `loop() to see how much time has elapsed. See blinkWithoutDelay example code in the Arduino IDE.

Here is an example in a simulator that may help you.

https://wokwi.com/projects/362275278547275777

The code is being slowed down by the delay() function because it is a blocking function.

The delay function does not exit until delay time passes, so no other functions can run, such as sensor reading.

The simplest solution for your code may be an FSM library (finite state machine). It would look after all of the timing for you.

You could also replace the delay() functions with your own delay function, something like


void myDelay(int totalDelay) {

   for (i = 0; i < totalDelay/100; i++) {

                           // do some housekeeping here, like reading sensors

      delay(100);          // this blocks for only 1/10 of a second
   }
}

The delay gets broken into a lot of small delays with time to do other things in between.

Another way is to check at the begining of loop() to see how much time has elapsed. See blinkWithoutDelay example code in the Arduino IDE.

Here is an example in a simulator that may help you.

https://wokwi.com/projects/362275278547275777

Source Link
jsotola
  • 1.6k
  • 2
  • 13
  • 22

The code is being slowed down by the delay() function because it is a blocking function.

The delay function does not exit until delay time passes, so no other functions can run, such as sensor reading.

The simplest solution for your code may be an FSM library (finite state machine). It would look after all of the timing for you.

You could also replace the delay() functions with your own delay function, something like


void myDelay(int totalDelay) {

   for (i = 0; i < totalDelay/100; i++) {

                           // do some housekeeping here, like reading sensors

      delay(100);          // this blocks for only 1/10 of a second
   }
}

The delay gets broken into a lot of small delays with time to do other things in between.

Another way is to check at the begining of `loop() to see how much time has elapsed. See blinkWithoutDelay example code in the Arduino IDE.

Here is an example in a simulator that may help you.

https://wokwi.com/projects/362275278547275777