Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created May 3, 2020 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/90849853bccd09b9b0ad975dca5f4479 to your computer and use it in GitHub Desktop.
Save JeffersGlass/90849853bccd09b9b0ad975dca5f4479 to your computer and use it in GitHub Desktop.
int redPin = 3;
int greenPin = 5;
int bluePin = 6;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setLedColor(255,0,0);
}
void loop() {
for (int i = 0; i < 360; i++){
setColorByHue(i);
delay(10);
}
}
void setColorByHue(int angle){
if (0 <= angle && angle < 120){
int red = map(angle, 0, 120, 255, 0);
int green = map(angle, 0, 120, 0, 255);
int blue = 0;
setLedColor(red, green, blue);
}
else if (120 <= angle && angle < 240){
int red = 0;
int green = map(angle, 120, 240, 255, 0);
int blue = map(angle, 120, 240, 0, 255);
setLedColor(red, green, blue);
}
else{ //angle between 240 and 360
int blue = map(angle, 240, 360, 255, 0);
int red = map(angle, 240, 360, 0, 255);
int green = 0;
setLedColor(red, green, blue);
}
}
void setLedColor(int r, int g, int b){
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
void setLedColor(int colorVector[]){
setLedColor(colorVector[0], colorVector[1], colorVector[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment