Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Sdavis25
Published © GPL3+

Control an RGB LED with a Joystick

This project takes a 5-pin joystick module and allows the user to control a 4-pin RGB LED with it.

BeginnerFull instructions provided9,125
Control an RGB LED with a Joystick

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Resistor 330 ohm
Resistor 330 ohm
×3
Jumper wires (generic)
Jumper wires (generic)
×3
Male/Female Jumper Wires
Male/Female Jumper Wires
×5
RGB Diffused Common Cathode
RGB Diffused Common Cathode
You can use a Common Anode RGB LED, just make sure to connect to 5v instead of ground
×1
Analog joystick (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Joystick RGB LED Schematic

Code

RGB LED CODE

C/C++
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output
 int redPin = 9; //red pin of RGB led plugged into digital pin 9
 int greenPin = 10; // green pin plugged into digital pin 10
 int bluePin = 11;//and so on
int YPIN; //declaring YPIN an integer for later
int XPIN;
int SWPIN;
void setup() {
  pinMode(SW_pin, INPUT); // Switch pin is an input
  digitalWrite(SW_pin, HIGH); //set to high
  Serial.begin(9600); //begin serial monitor
  pinMode(redPin, OUTPUT); // declares RGB LED as an output
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

}

void setColor(int red, int green, int blue) //sets up format for colors
{
  analogWrite(redPin, red); 
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

void loop() {
  int YPIN = analogRead(Y_pin);  //sets the number thats comes out of analogRead(Y_pin); equal to a new variable
  int XPIN = analogRead(X_pin);
  int SWPIN = digitalRead(SW_pin);
  
  Serial.print("Switch:"); //in serial monitor print switch
  Serial.print(digitalRead(SW_pin)); //next to switch print the value of SW_pin
  Serial.print("  ");
  Serial.print("X-axis:");
  Serial.print(analogRead(X_pin));
  Serial.print("  ");
  Serial.print("Y-axis:");
  Serial.println(analogRead(Y_pin));
  if(YPIN == 0) {setColor(100, 80, 0); delay(100);} //if YPIN is equal to 0, set the color
  else {setColor(0, 0, 0);}
  
  if(YPIN == 1023) {setColor(225, 0, 225); delay(100);}
    else {setColor(0, 0, 0);}
    
  if(XPIN == 0) {setColor(0, 0, 225); delay(100);}
    else {setColor(0, 0, 0);}

  if(XPIN == 1023) {setColor(0, 225, 0); delay(100);}
    else {setColor(0, 0, 0);}

   if(SWPIN == LOW) {setColor(80, 20, 0); delay(100);} //if SWPIN is low set color
    else {setColor(0, 0, 0);}

}

Credits

Sdavis25
0 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.