Adding button and colour speed control to existing project

Hi

I have the following project working which is using 3 potentiometers to control 3 TTL outputs on an RGB laser module. With this i can change the power to each laser to make any colour. Im wanting to have it cycle/fade through the spectrum of colours and have a 4th pot to change the speed and a switch to switch between the current mode and this new mode. Ive found info on changing colour but it jumps from to next but i want it smooth.

Current sketch is

const int Radc = 0 ;  //naming pin 0 of analog input side as ?Red adc?
const int Gadc = 1 ;  //naming pin 1 of analog input side as ?Greenadc?
const int Badc = 2 ;  //naming pin 2 of analog input side as ?Blue adc?

const int Rpwm = 3 ;    //naming pin 3 as ?Red pwm? variable
const int Gpwm = 5 ;    //naming pin 5 as ?Green pwm? variable
const int Bpwm = 6 ;    //naming pin 6 as ?Blue pwm? variable

void adjustChannel(int inputPin, int outputPin)
{
  int inputVoltage = analogRead(inputPin);
  int outputVoltage = map(inputVoltage, 0, 1023, 0, 255);
  analogWrite(outputPin, outputVoltage) ;
}

void setup()
{
  pinMode(Rpwm, OUTPUT) ;   //setting pin 3 as output
  pinMode(Gpwm, OUTPUT) ;   //setting pin 5 as output
  pinMode(Bpwm, OUTPUT) ;   //setting pin 6 as output
}

void loop()
{
  adjustChannel (Radc, Rpwm);
  adjustChannel (Gadc, Gpwm);
  adjustChannel (Badc, Bpwm);
}

Can anyone give me some pointers or help on this.

Thanks

Lee

Ive found info on changing colour but it jumps from to next but i want it smooth.

Your post made sense up to this part.

Fair point :slight_smile:
The code i had found would change from one colour to the next without fading so was like a hard change from say red to orange to yellow. not smooth.
Since posting I have found a few that say they fade through the colours smoothly but i need to test them. Im wanting it to be in the spectrum order from dark red all the way to violet. The plan is to use a moving mirror to move the beam and the colours change quick enough that you can see the range of colour. This is why i also need the control over the change speed via a pot. Hope that makes a bit more sense.
Here are some images of what im trying to do attached. The "fan" like one is what im after. Once ive got it i then want to work on getting single colour lines like in the second image.

Thanks

Lee


I forsee a problem. The frequency of Arduino pwm won't be fast enough to create effects like that first one. It's only around 0.5~1KHz, you would need 10 times that at least. But it can be changed.

Here's what I suggest:

Don't use pins 5, 6. If you try to change the pwm frequency on those pins, delay() and millis() will stop working correctly, which will be a pain.

You can change the frequency of pwm on pins 9, 10, 11, 3 to 31KHz using the code given on this page.

There's a simple algorithm I like to use to create spectrum effects. It's so simple, I don't have to look it up, just code it from memory.

You have a variable, either controlled by a for-loop containing a delay(), or updated every few milliseconds, which starts at 0 and increments to 767, then back to 0. This controls the "hue".

When hue is less than 256: green = hue, red = 255 - hue and blue = 0..
Otherwise, when hue is less than 512: blue = hue - 256, green = 511 - hue and red = 0.
Otherwise: red = hue - 512, blue = 767 - hue and green = 0.