Thanks for posting your code; here are some changes, which you may try.
I have not investigated in detail what your functions
- pixelLedLys();
- alleLys();
are doing. So you may have to play around with delay()s inside those functions to keep their effect as long visible as you like.
What the sketch does (should do, I must say ...
) is:
- [Start] Read the joystick positions X and Y
- Read the joystick Z value (button)
- Create a light effect based on the X and Y when button Z is pressed (zVal == 0;)
- Clear all Leds and
- Go to [Start]
The stripe should become dark again when you release the button
#include "Freenove_WS2812_Lib_for_ESP32.h" // Inkluderer bibliotek Freenove_WS2812_Lib_for_ESP32.h fra Arduino biblioeket
int xVal;
int yVal;
int zVal;
int kanal = 20;
int xyzPins[] = {13, 12, 14}; //x,y,z pins
#define LEDS_COUNT 8
#define LEDS_PIN 2
#define CHANNEL 0
Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_RGB); // Henter funskjonen strip fra Freenove_ESP32_WS2812 og definerer denne
void setup() {
Serial.begin(115200);
pinMode(xyzPins[2], INPUT_PULLUP); //z axis is a button.
strip.begin(); // Begynner strip funksjonen
strip.setBrightness(50);
}
boolean ShowLeds = true;
void loop() {
GetJoyStick();
ClearAllLeds();
if (ShowLeds) {
ShowLeds = false;
pixelLedLys();
alleLys();
}
}
void GetJoyStick(){
xVal = analogRead(xyzPins[0]);
yVal = analogRead(xyzPins[1]);
zVal = digitalRead(xyzPins[2]);
ShowLeds = (zVal == 0);
Serial.printf("X,Y,Z: %d,\t%d,\t%d\n", xVal, yVal, zVal);
delay(50);
}
void ClearAllLeds(){
for (int i = 0; i < 8; i++) {
strip.setLedColorData(i, 0, 0, 0);
}
}
void pixelLedLys() {
int yMap = map(yVal, 0, 4095, 0, 2);
int xMap = map(xVal, 0, 4095, 0, 2);
// D1
if (xMap == 0 && yMap == 1) {
kanal = 0;
}
// D2
if (xMap == 0 && yMap == 0) {
kanal = 1;
}
// D3
if (yMap == 0 && xMap == 1) {
kanal = 2;
}
// D4
if (xVal == 4095 && yVal == 0) {
kanal = 3;
}
// D5
if (xMap == 2 && yMap == 1) {
kanal = 4;
}
// D6
if (xMap == 2 && yMap == 2) {
kanal = 5;
}
// D7
if (yMap == 2 && xMap == 1) {
kanal = 6;
}
// D8
if (xMap == 0 && yMap == 2) {
kanal = 7;
}
strip.setLedColorData(kanal, random(0, 255), random(0, 255), random(0, 255));
strip.show();
delay(5);
}
void alleLys() {
if (zVal == 0) {
for (int j = 0; j < 255; j += 1) {
for (int i = 0; i < LEDS_COUNT; i++) {
strip.setLedColorData(i, strip.Wheel((i * 256 / LEDS_COUNT + j) & 255));
}
strip.show();
delay(5);
}
}
}