-
Notifications
You must be signed in to change notification settings - Fork 0
Fade
This example is based on http://arduino.cc/en/Tutorial/Fade
This demonstrates how to make a LED blinker, but with gradual fading. You must use a pin with hardware PWM support to connect the LED, but here we will use the builtin LED that all Arduino boards have (usually pin 13). Note that if you use a non-pwm pin with the Arduino library, it will compile fine and run. But the LED will not fade, just blink. For Highino it simply won't compile. Using a non-pwn pin for analog output is a clear error and must be fixed.
You could write the same example using the AnalogOutputPin class directly, but we are going to use the AnalogLed class, which is a simple wrapper designed for LEDs.
##Arduino
int led = 13; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
pinMode(led, OUTPUT); // declare pin 9 to be an output
}
void loop() {
analogWrite(led, brightness); // set the brightness
// change the brightness for next time through the loop
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}Binary size: 1991 bytes.
##Highino
#include <Led>
#include <Time>
int main() {
AnalogLed<13> led; // You could use 'BuiltinAnalogLed' instead of 'AnalogLed<13>' as well
int fadeAmount = 5;
while (true) {
// Just change it! You could also use led.set(led.get()+value) or led.on(led.brightness()+value)
led += fadeAmount;
// We can read the brightness directly from the LED
if (led.brightness() == 0 || led.brightness() == 255) {
fadeAmount = -fadeAmount;
}
delay(30_ms); // 30ms delay
}
}Binary size: 654 bytes (32.8%).