Hello there,
As i am not the professional in coding, i am seeking for help.
I can sit for weeks every evening behind my laptop, trying to change just 1 piece of code. If i get tired of it, i just walk away and try again the next day, but this one is bugging me for months now...
I am really struggling with my scoreboard, trying to count up numbers with a keypad. I am allmost shure it is just a few lines of code i need to change/add, but i am at a dead end really.
I made a basketball scoreboard with 560 ws2812b LED's. Hooked up a home made controller with 30 buttons via Keypad matrix.
I can set a number on the board with the press of a button, but for the life of me, i can't figure out how to count up from 0 to 5 with that button.
The code i show here, is just for the setting of the Period. The complete code is for score home, score guests, faults home, faults guests, period and timer.
For testing (the timer works, thanks to another thread here), i removed all irrelevant code.
If i can figure out how to count up the numbers for period, i can allso do this for the rest.
#include <FastLED.h>
#define LED_PIN1 7 // pin for score, faults, periods#define NUM_LEDS1 332 // leds for score, faults, periods
#define NUM_LEDS1 332 // leds for score, faults, periods
#define BRIGHTNESS 50 // overall brightness for scoreboard
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define ROWS 6 // number of rows for controller
#define COLS 5 // number of cols for controller
#include <Keypad.h>
uint8_t rowPins[ROWS] = {22, 24, 26, 28, 30, 32};
uint8_t colPins[COLS] = {23, 25, 27, 29, 31};
char keys[ROWS][COLS] = {
{'1','2','3','4','5'},
{'6','7','8','9','0'},
{'a','b','c','d','e'},
{'f','g','h','i','j'},
{'k','l','m','n','o'},
{'p','q','r','s','t'}
};
bool lit[ROWS*COLS] = {0};
CRGB leds1[NUM_LEDS1];
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Structure of a number in segments
#define SegmentA 8
#define SegmentB 16
#define SegmentC 48
#define SegmentD 40
#define SegmentE 32
#define SegmentF 0
#define SegmentG 24
// What LED segments are needed for numbers?
// Segments orientation: H V V H V V H
// Segments used: A B C D E F G For number:
byte SegmentNumbers[10][7] = { { 1,1,1,1,1,1,0 }, // 0
{ 0,1,1,0,0,0,0 }, // 1
{ 1,1,0,1,1,0,1 }, // 2
{ 1,1,1,1,0,0,1 }, // 3
{ 0,1,1,0,0,1,1 }, // 4
{ 1,0,1,1,0,1,1 }, // 5
{ 1,0,1,1,1,1,1 }, // 6
{ 1,1,1,0,0,0,0 }, // 7
{ 1,1,1,1,1,1,1 }, // 8
{ 1,1,1,1,0,1,1 } }; // 9
// Horizontal/Vertical number of LEDs per segment
#define SegmentHSize 8
#define SegmentVSize 8
// Total number of LEDs per number
#define TotalSegmentSize (3*SegmentHSize)+(4*SegmentVSize)
#define StartLEDPeriod 266
void SetPeriod(int StartLED, CRGB PeriodColor, int Period) {
for (int SetPeriod=0; SetPeriod<5; SetPeriod++) {
}
SetDigit1(StartLED, PeriodColor, Period);
FastLED.show();
}
void SetDigit1(int StartLED, CRGB DigitColor, int DigitValue) {
//SetDigitDark(StartLED); // set LED to dark, do not make this change visible yet (avoid flicker)
if( SegmentNumbers[DigitValue][0]==1 ) { SetSegment(StartLED+SegmentA, SegmentHSize, DigitColor); } // A = horizontal
if( SegmentNumbers[DigitValue][1]==1 ) { SetSegment(StartLED+SegmentB, SegmentVSize, DigitColor); } // B = vertical
if( SegmentNumbers[DigitValue][2]==1 ) { SetSegment(StartLED+SegmentC, SegmentVSize, DigitColor); } // C = vertical
if( SegmentNumbers[DigitValue][3]==1 ) { SetSegment(StartLED+SegmentD, SegmentHSize, DigitColor); } // D = horizontal
if( SegmentNumbers[DigitValue][4]==1 ) { SetSegment(StartLED+SegmentE, SegmentVSize, DigitColor); } // E = vertical
if( SegmentNumbers[DigitValue][5]==1 ) { SetSegment(StartLED+SegmentF, SegmentVSize, DigitColor); } // F = vertical
if( SegmentNumbers[DigitValue][6]==1 ) { SetSegment(StartLED+SegmentG, SegmentHSize, DigitColor); } // G = horizontal
}
void SetSegment(int StartLED, int LEDCount, CRGB SegmentColor) {
for(int i=0; i<LEDCount; i++) {
leds1[StartLED+i] =SegmentColor;
}
}
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN1, COLOR_ORDER>(leds1, NUM_LEDS1).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS ); // Set overall brightness
}
void loop() {
char key = keypad.getKey();
if (key == 'g') {
for (int Period=0; Period<5; Period++) {
SetPeriod(266, CRGB::Blue, (Period));
}
}
FastLED.show();
}
If i push button "g" with the above code, an "8" lights up, what i do not understand.
If i change the code to :
void loop() {
char key = keypad.getKey();
if (key == 'g') {
for (int Period=0; Period<5; Period++) {
SetPeriod(266, CRGB::Blue, 4);
}
}
a "4" lights up (what i understand!)
If i change the code to :
void SetPeriod(int StartLED, CRGB PeriodColor, int Period) {
SetDigit1(StartLED, PeriodColor, Period);
FastLED.show();
}
void loop() {
char key = keypad.getKey();
if (key == 'g') {
SetPeriod(266, CRGB::Blue, 4);
}
FastLED.show();
still it shows a "4".
As far as i think, i have to tell the code to add a number via a for loop, but whatever i try (and i tried a lot) it won't work.
Lots of examples on the internet, but they all use button-pins, not a keypad.
Is there someone who can set me in the right direction? If i understand this piece of code, i think i can work it out for the rest of my scoreboard too.
I am not asking for THE solution, but a guidance in the right direction.
I tried NoiascaNeopixelDisplay, but this allso uses button-pins.
If the complete code is desired, i will add it.
Thanks for helping out.