strange problem

Hello!! The project is step counter with ws2812 matrix screen. having strange problem with my code: when variable “steps” < 10 ESP32 rebooting every cycle, when scrolling text reaching edge of the screen. If > 10 everything works fine.

#include <FastLED.h>
#include <LEDMatrix.h>
#include <LEDText.h>
#include <Font12x16.h>
#define xpin A6
#define ypin A3
#define zpin A0
#define LED_PIN        2
#define COLOR_ORDER    RGB
#define CHIPSET        WS2812B
#define MATRIX_WIDTH   16
#define MATRIX_HEIGHT  16
#define MATRIX_TYPE    HORIZONTAL_ZIGZAG_MATRIX
cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds;
cLEDText ScrollingMsg;
      float xval[15]={0};
      float yval[15]={0};
      float zval[15]={0};
      float xavg;
      float yavg;
      float zavg;
      int steps=10;
      int flag=0;
      float threshhold=800.0;
      String str;
      char TxtDemo[5];

void setup()
{
  Serial.begin(115200);
   Serial.println("Calibrating......");
  float sum=0;
  float sum1=0;
  float sum2=0;
  for (int i=0;i<15;i++)
  {
    xval[i]=float(analogRead(xpin));
    sum=xval[i]+sum;
  }
  delay(15);
  xavg=sum/15.0;
  Serial.println(xavg);
  for (int j=0;j<15;j++)
  {
    xval[j]=float(analogRead(ypin));
    sum1=xval[j]+sum1;
  }
  yavg=sum1/15.0;
  Serial.println(yavg);
  delay(15);
  for (int i=0;i<15;i++)
  {
    zval[i]=float(analogRead(zpin));
    sum2=zval[i]+sum2;
  }
  zavg=sum2/15.0;
  delay(15);
  Serial.println(zavg);
  Serial.println("Calibration Successful!");
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds[0], leds.Size());
  FastLED.setBrightness(20);
  FastLED.clear(true);
  //FastLED.show();
  //ScrollingMsg.SetFrameRate(00);
  ScrollingMsg.SetFont(Font12x16Data);
  ScrollingMsg.Init(&leds, leds.Width(), ScrollingMsg.FontHeight(), 0, 0);
  ScrollingMsg.SetText((unsigned char *)TxtDemo, sizeof(TxtDemo) - 1);
  ScrollingMsg.SetTextColrOptions(COLR_RGB | COLR_SINGLE, 0xff, 0xff, 0xff);
  //ScrollingMsg.SetBackgroundMode(BACKGND_LEAVE);
 // ScrollingMsg.SetScrollDirection(SCROLL_UP);
  //ScrollingMsg.SetTextDirection(CHAR_UP);
 // ScrollingMsg.SetScrollDirection(SCROLL_LEFT);
  ScrollingMsg.SetFrameRate(01);
 //ScrollingMsg.SetTextDirection(DOWN);
 
}


void loop()
{
  int acc=0;
  float totvect[5]={0};
  float totave[5]={0};
  float xaccl[5]={0};
  float yaccl[5]={0};
  float zaccl[5]={0};
  for (int i=0;i<5;i++)
  {
    xaccl[i]=float(analogRead(xpin));
    delay(1);
    yaccl[i]=float(analogRead(ypin));
    delay(1);
    zaccl[i]=float(analogRead(zpin));
    delay(1);
    totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zval[i] - zavg)*(zval[i] - zavg)));
    totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
    
    //cal steps 
    if (totave[i]>threshhold && flag==0)
    {
       steps=steps+1;
       flag=1;
    }
    else if (totave[i] > threshhold && flag==1)
    {
        //do nothing 
    }
    if (totave[i] <threshhold  && flag==1)
    {
      flag=0;
    }
   
  }
 
  str =  String(" ") + String(steps) + String(" ");
  str.toCharArray(TxtDemo,5); 
  if (ScrollingMsg.UpdateText() == -1)
    ScrollingMsg.SetText((unsigned char *)TxtDemo, sizeof(TxtDemo) - 1);
  else
    FastLED.show();
    Serial.println(TxtDemo);
}

jump.ino (2.93 KB)

What you describe can be indicative of writing and/or reading to/from beyond the bounds of an array. Carefully examine operations on arrays for that problem.

indeed. Array bounds.
This looks rather odd in a loop where 'i' has a value 0 through to 4:

totave[i] = (totvect[i] + totvect[i-1]) / 2 ;

ArduinoPedometer/ArduinoPedometer.cpp at master · abhaysbharadwaj/ArduinoPedometer · GitHub This part of code from that library and works fine to transfer data to serial port.

str = String(" ") + String(steps) + String(" ");
str.toCharArray(TxtDemo,5);

do I convert string into char array right??

Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1)
Core 1 register dump:
PC : 0x4000c271 PS : 0x00060034 A0 : 0x80088ed7 A1 : 0x3ffb1db0
A2 : 0x3ffb0058 A3 : 0x3f4053d8 A4 : 0x00000014 A5 : 0x3ffbe790
A6 : 0x3ffbe7d8 A7 : 0x00000001 A8 : 0x00000001 A9 : 0x3f4053d9
A10 : 0x000000a5 A11 : 0xff000000 A12 : 0x80089abf A13 : 0x3ffbe760
A14 : 0x00000008 A15 : 0x00000001 SAR : 0x0000001f EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x40001699 LEND : 0x400016aa LCOUNT : 0xfffffffd

Backtrace: 0x4000c271:0x3ffb1db0 0x40088ed4:0x3ffb1dd0 0x4008a8e8:0x3ffb1df0 0x4008a89e:0x3ff9c50f

Core 0 register dump:
PC : 0x400ec01a PS : 0x00060134 A0 : 0x800d5af2 A1 : 0x3ffbbff0
A2 : 0x00000000 A3 : 0x00000001 A4 : 0x00000000 A5 : 0x00000001
A6 : 0x00060120 A7 : 0x00000000 A8 : 0x800d56ba A9 : 0x3ffbbfc0
A10 : 0x00000000 A11 : 0x40085120 A12 : 0x00060120 A13 : 0x3ffbb970
A14 : 0x00000000 A15 : 0x3ffbbce0 SAR : 0x00000000 EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000

Backtrace: 0x400ec01a:0x3ffbbff0 0x400d5aef:0x3ffbc010 0x40089aae:0x3ffbc030 0x400885bd:0x3ffbc050

What this could be??

as far as i can tell, this should anyway get you into trouble at some pointchar TxtDemo[5];``str.toCharArray(TxtDemo,5);If you copy 5 characters, you should leave space for the terminator.

Try

   str =  " "  // avoid String( ) unless really needed
   str += steps;
   str += " ";
  if (ScrollingMsg.UpdateText() == -1) {
    ScrollingMsg.SetText(str.c_str(), str.length());
  } else {
    FastLED.show();
  }
    Serial.println(TxtDemo);

Deva_Rishi:
as far as i can tell, this should anyway get you into trouble at some point

char TxtDemo[5];
str.toCharArray(TxtDemo,5);

If you copy 5 characters, you should leave space for the terminator.

Thanks!!! There was a problem))

drmpf:
Try

   str =  " "  // avoid String( ) unless really needed

str += steps;
  str += " ";
 if (ScrollingMsg.UpdateText() == -1) {
   ScrollingMsg.SetText(str.c_str(), str.length());
 } else {
   FastLED.show();
 }
   Serial.println(TxtDemo);

Thannk you!! that code runs much faster

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