Joystick with RGB Led
Controlling RGB led with Joystick
Components and supplies
1
Analog joystick (Generic)
1
Arduino Nano R3
1
Jumper wires (generic)
1
Breadboard (generic)
1
RGB Diffused Common Cathode
3
Resistor 330 ohm
Apps and platforms
1
Arduino IDE
Project description
Code
Code
arduino
1int R_PIN = 9; //Pin for the red portion of the LED 2int G_PIN = 10; //Pin for the red portion of the LED 3int B_PIN = 11; //Pin for the red portion of the LED 4int JX_PIN = 0; //Pin for the Joystick X Direction (Analog) 5int JY_PIN = 1; //Pin for the Joystick Y Direction (Analog) 6int JSW_PIN = 2; //Pin for the Joystick Switch (Analog) 7 8 9int joyX; //Variable to store the Joystick X reading 10int joyY; //Variable to store the Joystick Y reading 11int joySW; //Variable to store the Joystick Switch Reading 12 13//Setup the Arduino 14void setup() 15{ 16 pinMode(R_PIN, OUTPUT); //Make the pin you used an output on the Arduino 17 pinMode(G_PIN, OUTPUT); //Make the pin you used an output on the Arduino 18 pinMode(B_PIN, OUTPUT); //Make the pin you used an output on the Arduino 19} 20 21//This function will update the RGB LED when called 22void setRGB(int rLed, int gLed, int bLed) 23{ 24 analogWrite(R_PIN, rLed); 25 analogWrite(G_PIN, gLed); 26 analogWrite(B_PIN, bLed); 27} 28 29//This code will run infinitely 30void loop() 31{ 32 33 delay(100); //Delay 100mS to slow to 10 readings per second 34 joyX = analogRead(JX_PIN); //Read the X position 35 joyY = analogRead(JY_PIN); //Read the Y position 36 joySW = analogRead(JSW_PIN); //Read the Switch, 255 or 0 37 38 setRGB(joyX, joyY, joySW); //Set the RGB output to the inputs from the joystick 39 40}
Code
arduino
1int R_PIN = 9; //Pin for the red portion of the LED 2int G_PIN = 10; //Pin for the red portion of the LED 3int B_PIN = 11; //Pin for the red portion of the LED 4int JX_PIN = 0; //Pin for the Joystick X Direction (Analog) 5int JY_PIN = 1; //Pin for the Joystick Y Direction (Analog) 6int JSW_PIN = 2; //Pin for the Joystick Switch (Analog) 7 8 9int joyX; //Variable to store the Joystick X reading 10int joyY; //Variable to store the Joystick Y reading 11int joySW; //Variable to store the Joystick Switch Reading 12 13//Setup the Arduino 14void setup() 15{ 16 pinMode(R_PIN, OUTPUT); //Make the pin you used an output on the Arduino 17 pinMode(G_PIN, OUTPUT); //Make the pin you used an output on the Arduino 18 pinMode(B_PIN, OUTPUT); //Make the pin you used an output on the Arduino 19} 20 21//This function will update the RGB LED when called 22void setRGB(int rLed, int gLed, int bLed) 23{ 24 analogWrite(R_PIN, rLed); 25 analogWrite(G_PIN, gLed); 26 analogWrite(B_PIN, bLed); 27} 28 29//This code will run infinitely 30void loop() 31{ 32 33 delay(100); //Delay 100mS to slow to 10 readings per second 34 joyX = analogRead(JX_PIN); //Read the X position 35 joyY = analogRead(JY_PIN); //Read the Y position 36 joySW = analogRead(JSW_PIN); //Read the Switch, 255 or 0 37 38 setRGB(joyX, joyY, joySW); //Set the RGB output to the inputs from the joystick 39 40}
Downloadable files
Circuit Diagram
Circuit Diagram

Comments
Only logged in users can leave comments