0

我想知道是否有人可以帮助我?

我有一个 5M 的 LED 条(300 个 LED)并在我的 ESP32 上运行以下代码。该代码将使 LED 灯旋转!

void spinningwheel(){
int random_number;
int spin_speed;
for(int i =0; i < 1; i++){
random_number = random(NUM_LEDS);
}

Serial.println(random_number);
for (int i = 0; i <= random(6,15); i++){
spin_speed = i+1;
for (int i = 0; i <= NUM_LEDS; i++){
leds[i] = CRGB ( 0, 0, 255);
leds[i-10] = CRGB ( 0, 0, 0);
FastLED.show();
delay(spin_speed);
}
FastLED.clear();

}
for (int i = 0; i <= random_number; i++){
leds[i] = CRGB ( 0, 0, 255);
leds[i-10] = CRGB ( 0, 0, 0);
FastLED.show();
delay(spin_speed);
}
FastLED.clear();
delay(1000);
}

如果我使用,代码工作正常leds[i-3] = CRGB ( 0, 0, 0); 但是当我将数字更改i-3为类似i-10ill 在我的串行端口中出现错误时

15:51:36.233 -> 258
15:51:36.267 -> Guru Meditation Error: Core  1 panic'ed (StoreProhibited). 
Exception was unhandled.
15:51:36.335 -> Core 1 register dump:
15:51:36.368 -> PC      : 0x400d12ae  PS      : 0x00060930  A0      : 
0x800d1658  A1      : 0x3ffb1e80  
15:51:36.469 -> A2      : 0x3ffc0124  A3      : 0x3ffb1ea4  A4      : 
0x3ffc04bc  A5      : 0x3ffb1eb0  
15:51:36.571 -> A6      : 0x00000000  A7      : 0x3ffb0060  A8      : 
0x00000000  A9      : 0x3ffb1e40  
15:51:36.639 -> A10     : 0x00000001  A11     : 0x00000000  A12     : 
0x3ffb8570  A13     : 0x00000000  
15:51:36.742 -> A14     : 0x3ffb8528  A15     : 0x00000000  SAR     : 
0x00000020  EXCCAUSE: 0x0000001d  
15:51:36.843 -> EXCVADDR: 0x00000000  LBEG    : 0x400d0dfd  LEND    : 
0x400d0e0c  LCOUNT  : 0x00000000  
15:51:36.944 -> 
15:51:36.944 -> Backtrace: 0x400d12ae:0x3ffb1e80 0x400d1655:0x3ffb1ea0 
0x400d1741:0x3ffb1ee0 0x400d0ee0:0x3ffb1f20 0x400d0fe1:0x3ffb1f40 
0x400d115e:0x3ffb1f70 0x400d118f:0x3ffb1f90 0x400d216d:0x3ffb1fb0 
0x40088215:0x3ffb1fd0
15:51:37.147 -> 
15:51:37.147 -> Rebooting...
15:51:37.180 -> :⸮⸮⸮⸮L⸮⸮1⸮m֊⸮1
HL⸮⸮b⸮⸮⸮

错误解码

PC: 0x400d12ae: ClocklessController14, 60, 150, 90, (EOrder)66, 0, false, 
5>::showPixels(PixelController(EOrder)66, 1, 4294967295u>&) at 
D:\Documenten\Arduino\libraries\FastLED/controller.h line 178
EXCVADDR: 0x00000000

Decoding stack results
0x400d12ae: ClocklessController14, 60, 150, 90, (EOrder)66, 0, false, 
5>::showPixels(PixelController(EOrder)66, 1, 4294967295u>&) at 
D:\Documenten\Arduino\libraries\FastLED/controller.h line 178
0x400d1655: CPixelLEDController(EOrder)66, 1, 4294967295u>::show(CRGB const*, 
int, CRGB) at D:\Documenten\Arduino\libraries\FastLED/controller.h line 408
0x400d1741: CFastLED::show(unsigned char) at 
D:\Documenten\Arduino\libraries\FastLED/controller.h line 90
0x400d0ee0: CFastLED::show() at 
D:\Documenten\Arduino\libraries\FastLED/FastLED.h line 500
0x400d0fe1: spinningwheel() at 
D:\Documenten\Arduino\Ledstrip_wave/Ledstrip_wave.ino line 48
0x400d115e: Binair_buttons() at 
D:\Documenten\Arduino\Ledstrip_wave/Ledstrip_wave.ino line 125
0x400d118f: loop() at D:\Documenten\Arduino\Ledstrip_wave/Ledstrip_wave.ino 
line 141
0x400d216d: loopTask(void*) at 
C:\Users\....\AppData\Local\Arduino15\packages\esp32\
hardware\esp32\1.0.4\cores\esp32\main.cpp line 19
0x40088215: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib- 
builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

有人可以解释一下我做错了什么吗?或者我是否通过了最大 RAM 或 CPU 使用率。

4

1 回答 1

0

您有从 0 到 NUM_LEDS-1 索引的颜色数组,如果您在该空间之前或之后使用索引,那么您正在破坏您不应该破坏的东西。现在:

for (int i = 0; i <= NUM_LEDS; i++){  // values of i  0 .. NUM_LEDS (including! -buffer overflow)
  leds[i] = CRGB ( 0, 0, 255);
  leds[i-10] = CRGB ( 0, 0, 0);  // for all i less than 0, you are BEFORE the actual array

所以你已经做到了两边都走出阵外。在第一个元素之前添加三个元素显然不会对程序崩溃造成太大的破坏,但是在前面的 10 个元素肯定会破坏一些非常重要的东西。并且在数组边界之后也会破坏存储在它之后的东西..

于 2021-03-27T15:19:56.340 回答