Go Down

Topic: using arduino due timer interrupt functions to send data to nextion screen (Read 271 times) previous topic - next topic

iraqihu

hello I'm designing ECG simulator project which displays the signal real-time on screen also write it as real scale voltage on DAC pin
I'm using Arduino due for this purpose and nextion screen
so I need to use multitask in due
but the problem that when I call a function to plot data wave on screen in timer function
the connection between due and screen is crashed and stop
what can I do to be able to call screen function in timer functions??
help me, please
thanks


here is the timer function code
Code: [Select]
void myHandler()
{
  if (onoff == 1) {  // && (reth == 1)) {

    for (int i = 0; i < 100; i++) {
      nexLoop(nex_listen_list);
      s0.addValue(1, y[i]);
      dbSerialPrintln(y[i]);
    }
    int c = map(bpm, 60, 100, 60, 1);
    for (int i = 1; i < c; i++) {
      s0.addValue(1, y[99]);
    }
  }

  reth = 1;
  Serial.println("reth");
}



here is the definiton of timer function
Code: [Select]
Timer.getAvailable().attachInterrupt(myHandler).start(); delay(100);
    DueTimer my = Timer.getAvailable();
    my.attachInterrupt(myHandler);
    my.start(10000);




here is the whole code

Code: [Select]
int y [] = {22
            , 23
            , 22
            , 23
            , 22
            , 21
            , 23
            , 24
            , 24
            , 27
            , 30
            , 32
            , 35
            , 36
            , 37
            , 38
            , 39
            , 41
            , 42
            , 44
            , 44
            , 43
            , 42
            , 43
            , 46
            , 52
            , 56
            , 57
            , 56
            , 52
            , 48
            , 43
            , 41
            , 40
            , 43
            , 43
            , 45
            , 43
            , 40
            , 39
            , 37
            , 36
            , 36
            , 36
            , 38
            , 36
            , 36
            , 34
            , 32
            , 32
            , 31
            , 33
            , 33
            , 32
            , 30
            , 26
            , 19
            , 15
            , 25
            , 54
            , 102
            , 160
            , 210
            , 237
            , 240
            , 214
            , 157
            , 83
            , 22
            , 0
            , 9
            , 27
            , 38
            , 39
            , 33
            , 28
            , 25
            , 25
            , 26
            , 27
            , 26
            , 25
            , 24
            , 24
            , 25
            , 26
            , 27
            , 25
            , 25
            , 24
            , 23
            , 23
            , 24
            , 23
            , 25
            , 24
            , 24
            , 23
            , 21
            , 20
           };
#include "Nextion.h"
#include <DueTimer.h>

NexText t1 = NexText(0, 2, "t1");
NexWaveform s0 = NexWaveform(0, 5, "s0");
NexNumber n0 = NexNumber(0, 7, "n0");
NexButton b0 = NexButton(0, 1, "b0");
NexPicture p1 = NexPicture(0, 6, "p1");
NexPicture p3 = NexPicture(0, 9, "p3");
NexPicture p4 = NexPicture(0, 10, "p4");
NexPicture p5 = NexPicture(0, 11, "p5");
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
  &n0,
  &t1,
  &p1,
  &p3,
  &p4,
  &p5,
  NULL
};

int reth = 0;
int onoff = 2;
uint32_t bpm;
void setup(void)
{ Serial1.begin(9600);
  nexInit();
  p1.attachPop(p1PopCallback);
  p3.attachPop(p3PopCallback);
  p4.attachPop(p4PopCallback);
  p5.attachPop(p5PopCallback);
  t1.attachPop(t1PopCallback);
  dbSerialPrintln("setup done");






  Timer.getAvailable().attachInterrupt(myHandler).start(); delay(100);
    DueTimer my = Timer.getAvailable();
    my.attachInterrupt(myHandler);
    my.start(10000);


}

void loop(void)
{
  nexLoop(nex_listen_list);
 
 
}


void p1PopCallback(void *ptr)
{
  uint32_t number = 0;
  p1.getPic(&number);
  uint32_t number1 = 0;
  n0.getValue(&number1);
  bpm = number1;
  if (number == 8)
  {
    onoff = 0;
    Serial.println("offecg");
  }
  else
  {
    Serial.println("onecg 1");
    onoff = 1;
  }
}

void t1PopCallback(void *ptr)
{
  reth = 1;
  Serial.println("reth");
  t1.setText("adfdasfd");
}

void p3PopCallback(void *ptr)
{
  uint32_t number1 = 0;
  n0.getValue(&number1);
  bpm = number1;
  n0.setValue(bpm);

}
void p4PopCallback(void *ptr)
{

  uint32_t number1 = 0;
  n0.getValue(&number1);
  bpm = number1;
  n0.setValue(bpm);
}


void p5PopCallback(void *ptr)
{
 

  reth = 1;
  Serial.println("reth");
}

void myHandler()
{
  if (onoff == 1) {  // && (reth == 1)) {

    for (int i = 0; i < 100; i++) {
      nexLoop(nex_listen_list);
      s0.addValue(1, y[i]);
      dbSerialPrintln(y[i]);
    }
    int c = map(bpm, 60, 100, 60, 1);
    for (int i = 1; i < c; i++) {
      s0.addValue(1, y[99]);
    }
  }

  reth = 1;
  Serial.println("reth");
}

ard_newbie


Some thoughts about your code:

Libraries included in a sketch should appear right at the beginning of the sketch.

Do not serial print inside an interrupt handler, instead set a flag in the interrupt handler, test this flag in loop() and print if required.

You could try tc_lib from antodom, a good timer library.

Try first to make the Nextion screen run properly with the DUE.

iraqihu

hello ard_newbie thanks for your replay

yes i tried to put the plotting function in the main loop but the problem is the Arduino can't continuously read the upcoming command from screen
Code: [Select]
nexLoop(nex_listen_list);

also, i tried to put this function
Code: [Select]
nexLoop(nex_listen_list);
in myHandler put the problem is the same nothing happen


iraqihu

yes this callback just receive the commands and only run once but the plotting function is continuous   

Idahowalker

Have you considered a RTOS like library?

Something like uMT or TaskScheduler or uTimerLibrary? They all work with the DUE . My favored is uMT which is most like freeRTOS. Task can be run on timer or round robin with uMT.

Go Up