Guru Meditation Error: Core 1 panic'ed when using Serial.write

I'm creating a system to run a WS2128B from a ESP32, and controlled by an android app written in MIT App Creator.
Linking between the phone and ESP32 works fine.
The WS2128B works fine. (Well, not fine, but that will be another question).
My problem is sending information back from the ESP32 to the phone.
I have cut down the arduino program from the original, with extensive labelling, to show the error.
The program DOES send data to the phone, correctly, but then, later, there is another send to the phone which crashes "Guru Meditation Error: Core 1 panic'ed (InstrFetchProhibited). Exception was unhandled."

This is the arduino program:

#include <FastLED.h>
#define NUM_LEDS 60  // Numbr of LEDs
#define DATA_PIN 4  // Incoming Data from BT
#define LED_TYPE NEOPIXEL
#define COLOUR_ORDER GRB
#define BRIGHTNESS 60
#define VOLTS 5
#define MAX_AMPS 500
int LED_SPEED = 10;  // LED Speed - varies with Faster/Slower
char LED_COLOUR = 'B'; //  LED Colour - varies Red/Blue/Green/White
char RUNNING = 'N';  //  Defines whether light strip/sounds/buzzes are currently running Y = Yes, B = Brake (finish cycle then stop), N = No
#include "BluetoothSerial.h"
#define LEDpin 2  // The pin to which the LED is connected
BluetoothSerial SerialBT;

char command;

CRGB leds[NUM_LEDS];


void setup()
{
  { Serial.begin(115200);
    SerialBT.begin("LightBar");  // Bluetooth device name
    pinMode(LEDpin, OUTPUT);
    Serial.println("The device started, now you can pair it with Bluetooth!");
  }
  {
    FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
    FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.clear();
    FastLED.show();
  }
}

void loop() {

  if (SerialBT.hasClient()) {
    digitalWrite(LEDpin, HIGH);  // Turn the LED on when connected
  } else {
    digitalWrite(LEDpin, LOW);  // Turn the LED off when not connected
  }

  // DATA RECEIVED DATA RECEIVED DATA RECEIVED DATA RECEIVED

  if (SerialBT.available()) {
    char receivedChar = SerialBT.read();
    {
      Serial.println(receivedChar);
      SerialBT.write(1);
    }

    if (receivedChar == 'T') {
      RUNNING = 'Y';
      Serial.println(RUNNING);
    }

    if (receivedChar == 'O') {
      RUNNING = 'B';
    }

  }

  // CYCLE CYCLE CYCLE CYCLE CYCLE CYCLE CYCLE CYCLE

  if (RUNNING == 'Y' || RUNNING == 'B') {

    Serial.println("CYCLE");
      SerialBT.write(1);
      Serial.println(RUNNING);
    if (RUNNING == 'B') {
      RUNNING = 'N';
    }
    Serial.println(RUNNING);
      SerialBT.write(1);
      if (LED_COLOUR == 'B') {  //                                                               START BLUE LEDs CYCLE to Right

      
      for (int dot = 0; dot < NUM_LEDS; dot++) {   //                                            Start Row heading RIGHT
        leds[dot] = CRGB::Blue; //                                                               Set Blue LED
        FastLED.show();
        // clear this led for the next time around the loop
        leds[dot] = CRGB::Black;
        delay(LED_SPEED);
      Serial.println(dot);             //                                                        Print "dot"
      Serial.println("Go Right");      //                                                        Print "Go Right" 
      SerialBT.write(1);
      } //                                                                                       END Row heading RIGHT


      
      SerialBT.write(1);               //                                                        Send 1 to BlueTooth
      Serial.println("Blue R");        //                                                        Print "Blue R"
      for (int dot = NUM_LEDS; dot >= 0; dot--) {  //                                            Start Row heading LEFT  
        leds[dot] = CRGB::Blue;
        FastLED.show();
        // clear this led for the next time around the loop
        leds[dot] = CRGB::Black;
        leds[1] = CRGB::Black;
        delay(LED_SPEED);
      Serial.println(dot);             //                                                        Print "dot"
      Serial.println("Go Left");      //                                                        Print "Go Left" 
      SerialBT.write(1);                  //                                        CRASHES ON THIS LINE
      }  //                                                                                      END Row heading LEFT
      Serial.println("Blue L");
      SerialBT.write(1);
      Serial.println("Blue +");
    }

  }
}

Serial Monitor Output:

T
Y
CYCLE
Y
Y
0
Go Right
1
Go Right
2
Go Right
3
Go Right
4
Go Right
5
Go Right
6
Go Right
7
Go Right
8
Go Right
9
Go Right
10
Go Right
11
Go Right
12
Go Right
13
Go Right
14
Go Right
15
Go Right
16
Go Right
17
Go Right
18
Go Right
19
Go Right
20
Go Right
21
Go Right
22
Go Right
23
Go Right
24
Go Right
25
Go Right
26
Go Right
27
Go Right
28
Go Right
29
Go Right
30
Go Right
31
Go Right
32
Go Right
33
Go Right
34
Go Right
35
Go Right
36
Go Right
37
Go Right
38
Go Right
39
Go Right
40
Go Right
41
Go Right
42
Go Right
43
Go Right
44
Go Right
45
Go Right
46
Go Right
47
Go Right
48
Go Right
49
Go Right
50
Go Right
51
Go Right
52
Go Right
53
Go Right
54
Go Right
55
Go Right
56
Go Right
57
Go Right
58
Go Right
59
Go Right
Blue R
60
Go Left
Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0xe709c000  PS      : 0x00060a30  A0      : 0x800d25e3  A1      : 0x3ffc9650  
A2      : 0x3ffc31e8  A3      : 0x00000001  A4      : 0x0000000a  A5      : 0x00000001  
A6      : 0x400d2404  A7      : 0x3ffc3134  A8      : 0x8018ecbc  A9      : 0x3ffc9640  
A10     : 0x3ffc31e8  A11     : 0x3ffc965c  A12     : 0x00000001  A13     : 0x00000000  
A14     : 0x00000063  A15     : 0x00060023  SAR     : 0x00000020  EXCCAUSE: 0x00000014  
EXCVADDR: 0xe709c000  LBEG    : 0x4009108d  LEND    : 0x4009109d  LCOUNT  : 0xffffffff  


Backtrace: 0x6709bffd:0x3ffc9650 0x400d25e0:0x3ffc9680 0x400d4e1d:0x3ffc96b0




ELF file SHA256: bbd2c2018ac531af

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1344
load:0x40078000,len:13964
load:0x40080400,len:3600
entry 0x400805f0
The device started, now you can pair it with Bluetooth!

The first iteration of this for loop will be index 60, out of bounds to your array.

Counting backwards needs a -1 in the initialization step.

`for (int dot = NUM_LEDS - 1; dot >= 0; dot--) `

Oh, sod it.
I'll test it later.
Stupid error.
The answer's right in front of me on the Serial Monitor.
Thanks. I'll report back later.

OK. I get this, but how can I get round it?
(I'm not C++ literate)
Sorry. I've tried a number of things but keep getting various errors

I gave you the answer. I even quoted how to change the line.

Do you understand that an array of 60 elements are numbered 0-59? There is no element 60?

If you're still having problems then post the modified code. I don't understand how you think anyone can help without seeing what you did.

OK. I missed those bottom two lines, but I worked out that's what I needed to do.
Trying it now....

Bingo. Excellent. Thanks so much.

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