How to use multithreading with arduino

7

I'm making a doorbell with arduino. This bell will be composed of:

  • 01 Arduino ONE,
  • 01 Buzzer,
  • 01 Transmitter 433 Mhz,
  • 01 Receiver 433 Mhz.

My question revolves around how I can handle the listener who will be listening the controls of the 433 Mhz control and at the same time make the treatment for that buzzer oscillates using delays.

My code is basically this one. I'm using the RFremote library I found at this link link

#include <RFremote.h>
SignalPatternParams params;
RFrecv rfrecv;

int status_prog = 0;

const int buzzer = 11;
const char *BOTAO1 = "0100100100110110110100100110110100100110110110110110100100110100100110100100110100110";
const char *BOTAO3 = "0100100100110110110100100110110100100110110110110110100100110100100100110100110100110";

void setup()
{
  Serial.begin(9600);
  delay(500);

  Serial.println("INICIADO!");

  // COMPATEC remote
  params.spaceMin = 10000;
  params.spaceMax = 15000;
  params.dotMin = 450;
  params.dotMax = 600;
  params.traceMin = 950;
  params.traceMax = 1150;
  params.skipFirst = 0;
  params.skipLast = 0;

  rfrecv = RFrecv(&params);
  rfrecv.begin();

  pinMode(buzzer,OUTPUT);

}

void loop()
{

  if (rfrecv.available()) // LISTENER DO CONTROLE - RECEBE ID DO CONTROLE/TECLA VIA FREQUENCIA 433 Mhz
  {
    if (strncmp((char*)rfrecv.cmd, BOTAO1, CMD_SIZE) == 0) { 
      status_prog = 1; // LIGA O BUZZER
    }
    if (strncmp((char*)rfrecv.cmd, BOTAO3, CMD_SIZE) == 0) {
      status_prog = 3; // DESLIGA O BUZZER
    }
  }

  if(status_prog == 1) {  // SE O BOTÃO PRESSIONADO FOR EQUIVALENTE A 1 LIGA
      tone(buzzer, 650); // ACIONA BUZZER COM A TONALIDADE 650
      delay(400);        // PAUSA POR 400 MS
      noTone(buzzer);    // DESLIGA BUZZER
      delay(400);        // PAUSA POR 400 MS
  }

  if(status_prog == 3){ 
    noTone(buzzer);      // SE O BOTÃO PRESSIONADO FOR EQUIVALENTE A 3 DESLIGA
  }

}

If I remove the delay in this part

if(status_prog == 1) {  // SE O BOTÃO PRESSIONADO FOR EQUIVALENTE A 1 LIGA
   tone(buzzer, 650); // ACIONA BUZZER COM A TONALIDADE 650
   //delay(400);        // PAUSA POR 400 MS
   //noTone(buzzer);    // DESLIGA BUZZER
   //delay(400);        // PAUSA POR 400 MS
}

The program works correctly.

I think that delay(400) ends up taking the time that the program as a whole has to listen to control. What would be the approach to take in this case? An Thread would be welcome ...

    
asked by anonymous 09.02.2015 / 02:56

2 answers

5

Arduino does not support multithreading, but I've heard of libraries that simulate it. However, the simplest is not to use delays (which actually leave the program "deaf and mute"), and to calculate whether it is time to turn the buzzer on or off based on the elapsed time since the program started running (using the millis() function). Something like this (not tested):

bool oscilando = false;
bool tocando = false
long comeco = 0;
long intervalo = 400;

void loop()
{
  unsigned long agora = millis();

  if (rfrecv.available()) // LISTENER DO CONTROLE - RECEBE ID DO CONTROLE/TECLA VIA FREQUENCIA 433 Mhz
  {
    if (strncmp((char*)rfrecv.cmd, BOTAO1, CMD_SIZE) == 0) { 
      oscilando = true;
      tocando = false;
      comeco = agora;
    }
    if (strncmp((char*)rfrecv.cmd, BOTAO3, CMD_SIZE) == 0) {
      oscilando = false;
      tocando = false;
      noTone(buzzer);
    }
  }

  if(oscilando && agora - comeco > intervalo) {
      tocando = !tocando;
      comeco = agora;
      if(tocando) {
          tone(buzzer, 650);
      } else {
          noTone(buzzer);
      }
  }
}
    
09.02.2015 / 14:15
3

To complement the @bfavaretto answer on Mult-task on Arduino

Arduino can support multitasking based on time-sharing, even on AVR controllers that are 8 bits, but this is only applicable in very simple codes and if the activity requires calculations and controls and real time, but without doubt the ideal is to use an Arduino DUE if I really need Multtask, I started some studies with FreeRTOS and I got to DuinOS, I even made a fork and some updates but for lack of time I stopped.

Using RTOS even in 8bits architecture helps a lot in tasks that require a good timing to trigger ports and read ports as long as they are simple tasks and calculations. It is possible to run Arduino UNO up to 3 basic tasks, let's assume you have read some level meters, trigger pumps so that everything works in three independent or synchronized control blocks.

The advantage is often to have a better timing and as it says, RTOS, brings us results in real time, which eliminates the use of functions as delay() and reduces the simplicity of the code in the relative layer of logic itself. processo, eliminando diversas variáveis de controle de tempo, como usado quando se quer eliminar o uso do delay () ';

    
11.06.2015 / 18:45