Create a gist now

Instantly share code, notes, and snippets.

Code and Diagrams from Intro to Arduino talk given at Montana Programmers on Thursday, Marth 8, 2012
int blue_brightness = 0; // how bright the blue LED is
int red_brightness = 255; // how bright the red LED is
int blue_fadeAmount = 5; // how many points to fade the blue LED by
int red_fadeAmount = -5; // how many points to fade the red LED by
void setup() {
// declare pins 9 and 10 to be an outputs:
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
// set the brightness levels:
analogWrite(9, blue_brightness);
analogWrite(10, red_brightness);
// change the brightness for next time through the loop:
blue_brightness = blue_brightness + blue_fadeAmount;
red_brightness = red_brightness + red_fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (blue_brightness == 0 || blue_brightness == 255) {
blue_fadeAmount = -blue_fadeAmount ;
}
if (red_brightness == 0 || red_brightness == 255) {
red_fadeAmount = -red_fadeAmount ;
}
// set the delay based off of the light sensor reading so that
// the speed of fading is set by the ambient light amount
delay(5);
}
int blue_brightness = 0; // how bright the blue LED is
int red_brightness = 255; // how bright the red LED is
int blue_fadeAmount = 5; // how many points to fade the blue LED by
int red_fadeAmount = -5; // how many points to fade the red LED by
int ambient = 0;
void setup() {
// declare pins 9 and 10 to be an outputs:
pinMode(9, OUTPUT); // blue LED
pinMode(10, OUTPUT); // red LED
pinMode(A0, INPUT); // Light Sensor
}
void loop() {
// set the brightness levels:
analogWrite(9, blue_brightness);
analogWrite(10, red_brightness);
ambient = analogRead(0); //grab the value from the light sensor
// change the brightness for next time through the loop:
blue_brightness = blue_brightness + blue_fadeAmount;
red_brightness = red_brightness + red_fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (blue_brightness == 0 || blue_brightness == 255) {
blue_fadeAmount = -blue_fadeAmount ;
}
if (red_brightness == 0 || red_brightness == 255) {
red_fadeAmount = -red_fadeAmount ;
}
// set the delay based off of the light sensor reading so that
// the speed of fading is set by the ambient light amount
delay(30 % ambient);
}
Code and Diagrams from Intro to Arduino talk given at Montana Programmers on Thursday, Marth 8, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment