HELP WITH RGB STRIP

Hello,

Im not great at programming but my problem is that I want to have two buttons and one potentiometer. The idea being each button selects a different mode and will loop until a different button is pressed. And the potentiometer to be able to select an individual colour.

I have tried so many different codes and just cannot get it to work and if its even possible.

Hopefully someone can help

Thanks
Jamie

Hi Jamie,

Hopefully someone can help

Yes, probably, but we are not psychic! Where is your sketch (use code tags!), your schematic (hand drawn is ok) and your description of what you want to happen (for example what are these 2 modes?)

Paul

Hello,

Thank you for your reply.
Firstly the Schematic is attached to the reply.
I have not done a huge amount of coding as just used examples of adafruit and websites such as that. But the first bit of code I have is to make the RGB fade through the outside of the colour wheel with a potentiometer which is the FIRST mode I want and I want this to override everything so if you start moving the potentiometer then it starts changing colours. But here is that piece of code:

int potpin = A0;              // POT connected to analogue pin 3
int rpin = 5;                // Red
int gpin = 6;               // Green
int bpin = 3;               // 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;
  }
}

The thing I want to happen is by pressing one of the buttons, it fades through the colours and keeps looping until I either press the other button or turn the potentiometer. I have the code for fading through colours automatically which is:

#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
 
#define FADESPEED 5     // make this higher to slow down
 
void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
 
 
void loop() {
  int r, g, b;
 
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}

The last mode is that when you press the other button it changes mode to flashing through each colour individually without a fade and again this need to be able to be overwritten by any other button or potentiometer and be able to loop until this happens. This is the only one I don’t have code for.

I have tried everything and cannot figure out how to do it as I’m not the best at programming an only ever do it every now and again so again hopefully its possible.

So...

Mode 1: fade through colours under control of pot
Mode 2: fade through colours slowly over time, ignoring pot

Correct?

Before anyone else makes these comments, I should say:

Sketch 1 is about the most inefficient way of doing that I've ever seen! All those floating point calculations that could just as easilly using integer maths.

Sketch 2 has lots of delay() functions in it, they will have to go, to allow rapid response to button presses. Otherwise it is also fading around the colour wheel but much more efficiently.

These are not crisitisms of you Jamie, I know you didn't write them!

The "schematic" on the other hand... well, its not a schematic at all, its one of those Fritzing "cartoons". You should know that many people here look down on them. But I can follow it, mostly. Some questions though:

That rgb led you show there. That represents your led strip, right? You wouldn't actually put an rgb led like the one shown there without series resistors, would you? It would just blow! (your led strip has built-in series resistors).

Your transistors. One is labelled "FET N", which would be correct, but the other two are labelled "FET P" which would be wrong in that place in the circuit. What are you actually using?

Your choice of wire colours is not "normal". Black is usually ground.

Anyway, a sketch to achieve what you want. Perfectly possible, but neither of the two you posted are a great way to start. I am busy this evening, but may be able to help get you started tomorrow.

Paul

Hi Paul,

Thank you very much for your help so far,

As for the modes, there are technically 3. First button to fade through colours, Second mode to flash through colours and mode 3 uses the potentiometer to select colour manually. The idea being, you can be in any of the modes on one of the buttons and then by turning the potentiometer, you can select a colour and vice versa so if your in a potentiometer mode, then you can press a button and it activates a different mode assigned to that button. Its hard to explain but hopefully you understand what I mean.

As for the FET's. They are all FET N's, i just forgot to change the other two. And yes that's correct, the RGB represents the strip.

I look forward to hearing for you and thanks again for your help.

Jamie

As for the modes, there are technically 3. First button to fade through colours, Second mode to flash through colours and mode 3 uses the potentiometer to select colour manually.

Ok, I get that.

you can be in any of the modes on one of the buttons and then by turning the potentiometer, you can select a colour and vice versa so if your in a potentiometer mode, then you can press a button and it activates a different mode assigned to that button.

No, sorry, you lost me again.

what i mean is say the potentiometer is one mode then when you press either of the buttons then it switched to the mode that that button is assigned to... Hard to explain lol.

Thanks
Jamie

Jamie77:
Hard to explain

If you can't explain it then it is a sure fire thing that you can't code it.

Try again.

3 modes, each selected by a separate button?

What does the pot do in each mode, if anything?

Does the pot ever cause a mode change?

What exactly does the flashing mode look like?

Take us through an example sequence of actions (buttons pressed, pot changes). One step at a time. Use short sentances!

Sorry,

BUTTON 1 = MODE 1
slow fade through colours

BUTTON 2 = MODE 2
flash through colours without fade inbetween

POTENTIOMETER = MODE 3
change colours manually

POT does nothing in mode 1 or 2 just 3

So you need to define how you get into mode 3.

If possible I would prefer to have to pot act as a button so when you start turning it, it activates mode 3 but if not then ill just add another button :slight_smile:

If possible I would prefer to have to pot act as a button so when you start turning it, it activates mode 3

Yes that is possible.
You need what is known as a state machine. It basically uses a variable and switch statement to call the appropriate routine for the mode it is in. The MODEs 1 & 2 must monitor the pot to see if it has moved sufficiently to change to mode 3. In fact all three modes must monitor for a mode change and set the state variable accordingly.
YOU SHOULD NEVER USE THE DELAY FUNCTION
You must write all three modes in functions so that they do not hold or loop in that function, they must all work like -> enter function, check for mode change, check if anything needs doing in this mode, if so do it, then exit.

I have tried so many different codes and just cannot get it to work and if its even possible.

Yes it is possible but you are wasting your time looking for ready written code to do this for you, you have to write your own code.
Some links to help you:-

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Robin2's several things at once

Ok!

I see all 3 modes as variations on the same theme. In all 3 modes, the sketch is moving around the colour wheel.

In mode 1, the movement is in small steps with a short period between each step. For example steps of 6 degrees every 0.1 seconds. A circuit of the wheel is completed every 6 seconds.

In mode 2, the movement is in large steps with a long period between each step. For example steps of 60 degrees every 1 second. Again, 6 seconds to complete a circuit of the wheel.

In mode 3, the position on the colour wheel is simply tied to the pot value.

Yes that's absolutely correct! :smiley:

Great. This is how I would structure the code in loop():

First section: read the buttons and the pot. Set the mode. Set the step and period (modes 1& 2). Variables needed for: mode, step, period, pot value, previous pot val. Hint: don't change to mode 3 unless the difference between the last and current pot readings are different by, say, at least 20 (otherwise noise in the pot readings will cause unwanted switching to mode 3). Useful functions: digitalRead(), analogRead(), abs().

Second section: update the position on the colour wheel. In mode 3, just translate the pot value (0..1023) into the colour angle (0..359). In other modes, check if its time to update the colour angle. If so, update it, check and correct if it has gone over 360, and note the time it was updated. Useful functions: map(), millis(). Variables needed: last update time (must be unsigned long).

Third section: turn the colour angle into red, green, blue components (0..255). In the angle range between 0 & 119, red should fade from 255 to 0 and green from 0 to 255. Similarly from 120 to 239 degrees for green & blue. Between 240 and 359 degrees for blue and red. Finally set the r, g & b pwm outputs to the values you calculated. Useful functions: map(), analogWrite(). Variables needed: red, green and blue values.

Try coding that first section and post it here and we can give more hints if needed. Read about the functions I mentioned in the Reference section from the menu at the top of this page.

Paul

Thanks Paul,

I will attempt the first part. Thanks again.