Danny van den Brande
Published © CC BY-SA

Arduino - Controlling LEDs With PS2 Joystick - KY-023

Hello world! Today I made a simple example on how to use the KY-023. We are going to control a RGB Module and a 7 color LED Module.

BeginnerProtip1 hour2,451
Arduino - Controlling LEDs With PS2 Joystick - KY-023

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
KY-023 Joystick module
×1
KY-016 RGB LE module
×1
KY-034 7 Color led module
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

Schematic

Code

KY-023_PS2_JOYSTICK_CONTROL_LED.ino

Arduino
/*
Author: Danny van den Brande, Arduinosensors.nl. BlueCore Tech.
This example shows you how to use the ps2 joystick with arduino.
right left up and down have their own values, you can see these in the serial monitor. Its easy to modify the code.
*/
int FlashLed = 2; 
int RedLed = 3; 
int GreenLed = 4; 
int BlueLed = 5; 

int XVALUE = 0;
int YVALUE = 0;
int VALUE = 0;

void setup() {
  pinMode(FlashLed, OUTPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(GreenLed, OUTPUT);
  pinMode(BlueLed, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  XVALUE = analogRead(0);
  Serial.print("X:"); Serial.print(XVALUE, DEC);
  YVALUE = analogRead(1);
  Serial.print("  Y:"); Serial.print(YVALUE, DEC);
  
  if (XVALUE < 10) {
    digitalWrite(FlashLed, HIGH);
    digitalWrite(RedLed, LOW);
    digitalWrite(GreenLed, LOW);
    digitalWrite(BlueLed, LOW);
    Serial.print(" - FlashLed");
  }
  if (XVALUE > 1000) {
    digitalWrite(FlashLed, LOW);
    digitalWrite(RedLed, HIGH);
    digitalWrite(GreenLed, LOW);
    digitalWrite(BlueLed, LOW);
    Serial.print(" - RedLed");
  }
  if (YVALUE < 10) {
    digitalWrite(FlashLed, LOW);
    digitalWrite(RedLed, LOW);
    digitalWrite(GreenLed, HIGH);
    digitalWrite(BlueLed, LOW);
    Serial.print(" - GreenLed");
  }
  if (YVALUE > 1000) {
    digitalWrite(FlashLed, LOW);
    digitalWrite(RedLed, LOW);
    digitalWrite(GreenLed, LOW);
    digitalWrite(BlueLed, HIGH);
    Serial.print(" - BlueLed");
  }
  if((YVALUE<1000 && YVALUE>10) && (XVALUE<1000 && XVALUE>10)  ) {
    digitalWrite(FlashLed, LOW);
    digitalWrite(RedLed, LOW);
    digitalWrite(GreenLed, LOW);
    digitalWrite(BlueLed, LOW);
  }
  
  Serial.println();
  delay(100);
}

Credits

Danny van den Brande
36 projects • 110 followers
"Hello world."
Contact

Comments

Please log in or sign up to comment.