Created
March 8, 2012 20:09
-
-
Save jaderobbins/2003122 to your computer and use it in GitHub Desktop.
Code and Diagrams from Intro to Arduino talk given at Montana Programmers on Thursday, Marth 8, 2012
This file contains hidden or 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
/* | |
Blink | |
Turns on an LED on for one second, then off for one second, repeatedly. | |
This example code is in the public domain. | |
*/ | |
void setup() { | |
// initialize the digital pin as an output. | |
// Pin 13 has an LED connected on most Arduino boards: | |
pinMode(9, OUTPUT); | |
} | |
void loop() { | |
digitalWrite(9, HIGH); // set the LED on | |
delay(1000); // wait for a second | |
digitalWrite(9, LOW); // set the LED off | |
delay(1000); // wait for a second | |
} |
This file contains hidden or 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
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); | |
} |
This file contains hidden or 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
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); | |
} |
This file contains hidden or 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
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