Created
July 10, 2010 23:58
-
-
Save tatsuro-ueda/471144 to your computer and use it in GitHub Desktop.
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
// for setup() | |
int ledPinRed = 11; | |
int ledPinGreen = 10; | |
int ledPinBlue = 9; | |
// for getRandomColor() | |
int Color[3] = {0, 0, 0}; | |
// for loop() | |
int valueRed = 0; | |
int valueGreen = 0; | |
int valueBlue = 0; | |
// for fadeBlink() | |
int i = 0; | |
int tempRed = 0; | |
int tempGreen = 0; | |
int tempBlue = 0; | |
void setup() { | |
// initialize the digital pin as an output: | |
pinMode(ledPinRed, OUTPUT); | |
pinMode(ledPinGreen, OUTPUT); | |
pinMode(ledPinBlue, OUTPUT); | |
digitalWrite(ledPinRed, HIGH); | |
digitalWrite(ledPinGreen, HIGH); | |
digitalWrite(ledPinBlue, HIGH); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
// 光らせる色をランダムに決める | |
setRandomColor(); | |
// 実際に光らせる | |
fadeBlink(Color[0], Color[1], Color[2]); | |
} | |
void setRandomColor() | |
{ | |
Color[0] = random(4); | |
Color[1] = random(4 - Color[0]); | |
Color[2] = 3 - (Color[0] + Color[1]); | |
Color[0] = Color[0] * 64; | |
Color[1] = Color[1] * 64; | |
Color[2] = Color[2] * 64; | |
Serial.println(Color[0]); | |
Serial.println(Color[1]); | |
Serial.println(Color[2]); | |
Serial.println(""); | |
} | |
void fadeBlink(int valueRed, int valueGreen, int valueBlue) | |
{ | |
for (i = 1; i < 32; i++) | |
{ | |
tempRed = valueRed * i / 256; | |
tempGreen = valueGreen * i / 256; | |
tempBlue = valueBlue * i / 256; | |
analogWrite3leds(tempRed, tempGreen, tempBlue); | |
delay(100); | |
} | |
for (i = 31; i > 0; i--) | |
{ | |
tempRed = valueRed * i / 256; | |
tempGreen = valueGreen * i / 256; | |
tempBlue = valueBlue * i / 256; | |
analogWrite3leds(tempRed, tempGreen, tempBlue); | |
delay(100); | |
} | |
delay(2000); | |
} | |
void analogWrite3leds(int valueRed, int valueGreen, int valueBlue) { | |
if (valueRed) { analogWrite(ledPinRed, 256 - valueRed); } | |
if (valueGreen) { analogWrite(ledPinGreen, 256 - valueGreen); } | |
if (valueBlue) { analogWrite(ledPinBlue, 256 - valueBlue); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment