Created
November 27, 2015 22:36
-
-
Save netmaniac/8be83f2e66ae25e949f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define NOT_CORRECTED 10 | |
/* | |
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. | |
*/ | |
#include <avr/pgmspace.h> | |
#define CIELPWM(a) (pgm_read_word_near(CIEL8 + a)) // CIE Lightness loopup table function | |
/* | |
5 bit CIE Lightness to 8 bit PWM conversion | |
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; | |
unsigned long last_event; | |
void stepCorrectedPWM(int b) { | |
// set the brightness of pin 9:, 0-31, 5 bit steps of brightness | |
analogWrite(9, CIELPWM(b)); | |
// change the brightness for next time through the loop: | |
// reverse the direction of the fading at the ends of the fade: | |
if (b == 0 || b == 31) { | |
fadeAmount = -fadeAmount ; | |
} | |
Serial.println(millis() - last_event); | |
last_event = millis(); | |
} | |
int pwm = 0; | |
int d_pwm = 1; | |
void stepPWM(int pwm) { | |
analogWrite(NOT_CORRECTED, pwm); | |
} | |
void setup() { | |
// declare pin 9 to be an output: | |
pinMode(9, OUTPUT); | |
Serial.begin(230400); | |
last_event = millis(); | |
} | |
void loop() { | |
for (int i = 0; i < 255; i++) { | |
stepPWM(i); | |
stepCorrectedPWM(i/8); | |
delay(25); | |
} | |
// delay(1000); | |
for (int i = 255; i >= 0; i--) { | |
stepPWM(i); | |
stepCorrectedPWM(i/8); | |
delay(25); | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment