Created
April 5, 2016 01:58
-
-
Save peteristhegreat/39c5484a8d126b156e9cabdefde479a1 to your computer and use it in GitHub Desktop.
RBG LED with button Arduino example, cycles colors 10x per second while the button is pressed
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
// https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/breadboard-layout | |
// http://www.arduino.cc/en/Tutorial/Button | |
/* | |
Adafruit Arduino - Lesson 3. RGB LED | |
*/ | |
int redPin = 11; | |
int greenPin = 10; | |
int bluePin = 9; | |
int buttonPin = 2; | |
//uncomment this line if using a Common Anode LED | |
//#define COMMON_ANODE | |
void setup() | |
{ | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
pinMode(buttonPin, INPUT); | |
//digitalWrite(buttonPin, HIGH); // turn on pullup resistors | |
} | |
int state = 0; | |
void loop() | |
{ | |
if(digitalRead(buttonPin)==1) | |
state++; | |
switch(state%6) | |
{ | |
case 0: | |
setColor(255, 0, 0); // red | |
break; | |
case 1: | |
setColor(0, 255, 0); // green | |
break; | |
case 2: | |
setColor(0, 0, 255); // blue | |
break; | |
case 3: | |
setColor(255, 255, 0); // yellow | |
break; | |
case 4: | |
setColor(80, 0, 80); // purple | |
break; | |
case 5: | |
setColor(0, 255, 255); // aqua | |
break; | |
default: | |
break; | |
} | |
delay(100); | |
} | |
void setColor(int red, int green, int blue) | |
{ | |
#ifdef COMMON_ANODE | |
red = 255 - red; | |
green = 255 - green; | |
blue = 255 - blue; | |
#endif | |
int divisor = 12; | |
analogWrite(redPin, red/divisor); | |
analogWrite(greenPin, green/divisor); | |
analogWrite(bluePin, blue/divisor); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment