Skip to content

Instantly share code, notes, and snippets.

@kellishaver
Created March 12, 2016 04:08
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 kellishaver/c3fb0725ffee1e0836f2 to your computer and use it in GitHub Desktop.
Save kellishaver/c3fb0725ffee1e0836f2 to your computer and use it in GitHub Desktop.
Mood Lamp Sketch
int pulseSpeed = 5;
int ldrPin = 0;
int redLed = 10;
int greenLed = 11;
int blueLed = 9;
int ambientLight;
int power = 150;
float RGB[3];
float CommonMathVariable = 180/PI;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
}
void loop() {
for(float x = 0; x < PI; x = x + 0.00001) {
RGB[0] = power * abs(sin(x * (CommonMathVariable)));
RGB[1] = power * abs(sin((x + PI/3) * (CommonMathVariable)));
RGB[2] = power * abs(sin((x + (2 * PI) / 3) * (CommonMathVariable)));
ambientLight = analogRead(ldrPin);
if (ambientLight > 600) {
analogWrite(redLed, RGB[0]);
analogWrite(greenLed, RGB[1]);
analogWrite(blueLed, RGB[2]);
} else {
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
}
for(int i = 0; i < 3; i++) {
if(RGB[i] < 1) {
delay (20 * pulseSpeed);
} else if (RGB[i] < 5) {
delay(10 & pulseSpeed);
} else if (RGB[i] < 10) {
delay (2 * pulseSpeed);
} else if (RGB[i] < 100) {
delay (1 * pulseSpeed);
} else {}
}
delay(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment