ftrias / TeensyThreads

MIT License
178 stars 26 forks source link

Teensy 4.1 keeps crashing with this simple code #34

Closed Zahidilyas closed 1 year ago

Zahidilyas commented 1 year ago

My code kept crashing after I started using Teensythreads. To get to the bottom of the problem, I kept deleting code until I found the following code. Without threading it does not crash. What might be the problem?

#include <Arduino.h>
#include "TeensyThreads.h"

void setup() {
  Serial.begin(9600);
  threads.addThread(serialread);
}

void loop() {
//  serialread();
  delay(500);
}

void serialread(){
  int numberOfSegments = 100; // only so i can change it to 2 in manual mode
  double x[numberOfSegments];
  double y[numberOfSegments];

  while(1){
    draw_line(0, 0, 0, 0, 100, x, y);

    threads.delay(1000);
    threads.yield();
  }
}

double draw_line(double x1, double y1, double x2, double y2, int NumberOfSegments, double *x, double *y) {

 for ( int i = 0; i < NumberOfSegments ; i++) {
   x[i] = 0;
   y[i] = 0;
   Serial.println(y[i]);
 }
}
Zahidilyas commented 1 year ago

The problem was the DEFAULT_STACK_SIZE = 1024;. It did not have enough memory to assign to the arrays.