Download the file
  1. #include <avr/interrupt.h>
  2. #include <avr/io.h>
  3. #include "TwoColorLED.h"
  4. #include "RotaryEncoder.h"
  5. #include "PushButton.h"
  6. #include "Random.h"
  7. #include <math.h>
  8.  
  9. volatile uint8_t counter = 0;
  10. volatile uint8_t compare = 8;
  11. #define MAX_COMPARE 0xFF
  12. #define MAX_ILLUMINATION 500
  13. #define MAX_LEDS 3
  14. #define MAX_BUTTONS 4
  15.  
  16. RotaryEncoder re(12,13);
  17. TwoColorLED leds[MAX_LEDS] = { TwoColorLED(6,7), TwoColorLED(3,4), TwoColorLED(9,11) };
  18. PushButton buttons[MAX_BUTTONS] = { PushButton(8), PushButton(2), PushButton(10), PushButton(5) };
  19.  
  20. #define LEDS(op) leds[0].op ; leds[1].op; leds[2].op
  21.  
  22. //Timer2 overflow interrupt vector handler, called (16,000,000/256)/8 times per second
  23. ISR(TIMER2_OVF_vect) {
  24. counter = (counter + 1) & MAX_COMPARE;
  25. if (counter == 0 && compare > 0) {
  26. LEDS(pulse(1));
  27. }
  28. else if (counter == compare && compare < MAX_COMPARE) {
  29. LEDS(pulse(0));
  30. }
  31. };
  32.  
  33. uint8_t dim_steps[] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F};
  34. uint8_t step = 0;
  35.  
  36. void setup() {
  37. //Serial.begin(57600);
  38.  
  39. //Timer2 Settings: Timer Prescaler /8, WGM mode 0
  40. TCCR2A = 0;
  41. TCCR2B = 1<<CS20;
  42. //Timer2 Overflow Interrupt Enable
  43. TIMSK2 = 1<<TOIE2;
  44. //reset timer
  45. TCNT2 = 0;
  46.  
  47. Serial.begin(57600);
  48. LEDS(red());
  49. delay(333);
  50. LEDS(yellow());
  51. delay(333);
  52. LEDS(green());
  53. delay(333);
  54. LEDS(deactivate());
  55. }
  56.  
  57. float illumination() {
  58. unsigned int v = analogRead(0);
  59. float I = 398.0 * pow((v/(1023.0 - v)), -1.8);
  60. int n = log10(I);
  61. I /= pow(10,n);
  62. I = round(I*10)*pow(10, n-1);
  63. return I;
  64. }
  65.  
  66. void handleInput() {
  67. static uint8_t wait_next;
  68. static int8_t id;
  69. if (Serial.available()) {
  70. char c = Serial.read();
  71. if (c == 'L') {
  72. Serial.print("L");
  73. Serial.println(illumination());
  74. return;
  75. }
  76. else if (c == 'Q') {
  77. Serial.print("Q");
  78. Serial.println(randomByte(), BYTE);
  79. }
  80. else if (!wait_next) {
  81. id = c - 48;
  82. if (id < 0 || id >= MAX_LEDS) {
  83. //Serial.read(); // Remove the next char too
  84. Serial.println("Ei");
  85. return ;
  86. }
  87. else
  88. wait_next = 1;
  89. }
  90. else {
  91. switch (c) {
  92. case 'R':
  93. leds[id].red();
  94. break;
  95. case 'G':
  96. leds[id].green();
  97. break;
  98. case 'Y':
  99. leds[id].yellow();
  100. break;
  101. case 'B':
  102. leds[id].deactivate();
  103. break;
  104. default:
  105. Serial.println("Ec");
  106. break;
  107. }
  108. wait_next = 0;
  109. }
  110. }
  111. }
  112.  
  113. void loop () {
  114.  
  115. handleInput();
  116.  
  117. int8_t r = re.read();
  118. if (r == 1)
  119. Serial.println("R+");
  120. else if (r == -1)
  121. Serial.println("R-");
  122.  
  123. for (uint8_t i = 0; i<MAX_BUTTONS; i++) {
  124. if (buttons[i].getPressed()) {
  125. Serial.print("B");
  126. Serial.println(i, DEC);
  127. }
  128. }
  129. if (millis() % 1000 == 0) {
  130. long I = illumination();
  131. if (I > MAX_ILLUMINATION) I = MAX_ILLUMINATION;
  132. compare = I * MAX_COMPARE / MAX_ILLUMINATION;
  133. if (compare == 0) compare = 1;
  134. }
  135. }
  136.