How to replace the Delay function? (avoid crashes) on arduino

Question:

Does anyone know how to override the Delay function, which causes delays, in arduino? It is that I try to measure distances with this code, but at the same time I don't want the whole system to "sleep" that time, I want to do several things at the same time measure distances and that I can continue sending orders to my system.

int Trig=4;
int Echo=3;

//Variable en la que se va a almacenar el valor correspondiente a la distancia
int Dist;

void setup() {
    pinMode (Trig, OUTPUT);
    pinMode (Echo, INPUT);
}

//Este módulo calcula y devuelve la distancia en cm.
void ultrasonido (int &Distancia){
    //Para estabilizar el valor del pin Trig se establece a LOW
    digitalWrite (Trig, LOW);
    delay(10);
    //Se lanzan los 8 pulsos
    digitalWrite (Trig, HIGH);
    delay(10);
    digitalWrite (Trig, LOW);
    Distancia= pulseIn (Echo, HIGH);
    Distancia=Distancia/58;
    Serial.println(Distancia);
    delay(100);
}

void loop() {
    ultrasonido(Dist);
    delay (250);
}

Answer:

Perhaps some of the following will help you depending on what you want to achieve in the long term or what best suits your needs:

There is no multi-threading or multi-processing . On Arduino at least as far as I know, no (hardware) threading is supported.

You can try to simulate multiple threads with some software / library.

For example:

Protothreads , although there are several:

Protothreads can help you in that task as they are extremely lightweight stackless threads designed for memory restricted systems such as small embedded systems.

(link) Protothreads http://dunkels.com/adam/pt/

Or use interruptions , especially timed ones. You can program a timer to interrupt the main routine every so often (micro seconds), in order to simulate what you are looking for by executing one or another specific routine. to do things while doing other things

(link) https://www.arduino.cc/en/Reference/Interrupts


I have looked for some code similar to the one commented to show you and this is the one that I found you can see it below with its link in the answer of a user of electronics.stackexchange.com maybe it will help you.

https://electronics.stackexchange.com/questions/67089/how-can-i-control-things-without-using-delay/67091#67091

In the example below, the LED blinks without using delay (). When Timer1 runs, the interrupt service routine (ISR) isrBlinker () is called toggling the state of the LED between on / off.

To demonstrate that other things can happen simultaneously, loop () repeatedly writes foo / bar to the serial port independent of the flashing LED.

#include "TimerOne.h" 

int led = 13;

void isrBlinker() {
  static bool on = false;
  digitalWrite( led, on ? HIGH : LOW );
  on = !on;
}

void setup() {                
  Serial.begin(9600);
  Serial.flush();
  Serial.println("Serial initialized");

  pinMode(led, OUTPUT);

  // initialize the ISR blinker
  Timer1.initialize(1000000);
  Timer1.attachInterrupt( isrBlinker );
}

void loop() {
  Serial.println("foo");
  delay(1000);
  Serial.println("bar");
  delay(1000);
}

videos that might help you

https://www.youtube.com/watch?v=oeP_NiajWME

Scroll to Top