Go Down

Topic: RGB Led Strip Controller (Read 61 times) previous topic - next topic

Arator

Apr 10, 2015, 09:56 pm Last Edit: Apr 10, 2015, 10:09 pm by Arator
Hello guys,

I was thinking about some new stuff i could try on my arduino and thought
that it could be a good idea to start making some videos about the arduino.
And when im already sharing my knowledge on youtube, why shouldn't i share
it here aswell? :)

Let's start with the code:
Code: [Select]

//LED
#define led_r 44
#define led_g 45
#define led_b 46
//Potentiometer
#define pot A0
//Buttons
#define bt_h 7
#define bt_v 5
#define bt_s 6

typedef struct
{
  float r;  //Red
  float g;  //Green
  float b;  //Blue
}RGB;

typedef struct
{
  float h;  //Hue
  float s = 0.9;  //Saturation
  float v = 0.9;  //Value
}HSV;

//Global Variables
RGB rgbval;
HSV hsvval;
int potval;
boolean automaticMode;

void setup()
{
  Serial.begin(9600);
  pinMode(led_r, OUTPUT);
  pinMode(led_g, OUTPUT);
  pinMode(led_b, OUTPUT);
  pinMode(pot, INPUT); //Optional
  pinMode(bt_h, INPUT);
  pinMode(bt_s, INPUT);
  pinMode(bt_v, INPUT);
}

void hsv2rgb()
{
  float h = hsvval.h;
  float s = hsvval.s;
  float v = hsvval.v;
  
  float c = v * s;
  float h1 = h / 60;
  float x = c * (1 - fabs((fmod(h1,2) - 1)));
  
  float r,g,b;
  if(0 <= h1 && h1 < 1)
  {
    r = c;
    g = x;
    b = 0;
  }else if (1 <= h1 && h1 < 2)
  {
    r = x;
    g = c;
    b = 0;
  }
  else if (2 <= h1 && h1 < 3)
  {
    r = 0;
    g = c;
    b = x;
  }
  else if (3 <= h1 && h1 < 4)
  {
    r = 0;
    g = x;
    b = c;
  }
  else if (4 <= h1 && h1 < 5)
  {
    r = x;
    g = 0;
    b = c;
  }
  else if (5 <= h1 && h1 < 6)
  {
    r = c;
    g = 0;
    b = x;
  }
  else
  {
    rgbval.r = 0;
    rgbval.g = 0;
    rgbval.b = 0;  
  }
  
  float m = v - c;
  rgbval.r = r + m;
  rgbval.g = g + m;
  rgbval.b = b + m;  
}

void printDebug()
{
  Serial.print("h:");
  Serial.print(hsvval.h);
  Serial.print("s:");
  Serial.print(hsvval.s);
  Serial.print("v:");
  Serial.print(hsvval.v);
  
  Serial.print(" ---> r:");
  Serial.print((int)(rgbval.r*255));
  Serial.print("g:");
  Serial.print((int)(rgbval.g*255));
  Serial.print("b:");
  Serial.println((int)(rgbval.b*255));
}

void loop()
{
  potval = analogRead(pot);  
  hsvval.v = (1024.0-potval)/1024.0;
  
  if(digitalRead(bt_h))
  {
    //(rgbval.r <= 255) ? rgbval.r += 0.01 : rgbval.r = 0;
    automaticMode = false;
    (hsvval.h <= 358) ? hsvval.h += 1 : hsvval.h = 0;
  }
  if(digitalRead(bt_s))
  {
    //(rgbval.g <= 255) ? rgbval.g += 0.01 : rgbval.g = 0;
    automaticMode = false;
    (hsvval.s < 1) ? hsvval.s += 0.01 : hsvval.s = 0;
  }
  if(digitalRead(bt_v))
  {
    //(rgbval.b <= 255) ? rgbval.b += 0.01 : rgbval.b = 0;
    automaticMode = false;
    (hsvval.v < 1) ? hsvval.v += 0.01 : hsvval.v = 0;
  }  
  
  
  
  if(digitalRead(bt_h)  && digitalRead(bt_v))
  {
      automaticMode = !automaticMode;
      hsvval.h = 0;
      hsvval.s = 1;
      hsvval.v = 1;
      delay(200);
  }
  if(automaticMode)
  {
    (hsvval.h <= 358) ? hsvval.h += 1 : hsvval.h = 0;  
  }
  
  hsv2rgb(); //Calculate RGB values from HSV
  printDebug();
  
  analogWrite(led_r, rgbval.r*255);
  analogWrite(led_g, rgbval.g*255);
  analogWrite(led_b, rgbval.b*255);
}


And I got a circuit aswell!
You can download an image of it here: http://bit.ly/1yjclUu
And you can download the original file (fritzing): http://bit.ly/1OjT9ts

This project should be helpful especially to beginners ;)

And of corse you can check out my video if you want: https://www.youtube.com/watch?v=Db3-8pDjcWc
It is a brief overview of the whole project, but there will be some more in depth videos on this
channel aswell.

And that's pretty much it, if you want to contact me you can write me a message here,
or check out my twitter account: https://twitter.com/AratorYT

Have a nice day!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy