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.