coding error: using rgb ledstrip (ws2812) to make 2 digit countdown timer

hey guys i am currently creating a project in which I am using an infrared sensor as an object counter and displaying the number of counts onto a 3-digit 7 segment display (with the use of an rgb ledstrip).

The 7 segment display is only displaying the counts from 0-9 (singe digit) and when it reaches double digits it stops counting.

i’d like to know what is wrong or missing from my code

Here is my code:

include <Adafruit_NeoPixel.h>

#define PIN 2
#define LEDSPERSEGMENT 1
int ir_pin = 3;
int counter = 0;
int hitObject = false;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(42, PIN, NEO_GRB + NEO_KHZ800);

unsigned long DigitBytes[] = {
0x0003FFFF, // 0
0x000001F8, // 1
0x001C7E3F, // 2
0x001C0FFF, // 3
0x001F81F8, // 4
0x001F8FC7, // 5
0x001FFFC7, // 6
0x000001FF, // 7
0x001FFFFF, // 8
0x001F81FF // 9
};

void setup() {
strip.begin();
Serial.begin(9600);
pinMode(ir_pin,INPUT);
}

void showDigit(int d, int offset) {
unsigned long digit = DigitBytes[d];

for (int i = offset; i < offset + 21; i++) {
if (digit & 1) {
strip.setPixelColor(i, 0xFF0000);
} else {
strip.setPixelColor(i, 0x000000);
}
digit = digit >> 1;
}
strip.show();
}

void showScore(int score) {
int ones = score % 10;
int tens = score / 10;
int hundreds = score / 100;

showDigit(ones, (LEDSPERSEGMENT * 7));
showDigit(tens, (LEDSPERSEGMENT * 7));
showDigit(hundreds, (LEDSPERSEGMENT * 7));

Serial.print(ones);
Serial.println(tens);
Serial.println(hundreds);

}

void loop() {
int val = digitalRead(ir_pin);

if( (val == 0) && (hitObject == false) ){
counter++;
hitObject = true;

}else if( (val ==1) && (hitObject == true) ) {
hitObject = false;
}

showDigit(counter,0);
showDigit(counter, 21);


Serial.println(counter);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Your array DigitBytes[] has 10 entries: 0 ... 9.

In your loop() function, you call showDigit(counter,0);
In showDigit() do you this:

unsigned long digit = DigitBytes[d];

When counter is greater than 9, you read a value from outside the bounds of your array.

Also, you seem to display the same single digit twice:

showDigit(counter,0);
showDigit(counter, 21);

You have a showScore() function, but you never call it.
In this function, you have some more problems:

  • If score is greater than 99, then tens will be greater than 9. (And if score > 999, then hundreds > 9.)
  • LEDSPERSEGMENT is defined as 1, that does not seem right.
  • You use the same offset for all 3 digits.

@skennyboi, stop cross-posting. Thread locked.