Joystick & LED PIXEL Project

Im working on a code on ESP32. Im trying to move a joystick and then make lights appear on the LED PIXEL that comes with the kit.

My questions is, do you guys have any tips on how i can make the joy sticks placement choose which led on the led pixel that i want to turn on.

X,Y,Z: 1819, 1903, 1

that are the placements of the joystick while its standing still.

Do you guys have any code examples?

Welcome to the forum

Have you got any code that reads the joystick ?

If so then you will have values between 0 and 1023 for each. You can translate those into other value ranges using the map() function in order to determine the required pixel position

If X and Y can be such high values then exactly which "LED PIXEL" (whatever that is) do you have and does it also have a Z dimension ?

Which "kit" do you have ?

1 Like

Hi @UKHeliBob! As @armandiniho is using an ESP32 it looks as if the TO is using one of the two 12-bit ADC, so the range goes up to 4095 (I assume this from the center position data in Post # 1).

There is an article about the use

https://w4krl.com/esp32-analog-to-digital-conversion-accuracy/

that also covers linearity questions of the ESP32 ADC and the problems at the lower end as well as the non-linearity at the upper end.

Hope that assists in finding a solution ...

1 Like

Well spotted. I missed that

I am particularly intrigued by the "LED PIXEL" having an X, Y and Z axis

That needs clarification :wink:

Thank you, i have the Freenove Ultimate Starter Kit for ESP32 (FNK0047).

The values that i get from the joystick is from 0-4095.
And the Z dimension goes from 0-1, so the X and Y dimensions go to 4095.

I did'nt take the map() function in consideration while making the code so i will do that, thanks.

Also the LED's from the "LED PIXEL" goes from 0-8

Skjermbilde 2022-02-09 120851

The LED PIXEL module does not have and x,y and z axis but the joy stick of course does, do you have any idea of how i can map these values from the joystick's x,y and z to the LED PIXEL values?

image

This is the readings from the joystick

image

What it requires is just a short description of the result you are looking for; heres some examples what could be done:

  • X value shall change color from RGB = {...} to RGB = {...}
  • Y value shall change brightness from 0 to 255
    or
  • X value shall control Red from .. to ...
  • Y value shall control Green (or Blue?) from ... to ...
    or
  • ...

As most joysticks do have two dimensions (unless you have a 3D mouse/joystick that can be lifted) the Z value will stay 1. Or Z might address a button at the joystick ?!?

Once you have made up your mind what to do, the data can be mapped accordingly.

That is also a relevant information! I looked this kit up and found a manual and the specific library at github:

https://github.com/Freenove/Freenove_Ultimate_Starter_Kit_for_ESP32

Are you using the specific lib called "Freenove_WS2812_Lib_for_ESP32.h"?

If yes, unfortunately they do NOT mention or list all functions that this lib should make available. If you have downloaded the library, you could try to find it at the place where Arduino IDE stores the libraries (see the Sketch directory in the IDE preferences and go for the ..\libraries... there ) and open the corresponding cpp file in e.g. notepad++ to read the available functions.

If I am not wrong there are functions like

setAllLedsColorData(rgb); // where rgb is a 32 bit value
setAllLedsColorData(r, g, b); // where r, g, b are separate 8 bit data

Both functions include the show() command, so should immediately cause an effect.

As there are 8 of them I assume that you mean that they are numbered 0 to 7

What exactly do you want to do ?

Interpreting your original post I think that you want the LED corresponding to the current joystick light so that when the joystick is pushed forward the top LED lights and so on. Is that correct ?

What type of LEDs are they ?
Single, RGB or maybe WS2812 ?

You probably missed that information in post # 6, did you? The manual for that kit is stored at github (see my post # 11). It is based on digital control, not PWM...

Just as a very quick hint how to handle it (here as an example for Arduino UNO):

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// Released under the GPLv3 license to match the rest of the
// Adafruit NeoPixel library

// Modified by ec2021 to set all "pixels" to the same color
// depending on the status of the two sliding potentiometers


#include <Adafruit_NeoPixel.h>

#define PIN        6 
#define NUMPIXELS 16 // Popular NeoPixel ring size
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
#define XPin A0
#define YPin A1

void setup() {

  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels.clear(); // Set all pixel colors to 'off'

}

int Red   = 0;
int Green = 0;
int valX = 0;
int valY = 0;


void loop() {
  valX = analogRead(XPin);
  Red = map(valX, 0,1023,0,255);
  valY = analogRead(YPin);
  Green = map(valY, 0,1023,0,255);
  pixels.fill(pixels.Color(Red,Green, 0));
  pixels.show();   // Send the updated pixel colors to the hardware.
}

You can run, test and modify it at https://wokwi.com/arduino/projects/323130783705334356

Changes required for your purpose are

  • Change the library and according functions to control the Leds
  • Change the values 1023 to 4095 (and test it, as I mentioned above you may have to avoid values close to the lower end).
  • You could store the last RGB data set and only call the routines if the values have changed.

I used two sliding potis instead of one 2 axis joystick .

yes they are numbered from 0-7 and the leds are RGB or GRB. My goal here is when I put my joystick up then the leds at the top of the led pixel to light up.

The Z dimension works as a button on the joystick, it is not a 3D mouse.

I have coded it that when Z is pushed down which means that the value is 0. Then all the lights light up with rainbow colors.

I see, you do have a code and are only checking @UKHeliBob 's and my coding skills ?!? :wink:

Would you mind to share your code with us? That would allow us to give some direct advice rather than rough examples ... (not to mention that it might probably save some time ... :innocent: :slightly_smiling_face: )

sure, i have been working on the code since i posted the project. what i am trying to accomplish now is to make the LED's on the LED-PIXEL dissaper after one second after turning on.

here is the code.

#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);
}

void loop() {
  xVal = analogRead(xyzPins[0]);
  yVal = analogRead(xyzPins[1]);
  zVal = digitalRead(xyzPins[2]);
  Serial.printf("X,Y,Z: %d,\t%d,\t%d\n", xVal, yVal, zVal);
  delay(50);

  for (int i = 0; i < 8; i++) {
    strip.setLedColorData(i, 0, 0, 0);
  }

  pixelLedLys();

  alleLys();
}

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);
    }
  }
}

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 ... :innocent:) 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);
    }
  }
}