![]() If you have a common anode RGB LED, one of the pins will be the anode (usually the longest pin) which is connected to the common positive (+) terminal.If you have a common cathode RGB LED, one of the pins will be the cathode (usually the longest pin or one indicated by a flat edge on the package) which is connected to the common negative (-) terminal.Each of these LEDs has a separate anode (positive terminal) and a common cathode (negative terminal) or a common anode and separate cathodes.An RGB LED contains three individual LEDs within a single package: one red, one green, and one blue.Here's how an RGB LED works with Arduino: By controlling the intensity of each of these colors, you can produce a wide range of colors, including white when all three are at full intensity. delay(500) //Used in debugging so one can read the serial output.An RGB LED (Red, Green, Blue Light Emitting Diode) is a specialized type of LED that can emit light in three primary colors: red, green, and blue. Serial.print("Douse=") //Remember, "DOUSE" is the xValue and it's role is to turn off red and blue LEDs Serial.println((x)) //until the Serial.begin(baud) is turned back on. Serial.print("X-axis: ") //I'm letting this still execute, but nothing will display GreenValue = constrain(greenValue, 0, 255) ĪnalogWrite(RED, redValue) //Make the lights turn on. ![]() RedValue = (map(x, 0, 512, 0, 255))-xValue //Set the red control to the left half of the X axis, then subtract the "Douse" valueīlueValue = (map(x, 512, 1023, 255, 0))-xValue //Set the blue control to the right half of the X axis, then subtract the "Douse" valueīlueValue = constrain(blueValue, 0, 255) //Make sure the range of power for each output is 0 to 255 GreenValue = (map(y, 0, 512, 0, 255)) //Set the green control to the lower half of the Y axis ![]() XValue = constrain (xValue, 0, 255) //Set the upper half range to 0 thru 255 ![]() XValue = (map(y, 512, 1023, 0, 255)) //Douse is the upper half of the Y axis Int xValue = 0 //xValue is later reffered to as "douse," as it's main role is to douse the red and and blue LEDs Y = abs(1023-(analogRead(Y_pin))) //Invert VRy read, as the module is inverted X = analogRead(X_pin) //Read VRx normally Serial.begin(9600) //Serial was used for debugging. digitalWrite(SW_pin, HIGH) //Swith currently unused pinMode(SW_pin, INPUT) //Swith currently unused const int SW_pin = 2 //Switch currently unusedĬonst int X_pin = 0 // VRx connected to A0Ĭonst int Y_pin = 1 // VRy connected to A1 Meaning, the axis on the joystick associated with redValue will dim RED Note that all the color value variables are used to dim the respective LED. So, I'm just going to post my code here for those looking for a more beginner level Joystick controlled RGB LED. ![]() Found other posts where people were looking for the same thing, but never really got their questions answered. I tried looking up if anyone else had done it, and they have, but when reading it, it just felt like their code was complicated, so I set out to make a simple one. I finished the joystick tutorial and thought it would be fun to have it control an RGB LED. I've been working my way through my Elegoo project box tutorials, but after every lesson, I try to branch off and figure out how to use what I just learned. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |