Skip to content

Instantly share code, notes, and snippets.

@jakubczaplicki
Created April 20, 2018 08:34
Show Gist options
  • Save jakubczaplicki/31f4a34410a1e85024e50fe3187922e7 to your computer and use it in GitHub Desktop.
Save jakubczaplicki/31f4a34410a1e85024e50fe3187922e7 to your computer and use it in GitHub Desktop.
Christmas lights fading
/*
Christmas lights fading example
The analogWrite() function uses PWM, so if
you want to change the pin you're using, be
sure to use another PWM capable pin. On most
Arduino, the PWM pins are identified with
a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
*/
#define CUSTOM_SETTINGS
#define INCLUDE_SLIDER_SHIELD
/* Include 1Sheeld library. */
#include <OneSheeld.h>
int ledsPin = 9; // the PWM pin for the led lights
int greenLedPin = 10; // green led example
int photodiodePin = 0; // define a pin for Photo resistor
void setup() {
// As stated in Arduino UNO board, it has only 1 Serial UART
// communication peripheral which are the pins 0 (Rx) and 1 (Tx)
// which are also occupied by 1Sheeld, so they aren’t used when
// 1Sheeld is adjusted on Uno. In case you need to use the UART
// peripheral with another device connected to 1Sheeld, you can
// use software serial mode.
//Serial.begin(9600);
// OneSheeld setup
OneSheeld.begin();
pinMode(ledsPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(photodiodePin, INPUT);
}
int light = 210;
int set_point = 0;
int brightness = 10; // how bright the LEDs are
int fade_amount = 5; // how many points to fade the LED by
int slider_value = 210;
void loop() {
light = analogRead(photodiodePin);
// get the slider value and output it as PWM
// slider_value = Slider.setValue(set_point);
analogWrite(greenLedPin,255 - set_point);
if (light < 200)
{
set_point = brightness;
}
else if (light > 220)
{
set_point = 0;
}
analogWrite(ledsPin, set_point);
brightness = brightness + fade_amount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 8 || brightness >= 255) {
fade_amount = -fade_amount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment