# Help With Tactile Button and RGB LED Needed

Hello everyone,

I really need some help, I have this code:

``````int potpin = A3;              // POT connected to analogue pin 3
int rpin = 11;                // Red
int gpin = 10;               // Green
int bpin = 9;               // Blue
float h;                     // Hue range
int h_int;                   // Hue color
int r = 0, g = 0, b = 0;           // Default RGB values

int val = 0;                   // Set POT value to default 0

void h2rgb(float h, int& R, int& G, int& B); // Instantiate h2rgb and it's variables  a.k.a  Hue to RGB

void setup()                    // Run once, when the sketch starts
{

}

void loop()                     // Run over and over again
{
val = analogRead(potpin);    // Read the pin and display the value
h = ((float)val)/1024;       // Get the range. pot value / 1024
h_int = (int) 360*h;         // Get the color hue by multiplying by 360

h2rgb(h,r,g,b);              // Call the h2rgb function passing it the hue value

analogWrite(rpin, r);        // Changes red led
analogWrite(gpin, g);        // Changes green led
analogWrite(bpin, b);        // Changes blue led

}

void h2rgb(float h, int& R, int& G, int& B) {

// Used HSV --> RGB function
// HSV - Hue, Saturation, Value
// RGB - Red, Green, Blue - example (255,255,255)
// Function below does a bunch of math to convert HSV values to RGB
int var_i;
float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

if ( S == 0 )                       //HSV values = 0 ÷ 1
{
R = V * 255;
G = V * 255;
B = V * 255;
}
else
{
var_h = h * 6;
if ( var_h == 6 ) var_h = 0;      //H must be < 1
var_i = int( var_h ) ;            //Or ... var_i = floor( var_h )
var_1 = V * ( 1 - S );
var_2 = V * ( 1 - S * ( var_h - var_i ) );
var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

if      ( var_i == 0 ) {
var_r = V     ;
var_g = var_3 ;
var_b = var_1 ;
}
else if ( var_i == 1 ) {
var_r = var_2 ;
var_g = V     ;
var_b = var_1 ;
}
else if ( var_i == 2 ) {
var_r = var_1 ;
var_g = V     ;
var_b = var_3 ;
}
else if ( var_i == 3 ) {
var_r = var_1 ;
var_g = var_2 ;
var_b = V     ;
}
else if ( var_i == 4 ) {
var_r = var_3 ;
var_g = var_1 ;
var_b = V     ;
}
else                   {
var_r = V     ;
var_g = var_1 ;
var_b = var_2 ;
}

R = (1-var_r) * 255;                  //RGB results = 0 ÷ 255
G = (1-var_g) * 255;
B = (1-var_b) * 255;
}
}
``````

And this basically uses a potentiometer to control/fade through the colour cycle on an RGB led, however I also want to add a piece of code which allows me to add two tactile buttons to it, one allowing me to choose a mode which cycles/fades through the colour cycle slowly and the other to snap between colours reasonably quick. I really hope someone can help.

Many Thanks
Jamie

Where did you get the code ? Whoever wrote this code would know the answer to your question which brings us to the question of "What DO you know about arduino programming ? You explained what your code does but you haven't researched how to connect a simple switch ? What's up with that? Do you know how to use an IF or CASE statement? Have you Googgled it?

I got the code of this website and I do know a bit aonly recently come back to it after a while. I have googled it but cannot dind anything?

Thanks
Jamie

Googgle "arduino switch"
and "arduino IF"
and
"arduino CASE"

Like I say im very much a beginner so I dont really understand, I just want a piece of code as doing a project on it.

Thanks
Jamie

Jamie77:
Like I say im very much a beginner so I dont really understand, I just want a piece of code as doing a project on it.

Thanks
Jamie

Sorry but this is not a code writing forum. What this is is a help forum, you write the code we help you with it. We do not write code to order or request.
If you want to pay someone to write some code for you then post in the Gigs & Collaboration section.

Good luck.
This isn't a program writing service so don't get your hopes up.
Your solution will probably involve

`````` while (switchpin == HIGH)
Do
// something
``````

raschemmel:
Googgle "arduino switch"
and "arduino IF"
and
"arduino CASE"

Don't even have to.

Just click the "Reference" button in the green bar at the top of this page.

Just click the "Reference" button in the green bar at the top of this page

I would have suggested that but I didn't want him to have a heart attack from information overload.

+1

Or should I say

++