Skip to content

Instantly share code, notes, and snippets.

@hughevans
Created September 22, 2009 13:26
Show Gist options
  • Save hughevans/191062 to your computer and use it in GitHub Desktop.
Save hughevans/191062 to your computer and use it in GitHub Desktop.
My first Arduino sketch
// Knobber
// Hook up a potentiometer to tri-color LED
int redPin = 9; // red LED, digital pin 9
int greenPin = 10; // green LED, digital pin 10
int bluePin = 11; // blue LED, digital pin 11
int potPin = 0; // potentiometer, analog pin 0
int redVal = 1;
int greenVal = 1;
int blueVal = 1;
int potVal;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
potVal = analogRead(potPin);
if (potVal < 255)
{
redVal = (255 - potVal);
greenVal = (potVal == 0 ? 1 : potVal);
blueVal = 1;
}
else if (potVal < 510)
{
redVal = 1;
greenVal = (510 - potVal);
blueVal = (1 + (potVal - 255));
}
else if (potVal < 765)
{
redVal = (1 + (potVal - 510));
greenVal = 1;
blueVal = (765 - potVal);
}
else
{
redVal = 0;
greenVal = 0;
blueVal = 0;
}
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment