Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Run parallel functions

i am getting this error when i try to run two functions parallel with my esp 32. The first loop will control an actuator via a self-defined function, the second loop will control a stepper motor through inputs from a joystick.

Error message:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
12:53:34.269 ->
12:53:34.269 -> Core 1 register dump:
12:53:34.269 -> PC : 0x400d155f PS : 0x00060630 A0 : 0x800d1487 A1 : 0x3ffb2780
12:53:34.411 -> A2 : 0x00000000 A3 : 0x00061a80 A4 : 0x0002ae71 A5 : 0xb33fffff
12:53:34.504 -> A6 : 0x00000001 A7 : 0x00000001 A8 : 0x80082e70 A9 : 0x3ffb2780
12:53:34.551 -> A10 : 0x3ff000e0 A11 : 0x00000001 A12 : 0x3ffbdc88 A13 : 0x00000000
12:53:34.645 -> A14 : 0x00000000 A15 : 0x00000000 SAR : 0x0000001e EXCCAUSE: 0x0000001c
12:53:34.739 -> EXCVADDR: 0x00000004 LBEG : 0x40085d89 LEND : 0x40085d99 LCOUNT : 0xfffffffe
Backtrace:0x400d155c:0x3ffb27800x400d1484:0x3ffb27a0 0x400d113a:0x3ffb27d0 0x400d1227:0x3ffb2800 0x400d4105:0x3ffb2820

#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_MCP4725.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <Stepper.h>

// define objects
Adafruit_MCP4725 dac0;

// PWM properties
const int pwmPin = 13;
const int freq = 732;
const int ledChannel = 0;
const int resolution= 8;

// boolean variables
bool autoMode = false;
bool mannualMode = false;
bool delayOrNot = false;
bool fastOrNot = false;
bool cutoffOrNot = false;
bool preTestTrigger = false;

// experimental variables
int delayTime = 19;
int downStep = 1023;
int cutoffVol = 1364;

// stepper motor
const int stepsPerRevolution = 2048;

// ULN2003 motor driver pins
#define IN1 D10
#define IN2 D11
#define IN3 D12
#define IN4 D13

// Joystick Pins
#define inputX A3
#define inputY A4
#define inputC D2

int outputX = 0;
int outputY = 0;
int outputC = 0;

// Initialise stepper motor
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);

TaskHandle_t Task1;

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Serial.begin(9600);

  while(!Serial);
  delay(1000);
  Serial.println("Serial communication set up");

  // set up pin mode
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(inputC, INPUT);

  // set up DAC converter
//  if(!dac0.begin(0x62)){
//    Serial.println("Failed to connect to the converter.");
//    while(1);
//  }

  // set up pwm properties
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(pwmPin, ledChannel);

  // stepper motor speed
  myStepper.setSpeed(5);

  // Initialise your task (2nd loop)
  xTaskCreatePinnedToCore(
    loop2,            // name of the task function
    "printLettera",   // name of the task
    1000,             // memory assigned for the task
    NULL,             // parameter to pass if any
    1,                // priority of task, starting from 0(Highestpriority) 
                      // *IMPORTANT*( if set to 1 and there is no activity in your 2nd loop, it will reset the esp32)
    &Task1,           // Reference name of taskHandle variable
    1);               // choose core (0 or 1)

   // LED for final check
   digitalWrite(LED_BUILTIN, HIGH);
   delay(3000);
   digitalWrite(LED_BUILTIN, LOW);
}

// Loop 1: For haptic motor control 
void loop() {
  // put your main code here, to run repeatedly:
  const int dutyCycle = 127;
  int inComing = Serial.read();

  // write specified input signal wave
  ledcWrite(ledChannel, dutyCycle);

  if(Serial.available()>0){
    if (inComing == 48){
      autoMode = true;
      Serial.println("Initial display is ready to go");
      if (autoMode){
        Serial.println("Display 9 configurations at once");
        delay(500);
        Serial.print("Starting in: \t");
        Serial.print("3 \t");
        delay(1000);
        Serial.print("2 \t");
        delay(1000);
        Serial.println("1 \t");
        delay(1000);

        // delay time configurations
        hapticConfig(19, 255,0,19);
        delay(5000);
//        hapticConfig(0,255,0,39);
//        delay(5000);
//        hapticConfig(39,255,0,15);
//
//        // ramp down step length configurations
//        hapticConfig(19,1024,0,29);
//        delay(5000);
//        hapticConfig(19,64,0,15);
//        delay(5000);
//        hapticConfig(19,255,0,19);
//        delay(5000);
//
//        // cut-off voltage configurations
//        hapticConfig(19,255,0,19);
//        delay(5000);
//        hapticConfig(19,255,1364,19);
//        delay(5000);
//        hapticConfig(19,255,511,19);
//        delay(5000);

        Serial.println("Initial displace ends");
      }
      autoMode = false;
    }
  }
}

// loop 2: For stepper motor control and encoder readings
void loop2(void*parameter){
  for (;;){
    // write your code the 2nd loop to run endlessly
    outputX = analogRead(inputX);
    outputY = analogRead(inputY);
    outputC = digitalRead(inputC);

    if (outputX < 100){
      Serial.println("Moving to the left");
      myStepper.step(-stepsPerRevolution/4);
    }
    else if (outputX > 4000){
      Serial.println("Moving to the right");
      myStepper.step(stepsPerRevolution/4);
    }
    
    delay(1000);
  }
}

void hapticConfig(int delayTime, int downStep, int cutoffVol, int maxRepeat){
  uint32_t dac0Vol = 4095;
  for (int repeatTime = 0; repeatTime <= maxRepeat; repeatTime ++){
      for (int counter = 0; counter <= delayTime; counter ++){
        dac0.setVoltage(dac0Vol, false);
        delay(1);
        //Serial.println(dac0Vol);
      } 
      for (int value = 4095; value >= cutoffVol; value = value - downStep){
        dac0.setVoltage(value, false);
        //Serial.println(value);
      }
    }
}

Hi,
avoid using large delays in ESP32.
These delays may cause the kind of problem you're having.
If the use of large delay is unavoidable, use the function before the delay: yield();

So if the OP commets out the adafruity libraries and then allow one at a time to load does the problem go away and then reappear?

What did the OP get when they put the debug thignies into the ESP Exception Decoder?

Hi, thank you very much. I will go and try get rid of some delay function on Monday. Thanks for the suggestion.

Hello, thanks for the suggestion. But I don’t see any reason why adafruit library will cause a memory reading error?

There is no problem using delay(). A delay inherently yields, returning control to the scheduler, until the delay has expired.

The error suggests an attempt to read from memory that is invalid or inaccessible to the process was attempted. Look for an invalid pointer somewhere in your code.

1 Like

Hello thanks for this information. I am new to esp32 programming, would you mind me asking what is a pointer and how I normally debug this kind of invalid pointer error? Thanks!

It is a bad idea to put code in the loop() function when using freeRTOS. The loop() function is not guarenteed to run and when code is in the loop function memory clean up is not done.

The problem is now solved. By backtracing the error code, I forgot to initialise the DAC converter in the setup section so the pointer could not locate the correct position. Thanks all for your answers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.