Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int red = 0;
- const int green = 1;
- const int blue = 2;
- float current_hue = 0;
- float current_saturation = 0;
- int ledPins[] = {
- 6, 5, 3};
- int ledPWM[] = {
- 0, 85, 170};
- int ledPWM_i[] = {
- 1, 1, 1};
- void setup(){
- Serial.begin(9600);
- for(int i = 0; i < 3; i++){
- pinMode( ledPins[i], OUTPUT);
- }
- }
- void loop(){
- //animate_rainbow();
- //control_rainbow();
- combo_rainbow();
- delay(10);
- }
- void combo_rainbow(){
- current_hue ++;
- if(current_hue > 360){
- current_hue = 0;
- }
- current_saturation = analogRead(0);
- current_saturation = map(current_saturation, 0, 1023, 0, 100);
- hsbToRgb(current_hue, current_saturation, 100);
- for(int i = 0; i < 3; i++){
- analogWrite(ledPins[i], ledPWM[i]);
- Serial.print(ledPWM[i]);
- Serial.print(" ");
- }
- Serial.println();
- }
- void control_rainbow(){
- current_hue = analogRead(0);
- current_hue = map(current_hue, 0, 1023, 0, 359);
- hsbToRgb(current_hue, 100, 100);
- for(int i = 0; i < 3; i++){
- analogWrite(ledPins[i], ledPWM[i]);
- Serial.print(ledPWM[i]);
- Serial.print(" ");
- }
- Serial.println();
- }
- void animate_rainbow(){
- current_hue ++;
- if(current_hue > 360){
- current_hue = 0;
- }
- hsbToRgb(current_hue, 100, 100);
- for(int i = 0; i < 3; i++){
- analogWrite(ledPins[i], ledPWM[i]);
- Serial.print(ledPWM[i]);
- Serial.print(" ");
- }
- Serial.println();
- }
- //-------------------------------------------------
- // Turns HSB color to RGB color
- //-------------------------------------------------
- void hsbToRgb(float _hue, float _sat, float _bri) {
- if (_bri == 0) {
- // black
- // blue
- ledPWM[0] = 0;
- // green
- ledPWM[1] = 0;
- // red
- ledPWM[2] = 0;
- return;
- }
- if (_bri == 100 && _sat == 0){
- // white
- // blue
- ledPWM[0] = 255;
- // green
- ledPWM[1] = 255;
- // red
- ledPWM[2] = 255;
- return;
- }
- float red_HSB = 0;
- float green_HSB = 0;
- float blue_HSB = 0;
- _hue = map(_hue, 0, 360, 0, 360);
- _sat = map(_sat, 0, 100, 0, 100);
- _bri = map(_bri, 0, 100, 0, 100);
- if(_hue == 360) _hue = 0;
- _hue /= 60;
- _sat /= 100;
- _bri /= 100;
- int i = (int)floor(_hue);
- float f = _hue - i;
- float p = _bri*(1-_sat);
- float q = _bri*(1-(_sat*f));
- float t = _bri*(1-(_sat*(1-f)));
- switch (i) {
- case 0:
- red_HSB = _bri;
- green_HSB = t;
- blue_HSB = p;
- break;
- case 1:
- red_HSB = q;
- green_HSB = _bri;
- blue_HSB = p;
- break;
- case 2:
- red_HSB = p;
- green_HSB = _bri;
- blue_HSB = t;
- break;
- case 3:
- red_HSB = p;
- green_HSB = q;
- blue_HSB = _bri;
- break;
- case 4:
- red_HSB = t;
- green_HSB = p;
- blue_HSB = _bri;
- break;
- case 5:
- red_HSB = _bri;
- green_HSB = p;
- blue_HSB = q;
- break;
- }
- int r = (int)round(red_HSB*255);
- int g = (int)round(green_HSB*255);
- int b = (int)round(blue_HSB*255);
- // blue
- ledPWM[0] = r;
- // green
- ledPWM[1] = g;
- // red
- ledPWM[2] = b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement