Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
mohammadsohail0008
Published © GPL3+

Joystick with RGB Led

Controlling RGB led with Joystick

IntermediateFull instructions provided396
Joystick with RGB Led

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Analog joystick (Generic)
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Resistor 330 ohm
Resistor 330 ohm
×3
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram

Code

Code

Arduino
int R_PIN = 9; //Pin for the red portion of the LED
int G_PIN = 10; //Pin for the red portion of the LED
int B_PIN = 11; //Pin for the red portion of the LED
int JX_PIN = 0; //Pin for the Joystick X Direction (Analog)
int JY_PIN = 1; //Pin for the Joystick Y Direction (Analog)
int JSW_PIN = 2; //Pin for the Joystick Switch (Analog)


int joyX; //Variable to store the Joystick X reading
int joyY; //Variable to store the Joystick Y reading
int joySW; //Variable to store the Joystick Switch Reading

//Setup the Arduino
void setup() 
{
  pinMode(R_PIN, OUTPUT); //Make the pin you used an output on the Arduino
  pinMode(G_PIN, OUTPUT); //Make the pin you used an output on the Arduino
  pinMode(B_PIN, OUTPUT); //Make the pin you used an output on the Arduino
}

//This function will update the RGB LED when called
void setRGB(int rLed, int gLed, int bLed)
{
  analogWrite(R_PIN, rLed);
  analogWrite(G_PIN, gLed);
  analogWrite(B_PIN, bLed);
}

//This code will run infinitely
void loop() 
{

  delay(100); //Delay 100mS to slow to 10 readings per second
  joyX = analogRead(JX_PIN); //Read the X position
  joyY = analogRead(JY_PIN); //Read the Y position
  joySW = analogRead(JSW_PIN); //Read the Switch, 255 or 0

  setRGB(joyX, joyY, joySW); //Set the RGB output to the inputs from the joystick
  
}

Credits

mohammadsohail0008
42 projects • 31 followers

Comments