Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #define NOT_CORRECTED 10
  2.  
  3. /*
  4. Change brightness of LED linearly to Human eye
  5. 32 step brightness using 8 bit PWM of Arduino
  6. brightness step 24 should be twice bright than step 12 to your eye.
  7. */
  8.  
  9. #include <avr/pgmspace.h>
  10. #define CIELPWM(a) (pgm_read_word_near(CIEL8 + a)) // CIE Lightness loopup table function
  11.  
  12. /*
  13. 5 bit CIE Lightness to 8 bit PWM conversion
  14. L* = 116(Y/Yn)^1/3 - 16 , Y/Yn > 0.008856
  15. L* = 903.3(Y/Yn), Y/Yn <= 0.008856
  16. */
  17.  
  18. const uint8_t CIEL8[] PROGMEM = {
  19. 0, 1, 2, 3, 4, 5, 7, 9, 12,
  20. 15, 18, 22, 27, 32, 38, 44, 51, 58,
  21. 67, 76, 86, 96, 108, 120, 134, 148, 163,
  22. 180, 197, 216, 235, 255
  23. };
  24.  
  25. int brightness = 0; // initial brightness of LED
  26. int fadeAmount = 1;
  27.  
  28. unsigned long last_event;
  29.  
  30.  
  31.  
  32. void stepCorrectedPWM(int b) {
  33. // set the brightness of pin 9:, 0-31, 5 bit steps of brightness
  34. analogWrite(9, CIELPWM(b));
  35.  
  36. // change the brightness for next time through the loop:
  37.  
  38. // reverse the direction of the fading at the ends of the fade:
  39. if (b == 0 || b == 31) {
  40. fadeAmount = -fadeAmount ;
  41. }
  42. Serial.println(millis() - last_event);
  43. last_event = millis();
  44.  
  45. }
  46.  
  47. int pwm = 0;
  48. int d_pwm = 1;
  49.  
  50. void stepPWM(int pwm) {
  51. analogWrite(NOT_CORRECTED, pwm);
  52. }
  53. void setup() {
  54. // declare pin 9 to be an output:
  55. pinMode(9, OUTPUT);
  56. Serial.begin(230400);
  57. last_event = millis();
  58. }
  59.  
  60. void loop() {
  61. for (int i = 0; i < 255; i++) {
  62. stepPWM(i);
  63. stepCorrectedPWM(i/8);
  64. delay(25);
  65. }
  66. // delay(1000);
  67. for (int i = 255; i >= 0; i--) {
  68. stepPWM(i);
  69. stepCorrectedPWM(i/8);
  70. delay(25);
  71. }
  72. delay(500);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement