84 lines
2 KiB
Arduino
84 lines
2 KiB
Arduino
|
/*
|
||
|
Fade
|
||
|
|
||
|
This example shows how to fade an LED on pin 9 using the analogWrite()
|
||
|
function.
|
||
|
|
||
|
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.
|
||
|
|
||
|
This example code is in the public domain.
|
||
|
|
||
|
http://www.arduino.cc/en/Tutorial/Fade
|
||
|
*/
|
||
|
|
||
|
#define RED 11
|
||
|
#define GREEN 10
|
||
|
#define BLUE 9
|
||
|
#define WHITE 6
|
||
|
|
||
|
int brightness1 = 25; // how bright the LED is
|
||
|
int brightness2 = 25; // how bright the LED is
|
||
|
int brightness3 = 25; // how bright the LED is
|
||
|
|
||
|
int fadeAmount1 = 1; // how many points to fade the LED by
|
||
|
int fadeAmount2 = 1; // how many points to fade the LED by
|
||
|
int fadeAmount3 = 1; // how many points to fade the LED by
|
||
|
|
||
|
unsigned long previousMillis = 0; // will store last time LED was updated
|
||
|
const long interval = 30; // interval at which to blink (milliseconds)
|
||
|
|
||
|
|
||
|
// the setup routine runs once when you press reset:
|
||
|
void setup() {
|
||
|
// declare pin 9 to be an output:
|
||
|
pinMode(RED, OUTPUT);
|
||
|
pinMode(GREEN, OUTPUT);
|
||
|
pinMode(BLUE, OUTPUT);
|
||
|
pinMode(WHITE, OUTPUT);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
// the loop routine runs over and over again forever:
|
||
|
void loop() {
|
||
|
// set the brightness of pin 9:
|
||
|
|
||
|
unsigned long currentMillis = millis();
|
||
|
|
||
|
if (currentMillis - previousMillis >= interval)
|
||
|
{
|
||
|
previousMillis = currentMillis;
|
||
|
|
||
|
brightness1 = brightness1 + fadeAmount1;
|
||
|
brightness2 = brightness2 + fadeAmount2;
|
||
|
brightness3 = brightness3 + fadeAmount3;
|
||
|
|
||
|
Serial.println(brightness1);
|
||
|
|
||
|
if (brightness1 <= 10 || brightness1 >= 50)
|
||
|
{
|
||
|
fadeAmount1 = -fadeAmount1;
|
||
|
}
|
||
|
|
||
|
if (brightness2 <= 10 || brightness2 >= 55)
|
||
|
{
|
||
|
fadeAmount2 = -fadeAmount2;
|
||
|
}
|
||
|
|
||
|
if (brightness3 <= 10 || brightness3 >= 45)
|
||
|
{
|
||
|
fadeAmount3 = -fadeAmount3;
|
||
|
}
|
||
|
|
||
|
analogWrite(RED, brightness1);
|
||
|
analogWrite(GREEN, brightness2);
|
||
|
analogWrite(WHITE, brightness3);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|