My function isn't running

Hello, I am making a scoreboard for a school club and I am struggling with a function not running.

void clearSide(bool side) {
  if (side) {
    for (int i = 0; i<4; i++) {
      matrix.clearDisplay(addrSide1[i]);
    }
    Serial.println("side1");
  }
  else {
    for (int i = 0; 4; i++) {
      matrix.clearDisplay(addrSide0[i]);
    }
    Serial.println("side0");
  }
}

being ran here,

void loop() {
  if (Serial.available() > 0) {
    char read = Serial.read();
    Serial.println(read);
    message=read+message;
  }
  else if (message.toInt() > 0) {
    int n0 = message.toInt();
    int n1 = n0%10;
    bool side = (n0-1>9);
    clearSide(side);
    createDivider();
    Serial.print(n0);
    Serial.print(", ");
    Serial.print(n1);
    Serial.print(", ");
    Serial.println(side);
    writeNum(n1, side);
    message = "";
  }
}

and the functions around clearSide are running, it's not halting.

Here are the variables and functions calls though I doubt they're important:

void createDivider() {
  matrix.setColumn(1, 0, max);
  matrix.setColumn(2, 7, max);
  matrix.setColumn(5, 0, max);
  matrix.setColumn(6, 7, max);
}

void createPixelFromCoords(int x, int y, bool value) {
  int addr = 0;
  int ex = x%8;
  int ey = y%8;
  int ox = ex;
  int oy = ey;
  ex = oy;
  ey = ox;
  if (y < 8) {
    ex = 7-ex;
    ey = 7-ey;
    if (x < 8) {
      addr = 0;
    }
    else if (x < 16) {
      addr = 1;
    }
    else if (x < 24) {
      addr = 2;
    }
    else {
      addr = 3;
    }
  }
  else {
    if (x < 8) {
      addr = 7;
    }
    else if (x < 16) {
      addr = 6;
    }
    else if (x < 24) {
      addr = 5;
    }
    else {
      addr = 4;
    }
  }
  matrix.setLed(addr, ex, ey, value);
}

void writeNum(int num, bool side) {
  Serial.print(num);
  Serial.print(", ");
  Serial.print(side);
  Serial.print(", ");
  int arraySize;
  int sizeZero = sizeof(zero)/sizeof(zero[0]);
  int sizeOne = sizeof(one)/sizeof(one[0]);
  int sizeTwo = sizeof(two)/sizeof(two[0]);
  int sizeThree = sizeof(three)/sizeof(three[0]);
  int sizeFour = sizeof(four)/sizeof(four[0]);
  int sizeFive = sizeof(five)/sizeof(five[0]);
  int sizeSix = sizeof(six)/sizeof(six[0]);
  int sizeSeven = sizeof(seven)/sizeof(seven[0]);
  int sizeEight = sizeof(eight)/sizeof(eight[0]);
  int sizeNine = sizeof(nine)/sizeof(nine[0]);
  if (num == 0) {
    arraySize = sizeZero;
  }
  else if (num == 1) {
    arraySize = sizeOne;
  }
  else if (num == 2) {
    arraySize = sizeTwo;
  }
  else if (num == 3) {
    arraySize = sizeThree;
  }
  else if (num == 4) {
    arraySize = sizeFour;
  }
  else if (num == 5) {
    arraySize = sizeFive;
  }
  else if (num == 6) {
    arraySize = sizeSix;
  }
  else if (num == 7) {
    arraySize = sizeSeven;
  }
  else if (num == 8) {
    arraySize = sizeEight;
  }
  else if (num == 9) {
    arraySize = sizeNine;
  }
  else {
    arraySize = 0;
  }
  int arrayy[arraySize] = {};
  int len = 0;
  Serial.println(arraySize);
  if (num == 0) {
    for (int i = 0; i < sizeZero; i++) {
      arrayy[i] = zero[i];
    }
    len = sizeZero;
  }
  else if (num == 1) {
    for (int i = 0; i < sizeOne; i++) {
      arrayy[i] = one[i];
    }
    len = sizeOne;
  }
  else if (num == 2) {
    for (int i = 0; i < sizeTwo; i++) {
      arrayy[i] = two[i];
    }
    len = sizeTwo;
  }
  else if (num == 3) {
    for (int i = 0; i < sizeThree; i++) {
      arrayy[i] = three[i];
    }
    len = sizeThree;
  }
  else if (num == 4) {
    for (int i = 0; i < sizeFour; i++) {
      arrayy[i] = four[i];
    }
    len = sizeFour;
  }
  else if (num == 5) {
    for (int i = 0; i < sizeFive; i++) {
      arrayy[i] = five[i];
    }
    len = sizeFive;
  }
  else if (num == 6) {
    for (int i = 0; i < sizeSix; i++) {
      arrayy[i] = six[i];
    }
    len = sizeSix;
  }
  else if (num == 7) {
    for (int i = 0; i < sizeSeven; i++) {
      arrayy[i] = seven[i];
    }
    len = sizeSeven;
  }
  else if (num == 8) {
    for (int i = 0; i < sizeEight; i++) {
      arrayy[i] = eight[i];
    }
    len = sizeEight;
  }
  else if (num == 9) {
    for (int i = 0; i < sizeNine; i++) {
      arrayy[i] = nine[i];
    }
    len = sizeNine;
  }
  for (int i = 0; i < len; i+=2) {
    int x = arrayy[i];
    int y = arrayy[i+1];
    //int x = zero[i];
    //int y = zero[i+1];
    if (side) {
      x+=17;
    }
    createPixelFromCoords(x, y, true);
  }
}

void clearSide(bool side) {
  if (side) {
    for (int i = 0; i<4; i++) {
      matrix.clearDisplay(addrSide1[i]);
    }
    Serial.println("side1");
  }
  else {
    for (int i = 0; 4; i++) {
      matrix.clearDisplay(addrSide0[i]);
    }
    Serial.println("side0");
  }
}

String message = "";

and I'm not sending the arrays cause they're too long and I've already tested them.

    for (int i = 0; 4; i++) {

Looks a little odd ...

1 Like

What data is being given to read? Is CRLF part of the String()? Would you post the whole sketch?

You can simplify the pixel algorithm with: PIX = row * NUM_COLS + col

UK, I fixed that and also I completely changed my system to add 1 rather than to set it to a specific value so it works now, sorry for wasting your time.

I am glad that you got it working

For the benefit of anyone with a similar problem, please post your updated working sketch

I strongly recommend to put all of these in an array. It will save you tons of code...

@craxypt, +1 on both

and


Changing it to handle a few things differently would be some ppls' idea of fun; doing would not make sense without seeing a complete sketch.

TIA

a7

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