Maintenance: Project Hub will be unavailable on Monday 24 (9AM to 6PM CET) while we deploy critical improvements
Components and supplies
Arduino Mega 2560
Male/Female Jumper Wires
Jumper wires (generic)
RGB Diffused Common Cathode
Analog joystick (Generic)
Resistor 330 ohm
Apps and platforms
Arduino IDE
Project description
Code
RGB LED CODE
c_cpp
1const int SW_pin = 2; // digital pin connected to switch output 2const int X_pin = A0; // analog pin connected to X output 3const int Y_pin = A1; // analog pin connected to Y output 4 int redPin = 9; //red pin of RGB led plugged into digital pin 9 5 int greenPin = 10; // green pin plugged into digital pin 10 6 int bluePin = 11;//and so on 7int YPIN; //declaring YPIN an integer for later 8int XPIN; 9int SWPIN; 10void setup() { 11 pinMode(SW_pin, INPUT); // Switch pin is an input 12 digitalWrite(SW_pin, HIGH); //set to high 13 Serial.begin(9600); //begin serial monitor 14 pinMode(redPin, OUTPUT); // declares RGB LED as an output 15 pinMode(greenPin, OUTPUT); 16 pinMode(bluePin, OUTPUT); 17 18} 19 20void setColor(int red, int green, int blue) //sets up format for colors 21{ 22 analogWrite(redPin, red); 23 analogWrite(greenPin, green); 24 analogWrite(bluePin, blue); 25} 26 27void loop() { 28 int YPIN = analogRead(Y_pin); //sets the number thats comes out of analogRead(Y_pin); equal to a new variable 29 int XPIN = analogRead(X_pin); 30 int SWPIN = digitalRead(SW_pin); 31 32 Serial.print("Switch:"); //in serial monitor print switch 33 Serial.print(digitalRead(SW_pin)); //next to switch print the value of SW_pin 34 Serial.print(" "); 35 Serial.print("X-axis:"); 36 Serial.print(analogRead(X_pin)); 37 Serial.print(" "); 38 Serial.print("Y-axis:"); 39 Serial.println(analogRead(Y_pin)); 40 if(YPIN == 0) {setColor(100, 80, 0); delay(100);} //if YPIN is equal to 0, set the color 41 else {setColor(0, 0, 0);} 42 43 if(YPIN == 1023) {setColor(225, 0, 225); delay(100);} 44 else {setColor(0, 0, 0);} 45 46 if(XPIN == 0) {setColor(0, 0, 225); delay(100);} 47 else {setColor(0, 0, 0);} 48 49 if(XPIN == 1023) {setColor(0, 225, 0); delay(100);} 50 else {setColor(0, 0, 0);} 51 52 if(SWPIN == LOW) {setColor(80, 20, 0); delay(100);} //if SWPIN is low set color 53 else {setColor(0, 0, 0);} 54 55} 56 57
RGB LED CODE
c_cpp
1const int SW_pin = 2; // digital pin connected to switch output 2const int X_pin = A0; // analog pin connected to X output 3const int Y_pin = A1; // analog pin connected to Y output 4 int redPin = 9; //red pin of RGB led plugged into digital pin 9 5 int greenPin = 10; // green pin plugged into digital pin 10 6 int bluePin = 11;//and so on 7int YPIN; //declaring YPIN an integer for later 8int XPIN; 9int SWPIN; 10void setup() { 11 pinMode(SW_pin, INPUT); // Switch pin is an input 12 digitalWrite(SW_pin, HIGH); //set to high 13 Serial.begin(9600); //begin serial monitor 14 pinMode(redPin, OUTPUT); // declares RGB LED as an output 15 pinMode(greenPin, OUTPUT); 16 pinMode(bluePin, OUTPUT); 17 18} 19 20void setColor(int red, int green, int blue) //sets up format for colors 21{ 22 analogWrite(redPin, red); 23 analogWrite(greenPin, green); 24 analogWrite(bluePin, blue); 25} 26 27void loop() { 28 int YPIN = analogRead(Y_pin); //sets the number thats comes out of analogRead(Y_pin); equal to a new variable 29 int XPIN = analogRead(X_pin); 30 int SWPIN = digitalRead(SW_pin); 31 32 Serial.print("Switch:"); //in serial monitor print switch 33 Serial.print(digitalRead(SW_pin)); //next to switch print the value of SW_pin 34 Serial.print(" "); 35 Serial.print("X-axis:"); 36 Serial.print(analogRead(X_pin)); 37 Serial.print(" "); 38 Serial.print("Y-axis:"); 39 Serial.println(analogRead(Y_pin)); 40 if(YPIN == 0) {setColor(100, 80, 0); delay(100);} //if YPIN is equal to 0, set the color 41 else {setColor(0, 0, 0);} 42 43 if(YPIN == 1023) {setColor(225, 0, 225); delay(100);} 44 else {setColor(0, 0, 0);} 45 46 if(XPIN == 0) {setColor(0, 0, 225); delay(100);} 47 else {setColor(0, 0, 0);} 48 49 if(XPIN == 1023) {setColor(0, 225, 0); delay(100);} 50 else {setColor(0, 0, 0);} 51 52 if(SWPIN == LOW) {setColor(80, 20, 0); delay(100);} //if SWPIN is low set color 53 else {setColor(0, 0, 0);} 54 55} 56 57
Downloadable files
Joystick RGB LED Schematic
Joystick RGB LED Schematic
Comments
Only logged in users can leave comments
Sdavis25
7 years ago
Please feel free to post any questions, or issues here.