with a project that uses neopixels and the built-in led matrix the led matrix kind of flashes random LEDs that are already lit a little brighter whenever I update the neopixels. the matrix only glitches when LEDs on the neopixel are updating. it did not happen before i added neopixels and it isn't a power issue because it happens even when the neopixels are on the lowest brightness.
here are my codes, the first is the normal code and the second is optimised to show the problem by constantly updating
normal:
////////////////////////////////////////////////////
///////Arduino Uno R4 lamp by ArduinoPrints////////
//////////////////////////////////////////////////
////////////////////libraries////////////////////
#include <CtrlEnc.h>
#include <CtrlBtn.h>
#include <ArduinoGraphics.h>
#include <Arduino_LED_Matrix.h>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>
#include <BlockNot.h>
////////////////////etc////////////////////
BlockNot MenuTimer(6000);
#define LED_PIN A1
#define LED_COUNT 16
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
////////////////////variables////////////////////
char mode = 'n';
byte hue = 0;
byte saturation = 0;
byte brightness = 0;
//////////////////////////////////////////////////
/*
----------mode variable use guide----------
'n' = not changing anything
'h' = on hue in menu
's' = on saturation in menu
'b' = on brightness in menu
'H' = changing hue
'S' = changing saturation
'B' = changing brightness
*/
////////////////////led matrix////////////////////
ArduinoLEDMatrix matrix;
const uint32_t frameH[] = {
0x57654,
0x57765155,
0x76000700,
66
};
const uint32_t frameS[] = {
0x57654,
0x57765155,
0x76000070,
66
};
const uint32_t frameB[] = {
0x57654,
0x57765155,
0x76000007,
66
};
////////////////////CTRL////////////////////
// Define an onTurnleft handler.
void onTurnleft() {
MenuTimer.START(WITH_RESET);
Serial.println("rotary encoder turn left");
switch (mode) {
case 'n':
brightness--;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
case 'h':
mode = 'b';
break;
case 's':
mode = 'h';
break;
case 'b':
mode = 's';
break;
case 'H':
hue--;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
case 'S':
saturation--;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
case 'B':
brightness--;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
default:
break;
}
updateMatrix();
}
// Define an onTurnRight handler.
void onTurnRight() {
MenuTimer.START(WITH_RESET);
Serial.println("rotary encoder turn right");
switch (mode) {
case 'n':
brightness++;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
case 'h':
mode = 's';
break;
case 's':
mode = 'b';
break;
case 'b':
mode = 'h';
break;
case 'H':
hue++;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
case 'S':
saturation++;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
case 'B':
brightness++;
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
break;
default:
break;
}
updateMatrix();
}
// Define an onPress handler.
void onPress() {
MenuTimer.START(WITH_RESET);
Serial.println("button pressed");
switch (mode) {
case 'n':
mode = 'h';
break;
case 'h':
mode = 'H';
break;
case 's':
mode = 'S';
break;
case 'b':
mode = 'B';
break;
case 'H':
mode = 'h';
break;
case 'S':
mode = 's';
break;
case 'B':
mode = 'b';
break;
default:
break;
}
updateMatrix();
}
CtrlEnc encoder(A4, A3, onTurnleft, onTurnRight);
CtrlBtn button(A5, 15, onPress);
void setup() {
MenuTimer.stop();
Serial.begin(9600);
matrix.begin();
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
encoder.process();
button.process();
if (MenuTimer.triggered()) {
timerTrigger();
}
// delay(10);
}
void timerTrigger() {
switch (mode) {
case 'n':
MenuTimer.stop();
break;
case 'h':
MenuTimer.stop();
mode = 'n';
break;
case 's':
MenuTimer.stop();
mode = 'n';
break;
case 'b':
MenuTimer.stop();
mode = 'n';
break;
case 'H':
mode = 'h';
break;
case 'S':
mode = 's';
break;
case 'B':
mode = 'b';
break;
default:
break;
}
if (mode == 'h' || mode == 's' || mode == 'b') {
updateMatrix();
} else {
matrix.clear();
}
}
void updateMatrix() {
char buffer[4];
switch (mode) {
case 'n':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", brightness);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
case 'h':
matrix.loadFrame(frameH);
break;
case 's':
matrix.loadFrame(frameS);
break;
case 'b':
matrix.loadFrame(frameB);
break;
case 'H':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", hue);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
case 'S':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", saturation);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
case 'B':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", brightness);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
default:
break;
}
}
optimised
////////////////////////////////////////////////////
///////Arduino Uno R4 lamp by ArduinoPrints////////
//////////////////////////////////////////////////
////////////////////libraries////////////////////
#include <CtrlEnc.h>
#include <CtrlBtn.h>
#include <ArduinoGraphics.h>
#include <Arduino_LED_Matrix.h>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>
#include <BlockNot.h>
////////////////////etc////////////////////
BlockNot MenuTimer(6000);
#define LED_PIN A1
#define LED_COUNT 16
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
////////////////////variables////////////////////
char mode = 'n';
byte hue = 0;
byte saturation = 0;
byte brightness = 0;
//////////////////////////////////////////////////
/*
----------mode variable use guide----------
'n' = not changing anything
'h' = on hue in menu
's' = on saturation in menu
'b' = on brightness in menu
'H' = changing hue
'S' = changing saturation
'B' = changing brightness
*/
////////////////////led matrix////////////////////
ArduinoLEDMatrix matrix;
const uint32_t frameH[] = {
0x57654,
0x57765155,
0x76000700,
66
};
const uint32_t frameS[] = {
0x57654,
0x57765155,
0x76000070,
66
};
const uint32_t frameB[] = {
0x57654,
0x57765155,
0x76000007,
66
};
////////////////////CTRL////////////////////
// Define an onTurnleft handler.
void onTurnleft() {
MenuTimer.START(WITH_RESET);
Serial.println("rotary encoder turn left");
switch (mode) {
case 'n':
brightness--;
break;
case 'h':
mode = 'b';
break;
case 's':
mode = 'h';
break;
case 'b':
mode = 's';
break;
case 'H':
hue--;
break;
case 'S':
saturation--;
break;
case 'B':
brightness--;
break;
default:
break;
}
updateMatrix();
}
// Define an onTurnRight handler.
void onTurnRight() {
MenuTimer.START(WITH_RESET);
Serial.println("rotary encoder turn right");
switch (mode) {
case 'n':
brightness++;
break;
case 'h':
mode = 's';
break;
case 's':
mode = 'b';
break;
case 'b':
mode = 'h';
break;
case 'H':
hue++;
break;
case 'S':
saturation++;
strip.show();
break;
case 'B':
brightness++;
break;
default:
break;
}
updateMatrix();
}
// Define an onPress handler.
void onPress() {
MenuTimer.START(WITH_RESET);
Serial.println("button pressed");
switch (mode) {
case 'n':
mode = 'h';
break;
case 'h':
mode = 'H';
break;
case 's':
mode = 'S';
break;
case 'b':
mode = 'B';
break;
case 'H':
mode = 'h';
break;
case 'S':
mode = 's';
break;
case 'B':
mode = 'b';
break;
default:
break;
}
updateMatrix();
}
CtrlEnc encoder(A4, A3, onTurnleft, onTurnRight);
CtrlBtn button(A5, 15, onPress);
void setup() {
MenuTimer.stop();
Serial.begin(9600);
matrix.begin();
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
encoder.process();
button.process();
if (MenuTimer.triggered()) {
timerTrigger();
}
strip.fill(strip.ColorHSV(map(hue, 0, 255, 0, 65535), saturation, brightness), 0, 16);
strip.show();
}
void timerTrigger() {
switch (mode) {
case 'n':
MenuTimer.stop();
break;
case 'h':
MenuTimer.stop();
mode = 'n';
break;
case 's':
MenuTimer.stop();
mode = 'n';
break;
case 'b':
MenuTimer.stop();
mode = 'n';
break;
case 'H':
mode = 'h';
break;
case 'S':
mode = 's';
break;
case 'B':
mode = 'b';
break;
default:
break;
}
if (mode == 'h' || mode == 's' || mode == 'b') {
updateMatrix();
} else {
matrix.clear();
}
}
void updateMatrix() {
char buffer[4];
switch (mode) {
case 'n':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", brightness);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
case 'h':
matrix.loadFrame(frameH);
break;
case 's':
matrix.loadFrame(frameS);
break;
case 'b':
matrix.loadFrame(frameB);
break;
case 'H':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", hue);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
case 'S':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", saturation);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
case 'B':
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
sprintf(buffer, "%03d", brightness);
matrix.println(buffer);
matrix.endText();
matrix.endDraw();
break;
default:
break;
}
}
i also made videos for it the first is the normal code and the second is the optimised code.
normal. watch for glitching:
optimised for glitching: