From f2de80ea596c958fb5d919883befabfad25dcae2 Mon Sep 17 00:00:00 2001 From: eaman Date: Mon, 16 Jan 2017 03:08:27 +0100 Subject: [PATCH] Correzione di luminosita --- basic/pwm/pmw_5_coseno/pmw_5_coseno.ino | 8 +-- .../pwm_6_states_lum_correction_32.ino | 51 +++++++++++++++ .../pwm_7_1_states_dither.ino | 64 +++++++++++++++++++ .../pwm_7_states_lum_correction_256.ino | 64 +++++++++++++++++++ libraries/common/common.cpp | 6 ++ libraries/common/common.h | 26 ++++++++ 6 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 basic/pwm/pwm_6_states_lum_correction_32/pwm_6_states_lum_correction_32.ino create mode 100644 basic/pwm/pwm_7_1_states_dither/pwm_7_1_states_dither.ino create mode 100644 basic/pwm/pwm_7_states_lum_correction_256/pwm_7_states_lum_correction_256.ino diff --git a/basic/pwm/pmw_5_coseno/pmw_5_coseno.ino b/basic/pwm/pmw_5_coseno/pmw_5_coseno.ino index 91f9214..ccf03ed 100644 --- a/basic/pwm/pmw_5_coseno/pmw_5_coseno.ino +++ b/basic/pwm/pmw_5_coseno/pmw_5_coseno.ino @@ -8,8 +8,8 @@ */ int value, value2 ; -int ledpin = 10; // light connected to digital pin 10 -int ledpin2 = 11; // light connected to digital pin 11 +int ledpin = 9; // light connected to digital pin 10 +int ledpin2 = 11; // light connected to digital pin 11 long time=0; int periode = 2000; @@ -25,8 +25,8 @@ void loop() time = millis(); value = 128+127*cos(2*PI/periode*time); value2 = 128+127*cos(2*PI/periode*(displace-time)); - analogWrite(ledpin, value); // sets the value (range from 0 to 255) - analogWrite(ledpin2, value2); // sets the value (range from 0 to 255) + analogWrite(ledpin, value); // sets the value (range from 0 to 255) + analogWrite(ledpin2, value2);// sets the value (range from 0 to 255) } /* Esercizi: diff --git a/basic/pwm/pwm_6_states_lum_correction_32/pwm_6_states_lum_correction_32.ino b/basic/pwm/pwm_6_states_lum_correction_32/pwm_6_states_lum_correction_32.ino new file mode 100644 index 0000000..97b2d23 --- /dev/null +++ b/basic/pwm/pwm_6_states_lum_correction_32/pwm_6_states_lum_correction_32.ino @@ -0,0 +1,51 @@ +/* + Change brightness of LED linearly to Human eye + 32 step brightness using 8 bit PWM of Arduino + brightness step 24 should be twice bright than step 12 to your eye. + + https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ +*/ + + +#include +#define CIELPWM(a) (pgm_read_byte_near(CIEL8 + a)) // CIE Lightness lookup table function + +/* + 5 bit CIE Lightness to 8 bit PWM conversion + https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ + L* = 116(Y/Yn)^1/3 - 16 , Y/Yn > 0.008856 + L* = 903.3(Y/Yn), Y/Yn <= 0.008856 +*/ + +const uint8_t CIEL8[] PROGMEM = { + 0, 1, 2, 3, 4, 5, 7, 9, 12, + 15, 18, 22, 27, 32, 38, 44, 51, 58, + 67, 76, 86, 96, 108, 120, 134, 148, 163, + 180, 197, 216, 235, 255 +}; + +int brightness = 0; // initial brightness of LED +int fadeAmount = 1; +int led1Pin = 9; // pause between each interval (32) + +unsigned long startTime = 0; // timing variables +const long interval = 12; + + +void setup() { + pinMode(led1Pin, OUTPUT); // declare pin 9 to be an output: +} + +void loop() { + unsigned long currentTime = millis(); + if (currentTime - startTime >= interval) + { + startTime += interval; // increment timing sequence + analogWrite(led1Pin, CIELPWM(brightness)); // set the brightness of pin 9:, 0-31, 5 bit steps of brightness + brightness += fadeAmount; // change the brightness for next time through the loop: + if (brightness == 0 || brightness == 31) // reverse the direction of the fading at the ends of the fade: + { + fadeAmount = -fadeAmount ; + } + } +} diff --git a/basic/pwm/pwm_7_1_states_dither/pwm_7_1_states_dither.ino b/basic/pwm/pwm_7_1_states_dither/pwm_7_1_states_dither.ino new file mode 100644 index 0000000..4febeeb --- /dev/null +++ b/basic/pwm/pwm_7_1_states_dither/pwm_7_1_states_dither.ino @@ -0,0 +1,64 @@ +/* + Change brightness of LED linearly to Human eye + 256 step brightness using 8 bit PWM of Arduino + with quite a bit of dithering hard coded. + + https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ + https://learn.adafruit.com/led-tricks-gamma-correction/the-quick-fix +*/ + + +#include +#define CIELPWM(a) (pgm_read_byte_near(CIEL8 + a)) // CIE Lightness lookup table function + +/* + 5 bit CIE Lightness to 8 bit PWM conversion + https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ + L* = 116(Y/Yn)^1/3 - 16 , Y/Yn > 0.008856 + L* = 903.3(Y/Yn), Y/Yn <= 0.008856 +*/ + +const uint8_t CIEL8[] PROGMEM = { + 0,0,0,1,1,1,2,1,1,2,1,2,2,2,3,2, + 2,2,3,2,2,3,2,3,3,3,4,3,3,3,4,4, + 4,5,4,5,4,5,5,6,5,5,6,5,6,6,7,6, + 7,7,8,7,8,8,9,8,9,10,9,10,10,11,10,11, + 11,12,11,12,13,12,13,13,14,14,15,15,15,16,16,17, + 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24, + 25,25,26,26,27,28,28,29,29,30,31,31,32,32,33,34, + 34,35,36,37,37,38,39,39,40,41,42,43,43,44,45,46, + 47,47,48,49,50,51,52,53,54,54,55,56,57,58,59,60, + 61,62,63,64,65,66,67,68,70,71,72,73,74,75,76,77, + 79,80,81,82,83,85,86,87,88,90,91,92,94,95,96,98, + 99,100,102,103,105,106,108,109,110,112,113,115,116,118,120,121, + 123,124,126,128,129,131,132,134,136,138,139,141,143,145,146,148, + 150,152,154,155,157,159,161,163,165,167,169,171,173,175,177,179, + 181,183,185,187,189,191,193,196,198,200,202,204,207,209,211,214, + 216,218,220,223,225,228,230,232,235,237,240,242,245,248,252,255 +}; + +int brightness = 0; // initial brightness of LED +int fadeAmount = 1; +int led1Pin = 9; + +unsigned long startTime = 0; // timing variables +const long interval = 4; // pause between each interval (256) + + +void setup() { + pinMode(led1Pin, OUTPUT); // declare pin 9 to be an output: +} + +void loop() { + unsigned long currentTime = millis(); + if (currentTime - startTime >= interval) + { + startTime += interval; // increment timing sequence + analogWrite(led1Pin, CIELPWM(brightness)); // set the brightness of pin 9:, 0-31, 5 bit steps of brightness + brightness += fadeAmount; // change the brightness for next time through the loop: + if (brightness == 0 || brightness == 255) // reverse the direction of the fading at the ends of the fade: + { + fadeAmount = -fadeAmount ; + } + } +} diff --git a/basic/pwm/pwm_7_states_lum_correction_256/pwm_7_states_lum_correction_256.ino b/basic/pwm/pwm_7_states_lum_correction_256/pwm_7_states_lum_correction_256.ino new file mode 100644 index 0000000..c23d6b2 --- /dev/null +++ b/basic/pwm/pwm_7_states_lum_correction_256/pwm_7_states_lum_correction_256.ino @@ -0,0 +1,64 @@ +/* + Change brightness of LED linearly to Human eye + 256 step brightness using 8 bit PWM of Arduino + with slight correction at base. + + https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ + https://learn.adafruit.com/led-tricks-gamma-correction/the-quick-fix +*/ + + +#include +#define CIELPWM(a) (pgm_read_byte_near(CIEL8 + a)) // CIE Lightness lookup table function + +/* + 5 bit CIE Lightness to 8 bit PWM conversion + https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ + L* = 116(Y/Yn)^1/3 - 16 , Y/Yn > 0.008856 + L* = 903.3(Y/Yn), Y/Yn <= 0.008856 +*/ + +const uint8_t CIEL8[] PROGMEM = { + 0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2, + 2,2,2,2,2,3,3,3,3,3,3,3,3,3,4,4, + 4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6, + 7,7,7,7,8,8,8,8,9,9,9,10,10,10,10,11, + 11,11,12,12,12,13,13,13,14,14,15,15,15,16,16,17, + 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24, + 25,25,26,26,27,28,28,29,29,30,31,31,32,32,33,34, + 34,35,36,37,37,38,39,39,40,41,42,43,43,44,45,46, + 47,47,48,49,50,51,52,53,54,54,55,56,57,58,59,60, + 61,62,63,64,65,66,67,68,70,71,72,73,74,75,76,77, + 79,80,81,82,83,85,86,87,88,90,91,92,94,95,96,98, + 99,100,102,103,105,106,108,109,110,112,113,115,116,118,120,121, + 123,124,126,128,129,131,132,134,136,138,139,141,143,145,146,148, + 150,152,154,155,157,159,161,163,165,167,169,171,173,175,177,179, + 181,183,185,187,189,191,193,196,198,200,202,204,207,209,211,214, + 216,218,220,223,225,228,230,232,235,237,240,242,245,248,252,255 +}; + +int brightness = 0; // initial brightness of LED +int fadeAmount = 1; +int led1Pin = 1; + +unsigned long startTime = 0; // timing variables +const long interval = 4; // pause between each interval (256) + + +void setup() { + pinMode(led1Pin, OUTPUT); // declare pin 9 to be an output: +} + +void loop() { + unsigned long currentTime = millis(); + if (currentTime - startTime >= interval) + { + startTime += interval; // increment timing sequence + analogWrite(led1Pin, CIELPWM(brightness)); // set the brightness of pin 9:, 0-31, 5 bit steps of brightness + brightness += fadeAmount; // change the brightness for next time through the loop: + if (brightness == 0 || brightness == 255) // reverse the direction of the fading at the ends of the fade: + { + fadeAmount = -fadeAmount ; + } + } +} diff --git a/libraries/common/common.cpp b/libraries/common/common.cpp index d9c0233..3fd82db 100644 --- a/libraries/common/common.cpp +++ b/libraries/common/common.cpp @@ -201,3 +201,9 @@ pinMode(pin, OUTPUT); delay(velocita); // wait for a second }; + +byte lum(byte val) { + // Mappatura dell'intervallo 0-255 con correzione di luminosita. + // storata in SRAM +return pgm_read_byte_near(BCORRECT + val); +}; diff --git a/libraries/common/common.h b/libraries/common/common.h index dee00b6..ba07fa4 100644 --- a/libraries/common/common.h +++ b/libraries/common/common.h @@ -6,9 +6,33 @@ */ #include "Arduino.h" +#include + #ifndef common_h #define common_h +// Variabili +const uint8_t BCORRECT[256] PROGMEM = { // Tabella per correzione luminosita' PWM + 0,0,0,1,1,1,2,1,1,2,1,2,2,2,3,2, + 2,2,3,2,2,3,2,3,3,3,4,3,3,3,4,4, + 4,5,4,5,4,5,5,6,5,5,6,5,6,6,7,6, + 7,7,8,7,8,8,9,8,9,10,9,10,10,11,10,11, + 11,12,11,12,13,12,13,13,14,14,15,15,15,16,16,17, + 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24, + 25,25,26,26,27,28,28,29,29,30,31,31,32,32,33,34, + 34,35,36,37,37,38,39,39,40,41,42,43,43,44,45,46, + 47,47,48,49,50,51,52,53,54,54,55,56,57,58,59,60, + 61,62,63,64,65,66,67,68,70,71,72,73,74,75,76,77, + 79,80,81,82,83,85,86,87,88,90,91,92,94,95,96,98, + 99,100,102,103,105,106,108,109,110,112,113,115,116,118,120,121, + 123,124,126,128,129,131,132,134,136,138,139,141,143,145,146,148, + 150,152,154,155,157,159,161,163,165,167,169,171,173,175,177,179, + 181,183,185,187,189,191,193,196,198,200,202,204,207,209,211,214, + 216,218,220,223,225,228,230,232,235,237,240,242,245,247,251,255 +}; + +// Inserirne una con 32 valori + class RGBLed { // Classe rappresentativa di un LED RGB @@ -81,3 +105,5 @@ public: void brilla(byte pin, int velocita = 200) ; #endif + +byte lum(byte val); -- 2.39.2