Skip to content

Instantly share code, notes, and snippets.

@tekk
Created July 15, 2014 05:01
Show Gist options
  • Select an option

  • Save tekk/51f437da33a5e429848d to your computer and use it in GitHub Desktop.

Select an option

Save tekk/51f437da33a5e429848d to your computer and use it in GitHub Desktop.
PWM_ColorMixing_RGB_LED_CommonAnode
#include <FlexiTimer2.h>
#define GREEN 9
#define RED 6
#define BLUE 5
int redPin = RED;
int greenPin = GREEN;
int bluePin = BLUE;
void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, 1);
digitalWrite(GREEN, 1);
digitalWrite(BLUE, 1);
pinMode(10, OUTPUT);
FlexiTimer2::set(500, flash); // 500ms period
FlexiTimer2::start();
}
int redIntensity = 255;
void loop()
{
// set all 3 pins to the desired intensity
analogWrite(redPin, redIntensity);
analogWrite(greenPin, redIntensity - 255);
analogWrite(bluePin, 0);
// remain at this color, but not for very long
delay(10);
// increase the red
redIntensity--;
// since 255 is the maximum, set it back to 0
// when it increments beyond 255
if (redIntensity <= 0)
{
redIntensity = 255;
}
}
void flash()
{
static boolean output = HIGH;
digitalWrite(10, output);
output = !output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment