In the void loop(), we first read the potentiometer’s value with analogRead(). Read potentiometer and choose a mode void loop() By calling this function, instead of 3 redundant lines, you just have one line, which is digitalWriteRGB() with 3 parameters. This function will help us reduce the code we write in the following.Įvery time you need to change the color of the RGB LED, you will need to call digitalWrite() 3 times. digitalWriteRGB() function void digitalWriteRGB(byte red, byte blue, byte green) Nothing to do for the potentiometer, as an analog pin is already in input mode by default. In the void setup(), we initialize all LEDs (in fact, the 3 legs of the RGB LED) to OUTPUT mode. One for the potentiometer, and one for each color of the LED – we write the code as if we were controlling 3 different LEDs. Setup RGB and potentiometer pins #define RGB_RED_PIN 11Īt first, as a best practice, we create a define for each pin we are going to use. Int mode = map(potentiometerValue, 0, 1023, 0, 6) Int potentiometerValue = analogRead(POTENTIOMETER_PIN) Void digitalWriteRGB(byte red, byte blue, byte green) We are going to use digitalWrite() – LED fully powered on/off, which means that we have a combination of 7 colors: You can see the RGB LED as a combination of 3 different LEDs that you control separately. ![]() In this first application, we are going to modify the color of the RGB LED when we turn the potentiometer knob.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |