]> git.piffa.net Git - sketchbook_andrea/blob - basic/pwm/pwm_6_states_lum_correction_32/pwm_6_states_lum_correction_32.ino
Correzione di luminosita
[sketchbook_andrea] / basic / pwm / pwm_6_states_lum_correction_32 / pwm_6_states_lum_correction_32.ino
1 /*
2   Change brightness of LED linearly to Human eye
3   32 step brightness using 8 bit PWM of Arduino
4   brightness step 24 should be twice bright than step 12 to your eye.
5  
6   https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/
7 */
8
9
10 #include <avr/pgmspace.h>
11 #define CIELPWM(a) (pgm_read_byte_near(CIEL8 + a)) // CIE Lightness lookup table function
12
13 /*
14   5 bit CIE Lightness to 8 bit PWM conversion
15   https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/
16   L* = 116(Y/Yn)^1/3 - 16 , Y/Yn > 0.008856
17   L* = 903.3(Y/Yn), Y/Yn <= 0.008856
18 */
19
20 const uint8_t CIEL8[] PROGMEM = {
21   0,    1,    2,    3,    4,    5,    7,    9,    12,
22   15,    18,    22,    27,    32,    38,    44,    51,    58,
23   67,    76,    86,    96,    108,    120,    134,    148,    163,
24   180,    197,    216,    235,    255
25 };
26
27 int brightness = 0;  // initial brightness of LED
28 int fadeAmount = 1;
29 int led1Pin = 9;     // pause between each interval (32)
30
31 unsigned long startTime = 0;    // timing variables
32 const long interval = 12;
33
34
35 void setup()  {  
36   pinMode(led1Pin, OUTPUT);     // declare pin 9 to be an output:
37 }
38
39 void loop()  {
40   unsigned long currentTime = millis();
41   if (currentTime - startTime >= interval)
42   {
43     startTime += interval;                         // increment timing sequence
44     analogWrite(led1Pin, CIELPWM(brightness));     // set the brightness of pin 9:, 0-31, 5 bit steps of brightness
45     brightness += fadeAmount;                      // change the brightness for next time through the loop:
46     if (brightness == 0 || brightness == 31)       // reverse the direction of the fading at the ends of the fade:
47     {
48       fadeAmount = -fadeAmount ;
49     }
50   }
51 }