Skip to content

Instantly share code, notes, and snippets.

@pdesantis
Created June 18, 2013 00:02
Show Gist options
  • Save pdesantis/5801596 to your computer and use it in GitHub Desktop.
Save pdesantis/5801596 to your computer and use it in GitHub Desktop.
Create a "breathing" pulsing effect on an array of LEDs
// Arduino Workshop Project 3
// PWM
int d = 5; // d is used for time delay
const int number_of_pins = 5; // the number of output pins
int pins[number_of_pins] = {3, 5, 6, 9, 10}; // an array storing each output pin
// Sets the proper pins to output mode
void setup()
{
for (int i = 0; i < number_of_pins; i++)
{
pinMode(pins[i], OUTPUT);
}
}
void loop()
{
// Loop through each pin in pins array, pulsing them one at a time
for (int i = 0; i < number_of_pins; i++)
{
pulsePin(pins[i]);
}
// Pause for 200 milliseconds
delay(200);
}
// ******* Helper methods *******
// Function: lightUpPin
// Write analog values 0 to 255 to the specified pin
// Parameter: pin - the pin to light up
void lightUpPin(int pin)
{
for ( int a = 0 ; a < 256 ; a++ )
{
analogWrite(pin, a);
delay(d);
}
}
// Function: lightUpPin
// Write analog values 255 to 0 to the specified pin
// Parameter: pin - the pin to dim
void dimPin(int pin)
{
for ( int a = 255 ; a >= 0 ; a-- )
{
analogWrite(pin, a);
delay(d);
}
}
// Function: pulsePin
// Pulses a pin from analog 0 to 255 and back to 0
// Parameter: pin - the pin to pulse
void pulsePin(int pin)
{
lightUpPin(pin);
dimPin(pin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment