Notifications
Clear all

RGB Christams tree wiring  

Page 3 / 3
  RSS

Squish
(@squish)
Eminent Member
Joined: 4 months ago
Posts: 38
2020-10-27 5:37 am  
Posted by: @zoolandermicro

You might try my code for common cathode tri-colored LEDs. The colors blend nicely.

#include <avr/io.h>
#include <util/delay.h>
/**
   Scetch ColorBlend_CommonCathode is a color fade/blend routine for a tri-color
   common cathode led connected to Arduino Uno digital pins 9, 10, and 11 as PWM
   outputs. Colors transition from blue through teal, green, yellow, orange, red,
   violet, purple, and back to blue. With common cathode tri-colored leds,
   analogWrite() value of 0 results in a full 'off' state, and analogWrite()   
   value of 255 results in a full 'on' state. This scetch works with common
   anode tri-colored leds, only the color blend runs in the opposite order.
   Written in Arduino C language.
   @author Mike Tonge
   @date 12/03/2019
*/
// Constants
const uint8_t DELAY_TIME = 100; // Delay time (miliseconds)
const uint8_t redLED = 9; // Red on digital pin 9
const uint8_t grnLED = 10; // Green on digital pin 10
const uint8_t bluLED = 11; // Blue on digital pin 11
const uint8_t OFF = 0; // Value used to turn led off
// Variables
uint8_t redVal = 0; // Red value
uint8_t grnVal = 0; // Green value
uint8_t bluVal = 0; // Blue value

void setup() {
  pinMode(redLED, OUTPUT); // Set pin 9 as output
  pinMode(grnLED, OUTPUT); // Set pin 10 as output
  pinMode(bluLED, OUTPUT); // Set pin 11 as output
}

void loop() {
  for (int count = 0; count <= 765; count++) {
    switch (count) {
      // Red is off, Green gets brighter, Blue gets dimmer
      case 0 ... 255:
        redVal = OFF;
        grnVal = count;
        bluVal = 255 - count;
        break;
      // Red gets brighter, Green gets dimmer, Blue is off
      case 256 ... 510:
        redVal = count - 255;
        grnVal = 510 - count;
        bluVal = OFF;
        break;
      // Red gets dimmer, Green is off, Blue gets brighter
      case 511 ... 765:
        redVal = 765 - count;
        grnVal = OFF;
        bluVal = count - 510;
        break;
      default:
        break;
    } // End switch
    analogWrite(redLED, redVal);
    analogWrite(grnLED, grnVal);
    analogWrite(bluLED, bluVal);
    delay(DELAY_TIME);
  }
}
 

It's really beautiful, I've got some 0.75mm fibre optics shrink wrapped onto a clear RGB, and it's lovely. It's the sort of RGB where you can see the separate LEDS really easily, as such some of the fibre optics are slightly different.

#Question:-How would I reverse the colours, so it goes from Red Yellow Orange, Green Teal Blue, Blue Purple Violet and back To Red?

I never knew coloured lights could be so much fun.

Again many thanks. 

No such thing as too much energy, it's just un-utilised potential.


ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Trusted Member
Joined: 3 months ago
Posts: 81
2020-10-27 8:40 pm  

There is a great tutorial article on the Adafruit web site regarding NeoPixels. The NeoPixel Uber Guide ( https://learn.adafruit.com/adafruit-neopixel-uberguide/). The Best Practices page of the guide states:

  • Before connecting NeoPixels to any large power source (DC “wall wart” or even a large battery), add a capacitor (1000 µF, 6.3V or higher) across the + and – terminals as shown above. The capacitor buffers sudden changes in the current drawn by the strip.

ZoolanderMicro, where small ideas are a big deal


Squish liked
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Trusted Member
Joined: 3 months ago
Posts: 81
2020-10-27 8:58 pm  

Starting the routine with red high in the first case (redVal = 255 - count;). As red decreases/fades, green should increase (grnVal = count;). This transitions from  red to orange to yellow to green. In the second case green will be high and begin to get dimmer (grnVal = 510 - count;). Blue begins to get brighter and red is off. The PWM function accepts values from 0 - 255. The arithmetic in the switch case changes the values of the variables (redVal, grnVal, bluVal). The NeoPixel function also accepts PWM values: pixels.setPixelColor(PIXEL_INDEX, pixels.Color(redVal, greenVal, blueVal));. In this case, the instance of the NeoPixel object is named 'pixels'.  Use the show(); function to update the color. 

ZoolanderMicro, where small ideas are a big deal


Squish liked
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Trusted Member
Joined: 3 months ago
Posts: 81
2020-10-27 9:22 pm  

Perhaps I should clarify what I mean by the 'PWM' function. The Arduino C language has the analogWrite() function. The analog output is actually a pulse width modulated (PWM) signal in form of a square wave between 0 to 5 volts. Values passed to the analogWrite() function set the duration of the pulse. A value of 0 results in no output pulse. A value of 255 results in a high output pulse that lasts nearly as long as the frequency. I think the analog outputs of the Arduino use timer 0 that is set to a default frequency of ~1khz. I only mentioned that because the timer can be changed to a lower frequency to eliminate motor whine. 

ZoolanderMicro, where small ideas are a big deal


Squish liked
ReplyQuote
Squish
(@squish)
Eminent Member
Joined: 4 months ago
Posts: 38
2020-11-03 3:02 pm  

Thank you @zoolandermicro ,much appreciated.

 

Thanks, especially for the added PMW pin function, I haven't got my head around it, but I always assume that I need PMWs for LEDs?

I have recently been running through the adafruit neopixel tutorial, but not much is sticking atm.

Although I have been playing with a few things, including using your code on a seeduino XIOA, and it doesnt load, I cant remem what the error message is, next time I try it, ill copy it here.

 

Also played with an ideaspark esp8266, starter kit, with RGBring, remote conrol, etc, and managed to switch some leds on from my phone, I'll be wanting to have that option for quite a few lights in te future. So i'll be trying to combine the codes.

 

Again thanks very much for your help.

 

No such thing as too much energy, it's just un-utilised potential.


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 529
2020-11-03 5:27 pm  

@squish

... I haven't got my head around it, but I always assume that I need PMWs for LEDs?

That is just how the "analogue" works with the Arduino analogue output.

https://www.arduino.cc/en/Tutorial/Foundations/PWM

So, no you don't always need PWM for controlling the brightness of a LED you can in fact use a true analogue signal, such as provided by a variable resister, to change the voltage across the LED from 0 volts to whatever voltage the particular LED can cope with.

However there is an advantage in controlling the speed of an electric motor when you apply full voltage (full torque at slower speeds) at any given on time rather than varying the voltage.

With my early computers I would convert a digital value into an analogue value using a circuit called a digital to analogue converter. The output would be amplified and applied to a speaker to make music or other audible sound waves.

https://sciencing.com/analog-digital-converter-work-4968312.html

 

This post was modified 1 month ago 2 times by robotBuilder

Squish liked
ReplyQuote
Squish
(@squish)
Eminent Member
Joined: 4 months ago
Posts: 38
2020-11-06 4:51 am  

@rootbuilder,

 

Thanks for the links, I'm still in kindergarten, with all of this, but I'm enjoying the challenge.

 

No such thing as too much energy, it's just un-utilised potential.


ReplyQuote
Page 3 / 3