8/1/2023 0 Comments Arduino rgb led all colors![]() Now I will show you how to create a simple function that makes it easier to change the color of the RGB LED and also reduce the length of the code. So you get the basic idea of how to drive RGB LEDs. colors/colors_picker.asp Arduino Code const int redPin = 3 You can find any color of your choice and its R-G-B values using these online tools below. But for that, you need to have the R-G-B values of the desired color. In the below example, I will use the analogWrite() function to create different colors besides the base colors. By turning the pixels color dots on and off at different intensities, any number of colors and brightness can be created. There are 8 different modes for controlling your RGB LED color.} Get More Color Using the Arduino PWM Function You can see live data over there while rotating the encoder. open the serial monitor at the baud rate of 9600. RedVal = 255 - RGBVal // Red from off to fullīluVal = RGBVal // Blue from full to offĪfter successful upload of the program. GrnVal = RGBVal // Green from full to offīluVal = 255 - RGBVal // Blue from off to full GrnVal = 255 - RGBVal // Green from off to fullĮlse if (encVal < 171) // Middle third of range (86-170) RGBVal = (encVal * 3) // Normalize to 0-255 It has tree different color-emitting diodes that can be combined to. ![]() If (encVal < 86) // Lowest third of range (0-85) An RGB LED has 4 pins, one for each color (Red, Green, Blue) and a common cathode. GrnVal = 255 - encVal //Common Anode LED means inverse valuesĮlse if (stateNum = 1) //Blend Green to BlueĮlse if (stateNum = 2) //Blend Blue to RedĮlse if (stateNum = 3) //Blend Red to Green If (stateNum = 0) //All LEDs make white, off to brightest LastEncVal = encVal //Update encVal to reflect new valueĭigitalWrite(ledPin, HIGH) //Blink onboard LED If (encVal != lastEncVal) //Compare to previous pot value If (encVal > 255) encVal = 0 //Loop Values ![]() If (stateNum > 8) stateNum = 0 //Defines and loops Number of possible states If (buttonState = HIGH) //If button is pushed, incriment stateNum and blink onboard LED If (buttonState != lastButtonState) //Compare to last state Void intrEncChange2() //Read on interrupt Left turn - Slow -2īuttonState = digitalRead(SencPin) //Read Button state Void intrEncChange1() //Read on interrupt Right turn - Fast +4 Int redVal = 0 // Variables to store the values to send to the pinsĪttachInterrupt(0, intrEncChange1, CHANGE) ĪttachInterrupt(1, intrEncChange2, CHANGE) Int lastButtonState = 0 // previous state of the button Int buttonState = 0 // current state of the button digitalWrite (ledRed, 75) digitalWrite (ledBlue, 130) digitalWrite (ledGreen, 0) You need :- analogWrite (ledRed, 75) analogWrite (ledBlue, 130) analogWrite (ledGreen, 0) Providing that your ledRed, ledBlue and ledGreen pins are capable of PWM output, which on the Uno pin 3, 5 & 6 are. Int bluPin = 11 // Blue LED, connected to digital pin 11 Int grnPin = 10 // Green LED, connected to digital pin 10 Int redPin = 9 // Red LED, connected to digital pin 9 The RGB LED included in this kit has all the internal LEDs share the same ground wire, so there are four legs in total. Int ledPin = 13 // onboard LED for validation Int SencPin = 4 //Push Encoder switch to digital pin 4 Int encPin2 = 3 //Right turn of encoder to digital pin 3 ![]() Int encPin1 = 2 //Left turn of encoder to digital pin 2 Program Source Code for RGB LED Color Controller using Arduino & Rotary Encoder To upload the code, first select your Arduino Nano board and its COM port from the tools menu. To compile the code in Arduino IDE no extra library files are required. The following is the final program source code for RGB LED Controllerthat has 8 different modes. You can trust PCBWay for PCB & PCBA Services because the PCB quality they provide is very good. Choose the color of your PCB, choose your quantity and place an order. You can now upload the Gerber File to the Website.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |