Created
April 1, 2017 14:39
-
-
Save jonathanprozzi/d318764bd587c805398135c79b43c770 to your computer and use it in GitHub Desktop.
RGB 'Mixer' with potentiometers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* RGB Mixer code by Jonathan Prozzi. | |
* This includes Simon Monk's RGB LED code from the Adafruit lesson. | |
* | |
* There are several variables that need to be initialized. | |
* Remember, if you change the pins on the Arduino you | |
* also change the variables below. | |
*/ | |
// These are the pins connected to each of the RGB leads. | |
int redPin = 11; | |
int greenPin = 10; | |
int bluePin = 9; | |
// These are the input pins for the potentiometers. | |
int redPot = A0; | |
int greenPot = A1; | |
int bluePot = A2; | |
// These are the initial reading values for the potentiometers. | |
int redVal = 0; | |
int greenVal = 0; | |
int blueVal = 0; | |
// These are the initial color values - used later in the sketch. | |
int redColor = 0; | |
int greenColor = 0; | |
int blueColor = 0; | |
#define COMMON_ANODE; // This is necessary for the common anode RGB LED. | |
void setup() { | |
// Set the RGB LED pins for output. | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
Serial.begin(9600); // Setting the Serial monitor to check the values from the pots. | |
} | |
void loop() { | |
// Read the values from each potentiometer | |
redVal = analogRead(redPot); | |
greenVal = analogRead(greenPot); | |
blueVal = analogRead(bluePot); | |
// Print the pot values to the Serial monitor. This is to check that the readings are coming in. | |
Serial.print("Red Reading: "); | |
Serial.println(redVal); | |
Serial.print("Green Reading: "); | |
Serial.println(greenVal); | |
Serial.print("Blue Reading: "); | |
Serial.println(blueVal); | |
delay(1000); | |
// Map the potentiometer readings (0 - 1023) to RGB values (0 - 255) | |
redColor = map(redVal, 0, 1023, 0, 255); | |
greenColor = map(greenVal, 0, 1023, 0, 255); | |
blueColor = map(blueVal, 0, 1023, 0, 255); | |
// Set the RGB colors to the values from the potentiometers | |
setColor(redColor, greenColor, blueColor); | |
} | |
void setColor(int red, int green, int blue) { | |
#ifdef COMMON_ANODE | |
red = 255 - red; | |
green = 255 - green; | |
blue = 255 - blue; | |
#endif | |
analogWrite(redPin, red); | |
analogWrite(greenPin, green); | |
analogWrite(bluePin, blue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment