Devices & Components
Arduino Uno Rev3
KY-016
Software & Tools
Arduino IDE
Project description
Code
coloursweeper
c_cpp
The code will have a RGB LED connected to pins 11, 12 and 13 sweep through all colours in the spectrum. One cycle lasts 6144 milliseconds.
1/* 2 * COLOURSWEEPER 3 * by Johan Halmen 4 * credits to Brett Hagman 5 * 6 * Make sure you have installed 7 * the SoftPWM library by Brett Hagman 8 * 9 */ 10 11#include "SoftPWM.h" 12 13int bPin = 13; 14int gPin = 12; 15int rPin = 11; 16 17void setup() { 18 SoftPWMBegin(); 19 SoftPWMSet(rPin, 0); 20 SoftPWMSet(gPin, 0); 21 SoftPWMSet(bPin, 0); 22 SoftPWMSetFadeTime(rPin, 0, 0); 23 SoftPWMSetFadeTime(gPin, 0, 0); 24 SoftPWMSetFadeTime(bPin, 0, 0); 25 delay(1000); 26} 27 28void loop() { 29 int total, phase, delta; 30 long int now, then = 0; 31 now = millis(); 32 total = now % 6144; // The cycle lasts 6144 milliseconds (6 * 1024) 33 phase = total / 1024; // Divide the cycle into 6 phases, think of each 34 // phase as sliding one slide of three slides on 35 // a light board representing the red, green and blue 36 // channels of your light. 37 38 delta = (total % 1024) >> 2; // Have the "slide" value vary from 0 to 255, 39 // the delta value increments every four milliseconds 40 41 if (delta != then) // Update the slides only if the millisecond clock 42 // has ticked four times 43 { 44 switch (phase) 45 { 46 // Every second phase one slide goes up, 47 // every other second one slide goes down 48 case 0 : 49 SoftPWMSet(gPin, delta); // green goes up 50 break; 51 case 1 : 52 SoftPWMSet(rPin, 255 - delta); // red goes down 53 break; 54 case 2 : 55 SoftPWMSet(bPin, delta); // etc, you get the idea 56 break; 57 case 3 : 58 SoftPWMSet(gPin, 255 - delta); 59 break; 60 case 4 : 61 SoftPWMSet(rPin, delta); 62 break; 63 case 5 : 64 SoftPWMSet(bPin, 255 - delta); 65 break; 66 } 67 then = delta; // Save the delta time for this last 68 // update of the slides 69 } 70} 71
coloursweeper
c_cpp
The code will have a RGB LED connected to pins 11, 12 and 13 sweep through all colours in the spectrum. One cycle lasts 6144 milliseconds.
1/* 2 * COLOURSWEEPER 3 * by Johan Halmen 4 * credits to Brett Hagman 5 * 6 * Make sure you have installed 7 * the SoftPWM library by Brett Hagman 8 * 9 */ 10 11#include "SoftPWM.h" 12 13int bPin = 13; 14int gPin = 12; 15int rPin = 11; 16 17void setup() { 18 SoftPWMBegin(); 19 SoftPWMSet(rPin, 0); 20 SoftPWMSet(gPin, 0); 21 SoftPWMSet(bPin, 0); 22 SoftPWMSetFadeTime(rPin, 0, 0); 23 SoftPWMSetFadeTime(gPin, 0, 0); 24 SoftPWMSetFadeTime(bPin, 0, 0); 25 delay(1000); 26} 27 28void loop() { 29 int total, phase, delta; 30 long int now, then = 0; 31 now = millis(); 32 total = now % 6144; // The cycle lasts 6144 milliseconds (6 * 1024) 33 phase = total / 1024; // Divide the cycle into 6 phases, think of each 34 // phase as sliding one slide of three slides on 35 // a light board representing the red, green and blue 36 // channels of your light. 37 38 delta = (total % 1024) >> 2; // Have the "slide" value vary from 0 to 255, 39 // the delta value increments every four milliseconds 40 41 if (delta != then) // Update the slides only if the millisecond clock 42 // has ticked four times 43 { 44 switch (phase) 45 { 46 // Every second phase one slide goes up, 47 // every other second one slide goes down 48 case 0 : 49 SoftPWMSet(gPin, delta); // green goes up 50 break; 51 case 1 : 52 SoftPWMSet(rPin, 255 - delta); // red goes down 53 break; 54 case 2 : 55 SoftPWMSet(bPin, delta); // etc, you get the idea 56 break; 57 case 3 : 58 SoftPWMSet(gPin, 255 - delta); 59 break; 60 case 4 : 61 SoftPWMSet(rPin, delta); 62 break; 63 case 5 : 64 SoftPWMSet(bPin, 255 - delta); 65 break; 66 } 67 then = delta; // Save the delta time for this last 68 // update of the slides 69 } 70} 71
Downloadable files
The components
I didn't draw any schematics, since it's all about just plugging one component with 4 male pins into another component with 4 female pins. Locate the GND or "-" pin of the KY-016. This goes into the GND pin of the UNO, next to digital pin 13. Don't bother if the red, green and blue pins of your KY-016 are in another order, as long as they go to pins 11 to 13 and the GND or "-" pin goes to the GND of the UNO.
The components

Comments
Only logged in users can leave comments