0

I'm making a program to show numbers in a WS2812 LED and TM1637. I'm also using a common 4x4 keypad.

The WS2812 LED is just DIY with 25 x 10. Only two values can be displayed in a LED matrix just now. Without the LED part of the program, just the TM1637 and keypad connected to the Arduino, everything is fine. But adding the LED part of the program, I found out that entering 2 on keypad puts an erroneous value in my variable (-256 to exact). I tried debugging my code and found out that replacing the last iteration

for (int i = 225; i <= 231; i++) {

with this line

for (int i = 225; i <= 230; i++) {

mysteriously solved my problem. Any thoughts why this is happening?

#include <Keypad.h>
#include <Arduino.h>
#include <TM1637Display.h>
#include <Wire.h>
#include <FastLED.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
#define NUM_LEDS  225
#define LED_PIN   12
CRGB leds[NUM_LEDS];
TM1637Display display(CLK, DIO);
int DisplayNos=0;
int KeyNum;
char key;
boolean NewKey = false;
boolean DeleteAll=false;
int SavedNum;
// for 3 digits
int ExtractDigit[3] = {0, 0, 0};
byte DigitPoint= 1;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
//Create an object of keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(250);
}
void loop(){
int k;
//  uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };
//  uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 };
  display.setBrightness(0x0f);
  key = keypad.getKey();// Read the key
  // Print if key pressed
  if (key){
    Serial.print("Key Pressed : ");
    Serial.println(key);
    if (isDigit(key)){
      KeyNum = key-48;
      Serial.println(key);
      Serial.println(DisplayNos);
      if (DigitPoint==1){
        DisplayNos = 0;
        DisplayNos = KeyNum;
      }
      if (DigitPoint==2){
        DisplayNos = KeyNum + (DisplayNos*10);
      }
      if (DigitPoint==3){
        DisplayNos = KeyNum + (DisplayNos*10);
      }
      DigitPoint= DigitPoint+1;
      if (DigitPoint==4){
        DigitPoint=1;
      }
    }
      if (key=='*'){   // asterist key cancels all char
      DisplayNos = 0;
      DigitPoint = 1;
    }
    if (key=='#'){   // asterisk key cancels all chars

    }
    NewKey = true;
  }
  display.showNumberDec(DisplayNos,false,3,0);
  if (NewKey == true){
    DisplayLED();
    FastLED.show();
  }
}
void DisplayLED(){
int LedNum;
byte digitnos;
NewKey = false;
  ExtractDigit[3] = (DisplayNos/100)%10;
  ExtractDigit[2] = (DisplayNos/10)%10;
  ExtractDigit[1] = (DisplayNos)%10;
  for (int digitnos= 1; digitnos <= 3; digitnos++) {
    LedNum = (digitnos-1)*9;
    if (ExtractDigit[digitnos] == 1){
      leds[21-LedNum]= CRGB::Blue;
      leds[28+LedNum]= CRGB::Blue;
      leds[29+LedNum]= CRGB::Blue;
      leds[71-LedNum]= CRGB::Blue;
      leds[78+LedNum]= CRGB::Blue;
      leds[121-LedNum]= CRGB::Blue;
      leds[128+LedNum]= CRGB::Blue;
      leds[171-LedNum]= CRGB::Blue;
      leds[178+LedNum]= CRGB::Blue;
      leds[221-LedNum]= CRGB::Blue;
      leds[229+LedNum]= CRGB::Blue;
      leds[228+LedNum]= CRGB::Blue; 
      leds[227+LedNum]= CRGB::Blue;
    }
    if (ExtractDigit[digitnos] == 2){
      for (int i = 20; i <= 22; i++) {
        leds[i-LedNum] = CRGB::Blue;
      }     
      leds[26+LedNum]= CRGB::Blue;
      leds[30+LedNum]= CRGB::Blue;
      leds[68-LedNum]= CRGB::Blue;
      leds[74-LedNum]= CRGB::Blue;
      leds[76+LedNum]= CRGB::Blue;
      leds[122-LedNum]= CRGB::Blue;
      leds[128+LedNum]= CRGB::Blue;
      leds[170-LedNum]= CRGB::Blue;
      leds[180+LedNum]= CRGB::Blue;
      leds[218-LedNum]= CRGB::Blue;
      leds[225+LedNum]= CRGB::Blue;
      leds[226+LedNum]= CRGB::Blue;
      leds[227+LedNum]= CRGB::Blue;
      leds[228+LedNum]= CRGB::Blue;
      leds[229+LedNum]= CRGB::Blue;
      for (int i = 225; i <= 231; i++) {
        leds[i-LedNum] = CRGB::Blue;
      } 
    }
  }
  }

1 Answer 1

2
#define NUM_LEDS  225
...
CRGB leds[NUM_LEDS];
...
  leds[225 + LedNum] = CRGB::Blue;
  leds[226 + LedNum] = CRGB::Blue;
  leds[227 + LedNum] = CRGB::Blue;
  leds[228 + LedNum] = CRGB::Blue;
  leds[229 + LedNum] = CRGB::Blue; 

Your RGB matrix has 225 LEDs, but you're writing to RAM out of range and destroying their stored data and/or variables.

5
  • many thanks never think of that i will try to replace the value now... Nov 27, 2023 at 23:19
  • better rewrite complete digit creation, as an independent function or use neomatrix lib
    – Mykolaha99
    Nov 28, 2023 at 0:28
  • just curious Mykolaha99, what is wrong with my question, looks like somebody downvoted it..is it not related to this stack exchange? Sorry, i'm new to this and just want to improve next time my questions.. Nov 28, 2023 at 0:38
  • 1
    @LesterTherr Just ignore them. There are some people here who just go around downvoting questions. I've had my stuff downvoted, and I am a moderator. Someone liked your question enough to answer it, and that is what counts. :)
    – Nick Gammon
    Nov 28, 2023 at 5:22
  • @NickGammon, thanks for the explanation. Nov 28, 2023 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.