Is it possible to use or millis() with a PS2 Joystick module and FastLED?

So, I am using an Arduino NANO, a PS2 Joystick Module and a WS2812B 8x8 RGB LED Matrix Module.

I'm using the PS2 Joystick Module to tell the NANO to display stuff on the Matrix when it reach a certain value.

This code for example. It shows a left arrow on the matrix when I push the joystick to the left.

  if (x <= 80) {          // Left Arrow
      for(int showTime = 0; showTime <5; showTime++) {
    FastLED.clear();
    leds[3] = CRGB(255, 18, 0);
    leds[4] = CRGB(255, 18, 0);
    leds[11] = CRGB(255, 18, 0);
    leds[12] = CRGB(255, 18, 0);
    leds[19] = CRGB(255, 18, 0);
    leds[20] = CRGB(255, 18, 0);
    leds[24] = CRGB(255, 18, 0);
    leds[27] = CRGB(255, 18, 0);
    leds[28] = CRGB(255, 18, 0);
    leds[31] = CRGB(255, 18, 0);
    leds[32] = CRGB(255, 18, 0);
    leds[33] = CRGB(255, 18, 0);
    leds[35] = CRGB(255, 18, 0);
    leds[36] = CRGB(255, 18, 0);
    leds[38] = CRGB(255, 18, 0);
    leds[39] = CRGB(255, 18, 0);
    leds[41] = CRGB(255, 18, 0);
    leds[42] = CRGB(255, 18, 0);
    leds[43] = CRGB(255, 18, 0);
    leds[44] = CRGB(255, 18, 0);
    leds[45] = CRGB(255, 18, 0);
    leds[46] = CRGB(255, 18, 0);
    leds[50] = CRGB(255, 18, 0);
    leds[51] = CRGB(255, 18, 0);
    leds[52] = CRGB(255, 18, 0);
    leds[53] = CRGB(255, 18, 0);
    leds[59] = CRGB(255, 18, 0);
    leds[60] = CRGB(255, 18, 0);
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.show();

My sloppy code is working properly as it is (without the for loop), but I want the stuff on the Matrix to blink and cancel the said blink when I push the button for reset or when I push the joystick to another direction where it would do some blinking again, just with a different image or thing displayed on the Matrix.

I have tried a lot of different codes from millis(), StateChangeDetection, for loop iterations, while, do...while, etc.. and just can't make my code do what I wanted to.

I am at a loss, atm. Up to the point of just discarding the joystick and just go with buttons to make my life more easier but, I have hope. I think this is possible. I just don't know how to do it properly.

The closest thing that resembles something that I wanted to do was by using the for loop iteration and while, do...while, tho I know that it's not the right approach.

Anyway, here is my full code. [This code is currently just making the left arrow blink]

I used basic if statements from the Arduino Playground, some commands from the FastLED library, and the PS2 Joystick Example code from the IDE.

#include "FastLED.h"  // Include FastLED Library
#define Vx A0 // Define / Equate "Vx" with A0, the pin where Vx is connected
#define Vy A1 // Define / Equate "Vy" with A1, the pin where Vy is connected
#define Button A2 // Define / Equate Button with A2, the pin where the button is connected
#define NUM_LEDS 64 //  The number of LEDS on your matrix
#define DATA_PIN 2  //  Data-In of your matrix
#define BRIGHTNESS 25
CRGB leds[NUM_LEDS];

void setup() {

  pinMode(Vx, INPUT); // Configure Vx (A0) as an Input
  pinMode(Vy, INPUT); // Configure Vy (A1) as an Input
  pinMode(Button, INPUT_PULLUP); // Configure Button (A2) as an Input, internally "pulled-up" to 5V
  // Note, we're configuring an Analog input as digital input
  // which is perfectly fine.  I did this to make the wiring easier
  // and keep all of the wires on the same side of the board
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(9600);

}

void loop() {

  int x, y, btn;

  x = analogRead(Vx); // Read the analog value of Vx (Analog Values are from 0-1023 which equate to 0V to 5V)
  y = analogRead(Vy); // Read the analog value of Vy
  btn = digitalRead(Button); // Read the button.  When the button is open (unpushed),
  // the input will read High (+5V)
  // When the button is closed (pressed), the input pin
  // is connected to ground and will read Low (0V)

  // Uncomment this part to see the values on serial monitor, then setup your values for the Joystick
  // Serial.print(x);  // Print the X value to the serial port
  // Serial.print("\t"); // Print a Tab character
  // Serial.print(y);  // Print the Y value
  // Serial.print("\t"); // Print a Tab
  // Serial.println(btn); // Print the value of the Btn (0=Pushed, 1 = Not Pushed)
  // delay(250); // Delay 250ms so the results don't print too quickly


  if (x <= 80) {          // Left Arrow
    for (int showTime = 0; showTime < 5; showTime++) {
      FastLED.clear();
      leds[3] = CRGB(255, 18, 0);
      leds[4] = CRGB(255, 18, 0);
      leds[11] = CRGB(255, 18, 0);
      leds[12] = CRGB(255, 18, 0);
      leds[19] = CRGB(255, 18, 0);
      leds[20] = CRGB(255, 18, 0);
      leds[24] = CRGB(255, 18, 0);
      leds[27] = CRGB(255, 18, 0);
      leds[28] = CRGB(255, 18, 0);
      leds[31] = CRGB(255, 18, 0);
      leds[32] = CRGB(255, 18, 0);
      leds[33] = CRGB(255, 18, 0);
      leds[35] = CRGB(255, 18, 0);
      leds[36] = CRGB(255, 18, 0);
      leds[38] = CRGB(255, 18, 0);
      leds[39] = CRGB(255, 18, 0);
      leds[41] = CRGB(255, 18, 0);
      leds[42] = CRGB(255, 18, 0);
      leds[43] = CRGB(255, 18, 0);
      leds[44] = CRGB(255, 18, 0);
      leds[45] = CRGB(255, 18, 0);
      leds[46] = CRGB(255, 18, 0);
      leds[50] = CRGB(255, 18, 0);
      leds[51] = CRGB(255, 18, 0);
      leds[52] = CRGB(255, 18, 0);
      leds[53] = CRGB(255, 18, 0);
      leds[59] = CRGB(255, 18, 0);
      leds[60] = CRGB(255, 18, 0);
      FastLED.setBrightness(BRIGHTNESS);
      FastLED.show();
      delay(500);
      FastLED.clear();
      FastLED.show();
      delay(500);
    }
  }


  if (x >= 1000) {            // Right Arrow

    FastLED.clear();
    leds[3] = CRGB(255, 18, 0);
    leds[4] = CRGB(255, 18, 0);
    leds[10] = CRGB(255, 18, 0);
    leds[11] = CRGB(255, 18, 0);
    leds[12] = CRGB(255, 18, 0);
    leds[13] = CRGB(255, 18, 0);
    leds[17] = CRGB(255, 18, 0);
    leds[18] = CRGB(255, 18, 0);
    leds[19] = CRGB(255, 18, 0);
    leds[20] = CRGB(255, 18, 0);
    leds[21] = CRGB(255, 18, 0);
    leds[22] = CRGB(255, 18, 0);
    leds[24] = CRGB(255, 18, 0);
    leds[25] = CRGB(255, 18, 0);
    leds[27] = CRGB(255, 18, 0);
    leds[28] = CRGB(255, 18, 0);
    leds[30] = CRGB(255, 18, 0);
    leds[31] = CRGB(255, 18, 0);
    leds[32] = CRGB(255, 18, 0);
    leds[35] = CRGB(255, 18, 0);
    leds[36] = CRGB(255, 18, 0);
    leds[39] = CRGB(255, 18, 0);
    leds[43] = CRGB(255, 18, 0);
    leds[44] = CRGB(255, 18, 0);
    leds[51] = CRGB(255, 18, 0);
    leds[52] = CRGB(255, 18, 0);
    leds[59] = CRGB(255, 18, 0);
    leds[60] = CRGB(255, 18, 0);
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.show();
  }

  if (y <= 10) {    // Green LEDs or Go

    FastLED.clear();
    fill_solid (leds, NUM_LEDS, CRGB(0, 255, 0));
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.show();
  }
  if (y >= 1000) {  // Red LEDs or Stop

    FastLED.clear();
    fill_solid (leds, NUM_LEDS, CRGB(255, 0, 0));
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.show();
  }

  if (btn == 0) {   // Reset or Clear

    FastLED.clear();
    FastLED.show();
  }
}

Please point me to the right direction if this question has been asked before and any code shortening tips would be much appreciated.

Thanks.

I don't understand what the problem is. You have some code that does something, but you haven't done an adequate job of describing what the code actually does.

You want the code to do something, but it isn't clear how what it actually does differs from what you want.

On any given pass through loop(), you have two completely independent things you want to do.

  1. Determine what to show on the matrix, based on the joystick position and possibly other things.
  2. Show something on the matrix.

And, yet, your code mixes those two things.

You should learn to write functions, so that loop() looks like this:

void loop()
{
   determineWhatToShow();
   showIt();
}

That way, there is NO possibility of intermingling code.

PaulS:
You should learn to write functions, so that loop() looks like this:

void loop()

{
  determineWhatToShow();
  showIt();
}




That way, there is NO possibility of intermingling code.

Sorry, you mean, something like this?

void loop() {

  
  if (analogRead(Vx) <= 80)    {      // Left Arrow
     for (int LeftArrowBlink = 0; LeftArrowBlink <10; LeftArrowBlink++)  {
     
      FastLED.clear();
      leds[3] = CRGB(255, 18, 0);
      leds[4] = CRGB(255, 18, 0);
      leds[11] = CRGB(255, 18, 0);
      leds[12] = CRGB(255, 18, 0);
      leds[19] = CRGB(255, 18, 0);
      leds[20] = CRGB(255, 18, 0);
      leds[24] = CRGB(255, 18, 0);
      leds[27] = CRGB(255, 18, 0);
      leds[28] = CRGB(255, 18, 0);
      leds[31] = CRGB(255, 18, 0);
      leds[32] = CRGB(255, 18, 0);
      leds[33] = CRGB(255, 18, 0);
      leds[35] = CRGB(255, 18, 0);
      leds[36] = CRGB(255, 18, 0);
      leds[38] = CRGB(255, 18, 0);
      leds[39] = CRGB(255, 18, 0);
      leds[41] = CRGB(255, 18, 0);
      leds[42] = CRGB(255, 18, 0);
      leds[43] = CRGB(255, 18, 0);
      leds[44] = CRGB(255, 18, 0);
      leds[45] = CRGB(255, 18, 0);
      leds[46] = CRGB(255, 18, 0);
      leds[50] = CRGB(255, 18, 0);
      leds[51] = CRGB(255, 18, 0);
      leds[52] = CRGB(255, 18, 0);
      leds[53] = CRGB(255, 18, 0);
      leds[59] = CRGB(255, 18, 0);
      leds[60] = CRGB(255, 18, 0);
      FastLED.setBrightness(BRIGHTNESS);
      FastLED.show();
      delay(500);
      FastLED.clear();
      FastLED.show();
      delay(500);     
}
}
}

Sorry, you mean, something like this?

No, I don't. Displaying the data does not mean blocking while the matrix is drawn to/erased 10 times, while nothing else happens.

PaulS:
No, I don't. Displaying the data does not mean blocking while the matrix is drawn to/erased 10 times, while nothing else happens.

Hello, it's me again. So, I've been reading about function declarations and I think I know how to use it now. Thank you.

void loop() {

  if (analogRead(Vx) <= 80) {
    LeftArrowBlink();
  }
  if (analogRead(Vx) >= 1000) {
    RightArrowBlink();
  }
  if (analogRead(Vy) <= 10) {
    BlinkGreen();
  }
  if (analogRead(Vy) >= 1000) {
    BlinkRed();
  }
  if (analogRead(Button) == 0)  {
    Clear();
  }
}

  
void LeftArrowBlink() {
  
    FastLED.clear();
    leds[3] = CRGB(255, 18, 0);
    leds[4] = CRGB(255, 18, 0);
    leds[11] = CRGB(255, 18, 0);
    leds[12] = CRGB(255, 18, 0);
    leds[19] = CRGB(255, 18, 0);
    leds[20] = CRGB(255, 18, 0);
    leds[24] = CRGB(255, 18, 0);
    leds[27] = CRGB(255, 18, 0);
    leds[28] = CRGB(255, 18, 0);
    leds[31] = CRGB(255, 18, 0);
    leds[32] = CRGB(255, 18, 0);
    leds[33] = CRGB(255, 18, 0);
    leds[35] = CRGB(255, 18, 0);
    leds[36] = CRGB(255, 18, 0);
    leds[38] = CRGB(255, 18, 0);
    leds[39] = CRGB(255, 18, 0);
    leds[41] = CRGB(255, 18, 0);
    leds[42] = CRGB(255, 18, 0);
    leds[43] = CRGB(255, 18, 0);
    leds[44] = CRGB(255, 18, 0);
    leds[45] = CRGB(255, 18, 0);
    leds[46] = CRGB(255, 18, 0);
    leds[50] = CRGB(255, 18, 0);
    leds[51] = CRGB(255, 18, 0);
    leds[52] = CRGB(255, 18, 0);
    leds[53] = CRGB(255, 18, 0);
    leds[59] = CRGB(255, 18, 0);
    leds[60] = CRGB(255, 18, 0);
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.show();
  }


void RightArrowBlink()  {
  
    FastLED.clear();
    leds[3] = CRGB(255, 18, 0);
    leds[4] = CRGB(255, 18, 0);
    leds[10] = CRGB(255, 18, 0);
    leds[11] = CRGB(255, 18, 0);
    leds[12] = CRGB(255, 18, 0);
    leds[13] = CRGB(255, 18, 0);
    leds[17] = CRGB(255, 18, 0);
    leds[18] = CRGB(255, 18, 0);
    leds[19] = CRGB(255, 18, 0);
    leds[20] = CRGB(255, 18, 0);
    leds[21] = CRGB(255, 18, 0);
    leds[22] = CRGB(255, 18, 0);
    leds[24] = CRGB(255, 18, 0);
    leds[25] = CRGB(255, 18, 0);
    leds[27] = CRGB(255, 18, 0);
    leds[28] = CRGB(255, 18, 0);
    leds[30] = CRGB(255, 18, 0);
    leds[31] = CRGB(255, 18, 0);
    leds[32] = CRGB(255, 18, 0);
    leds[35] = CRGB(255, 18, 0);
    leds[36] = CRGB(255, 18, 0);
    leds[39] = CRGB(255, 18, 0);
    leds[43] = CRGB(255, 18, 0);
    leds[44] = CRGB(255, 18, 0);
    leds[51] = CRGB(255, 18, 0);
    leds[52] = CRGB(255, 18, 0);
    leds[59] = CRGB(255, 18, 0);
    leds[60] = CRGB(255, 18, 0);
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.show();
  }


void BlinkGreen()  {
  FastLED.clear();
  fill_solid (leds, NUM_LEDS, CRGB::Green);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();  
  }


void BlinkRed()  {
  FastLED.clear();
  fill_solid (leds, NUM_LEDS, CRGB::Red);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();
  }


void Clear()  {
  FastLED.clear();
  FastLED.show();
}

The problem I am having now is that, the void Clear() function is not being called even if I pushed the button and even if the value is 0 on serial monitor.

I think I also need to use millis() instead of for() loops on this one. I just can't wrap my head around it, atm.

The problem I am having now is that, the void Clear() function is not being called even if I pushed the button and even if the value is 0 on serial monitor.

Nothing in your code prints to the serial port, so it is impossible for you to see 0 in the Serial Monitor application, with that code.

Your functions with blink in the name do NOT make any blinking happen. So, the names are wrong.

You do NOT need to read the two analog pins twice each to determine what to do.

Switches are NOT analog devices, so using analogRead() to get the state of a switch, from a pin stupidly named Button, is wrong.

Other than that, good job.

PaulS:
Switches are NOT analog devices, so using analogRead() to get the state of a switch, from a pin stupidly named Button, is wrong.

Other than that, good job.

Well, I just copied the joystick code from the IDE and used it along with the connections.

Anyway, here's my updated code.

#include "FastLED.h"  // Include FastLED Library
#define Vx A0 // Define / Equate "Vx" with A0, the pin where Vx is connected
#define Vy A1 // Define / Equate "Vy" with A1, the pin where Vy is connected
#define Button A2 // Define / Equate Button with A2, the pin where the button is conne
#define NUM_LEDS 64 //  The number of LEDS on your matrix
#define DATA_PIN 2  //  Data-In of your matrix
#define BRIGHTNESS 25
CRGB leds[NUM_LEDS];

void setup() {


  pinMode(Vx, INPUT); // Configure Vx (A0) as an Input
  pinMode(Vy, INPUT); // Configure Vy (A1) as an Input
  pinMode(Button, INPUT_PULLUP); // Configure Button (A2) as an Input, internally "pulled-up" to 5v
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

}

void loop() {

  if (analogRead(Vx) <= 80) { // X-Axis Left
    LeftArrowBlink();
  }
  if (analogRead(Vx) >= 1000) { // X-Axis Right
    RightArrowBlink();
  }
  if (analogRead(Vy) <= 10) { // Y-Axis Up
    BlinkGreen();
  }
  if (analogRead(Vy) >= 1000) { // Y-Axis Down
    BlinkRed();
  }
  if (digitalRead(Button) == 0) {
    Clear();
}
}

void LeftArrowBlink() { // Function that is called when Vx is <= 80 and shows a Left Blinking Arrow on the matrix

  FastLED.clear();
  leds[3] = CRGB(255, 18, 0);
  leds[4] = CRGB(255, 18, 0);
  leds[11] = CRGB(255, 18, 0);
  leds[12] = CRGB(255, 18, 0);
  leds[19] = CRGB(255, 18, 0);
  leds[20] = CRGB(255, 18, 0);
  leds[24] = CRGB(255, 18, 0);
  leds[27] = CRGB(255, 18, 0);
  leds[28] = CRGB(255, 18, 0);
  leds[31] = CRGB(255, 18, 0);
  leds[32] = CRGB(255, 18, 0);
  leds[33] = CRGB(255, 18, 0);
  leds[35] = CRGB(255, 18, 0);
  leds[36] = CRGB(255, 18, 0);
  leds[38] = CRGB(255, 18, 0);
  leds[39] = CRGB(255, 18, 0);
  leds[41] = CRGB(255, 18, 0);
  leds[42] = CRGB(255, 18, 0);
  leds[43] = CRGB(255, 18, 0);
  leds[44] = CRGB(255, 18, 0);
  leds[45] = CRGB(255, 18, 0);
  leds[46] = CRGB(255, 18, 0);
  leds[50] = CRGB(255, 18, 0);
  leds[51] = CRGB(255, 18, 0);
  leds[52] = CRGB(255, 18, 0);
  leds[53] = CRGB(255, 18, 0);
  leds[59] = CRGB(255, 18, 0);
  leds[60] = CRGB(255, 18, 0);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();
  delay(500);
  FastLED.clear();
  FastLED.show();
  delay(500);
  
}


void RightArrowBlink()  {// Function that is called when Vx is >= 1000 and shows a Right Blinking Arrow on the matrix

  FastLED.clear();
  leds[3] = CRGB(255, 18, 0);
  leds[4] = CRGB(255, 18, 0);
  leds[10] = CRGB(255, 18, 0);
  leds[11] = CRGB(255, 18, 0);
  leds[12] = CRGB(255, 18, 0);
  leds[13] = CRGB(255, 18, 0);
  leds[17] = CRGB(255, 18, 0);
  leds[18] = CRGB(255, 18, 0);
  leds[19] = CRGB(255, 18, 0);
  leds[20] = CRGB(255, 18, 0);
  leds[21] = CRGB(255, 18, 0);
  leds[22] = CRGB(255, 18, 0);
  leds[24] = CRGB(255, 18, 0);
  leds[25] = CRGB(255, 18, 0);
  leds[27] = CRGB(255, 18, 0);
  leds[28] = CRGB(255, 18, 0);
  leds[30] = CRGB(255, 18, 0);
  leds[31] = CRGB(255, 18, 0);
  leds[32] = CRGB(255, 18, 0);
  leds[35] = CRGB(255, 18, 0);
  leds[36] = CRGB(255, 18, 0);
  leds[39] = CRGB(255, 18, 0);
  leds[43] = CRGB(255, 18, 0);
  leds[44] = CRGB(255, 18, 0);
  leds[51] = CRGB(255, 18, 0);
  leds[52] = CRGB(255, 18, 0);
  leds[59] = CRGB(255, 18, 0);
  leds[60] = CRGB(255, 18, 0);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();
  delay(500);
  FastLED.clear();
  FastLED.show();
  delay(500);
}


void BlinkGreen()  {// Function that is called when Vy is <= 10 and shows a blinking solid_fill of Green LEDs on the matrix
  FastLED.clear();
  fill_solid (leds, NUM_LEDS, CRGB::Green);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();
  delay(500);
  FastLED.clear();
  FastLED.show();
  delay(500);
}


void BlinkRed()  {// Function that is called when Vy is >= 1000 and shows a blinking solid_fill of Red LEDs on the matrix
  FastLED.clear();
  fill_solid (leds, NUM_LEDS, CRGB::Red);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();
  delay(500);
  FastLED.clear();
  FastLED.show();
  delay(500);
}


void Clear()  {

  FastLED.clear();
  FastLED.show();
}

It does blink now. But, only while I am holding the position of the joystick. I need to find a way to be able to save or hold the value of the X or Y axis for it to continue on blinking even if the joystick returns to it's original position. Hmmm.. Do you think Debounce would help? Or calibration? Or if you know a better way to do it, that would be awesome.

Right! Just took a quick look at the example code. The "Button" is supposed to be digitalRead.

My bad.