Advertisement
Guest User

MakerTron - MainteneTron

a guest
Oct 16th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. // MakerTron - MainteneTron
  2.  
  3. // LED Pins
  4. int l_r = 3;
  5. int l_b = 5;
  6. int l_g = 6;
  7. int r_r = 9;
  8. int r_b = 10;
  9. int r_g = 11;
  10.  
  11. int uv = 4;
  12.  
  13. // Color arrays
  14. int black[3] = { 0, 0, 0 };
  15. int white[3] = { 100, 100, 100 };
  16. int red[3] = { 100, 0, 0 };
  17. int green[3] = { 0, 100, 0 };
  18. int blue[3] = { 0, 0, 100 };
  19. int yellow[3] = { 40, 95, 0 };
  20. int dimWhite[3] = { 30, 30, 30 };
  21. // etc.
  22.  
  23. // Set initial color
  24. int redVal = black[0];
  25. int grnVal = black[1];
  26. int bluVal = black[2];
  27.  
  28.  
  29. int wait = 2; // 10ms internal crossFade delay; increase for slower fades
  30. int hold = 1000; // Optional hold when a color is complete, before the next crossFade
  31. int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
  32. int loopCount = 60; // How often should DEBUG report?
  33. int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
  34. int j = 0; // Loop counter for repeat
  35.  
  36. // Initialize color variables
  37. int prevR = redVal;
  38. int prevG = grnVal;
  39. int prevB = bluVal;
  40.  
  41.  
  42. void setup() {
  43. pinMode(l_r, OUTPUT);
  44. pinMode(l_g, OUTPUT);
  45. pinMode(l_b, OUTPUT);
  46. pinMode(r_r, OUTPUT);
  47. pinMode(r_g, OUTPUT);
  48. pinMode(r_b, OUTPUT);
  49. pinMode(uv, OUTPUT);
  50. analogWrite(l_r, 255);
  51. analogWrite(l_g, 255);
  52. analogWrite(l_b, 255);
  53. analogWrite(r_r, 255);
  54. analogWrite(r_g, 255);
  55. analogWrite(r_b, 255);
  56. digitalWrite(uv, LOW);
  57. }
  58.  
  59. void loop() {
  60. crossFade(red);
  61. laserFire();
  62. crossFade(green);
  63. laserFire();
  64. crossFade(blue);
  65. laserFire();
  66. crossFade(yellow);
  67. laserFire();
  68. }
  69.  
  70. void laserFire(){
  71. for (int i = 0; i < 10; i++){
  72. digitalWrite(uv, HIGH);
  73. delay(50);
  74. digitalWrite(uv, LOW);
  75. delay(50);
  76. }
  77.  
  78. }
  79. int calculateStep(int prevValue, int endValue) {
  80. int step = endValue - prevValue; // What's the overall gap?
  81. if (step) { // If its non-zero,
  82. step = 1020/step; // divide by 1020
  83. }
  84. return step;
  85. }
  86.  
  87. /* The next function is calculateVal. When the loop value, i,
  88. * reaches the step size appropriate for one of the
  89. * colors, it increases or decreases the value of that color by 1.
  90. * (R, G, and B are each calculated separately.)
  91. */
  92.  
  93. int calculateVal(int step, int val, int i) {
  94.  
  95. if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
  96. if (step > 0) { // increment the value if step is positive...
  97. val += 1;
  98. }
  99. else if (step < 0) { // ...or decrement it if step is negative
  100. val -= 1;
  101. }
  102. }
  103. // Defensive driving: make sure val stays in the range 0-255
  104. if (val > 255) {
  105. val = 255;
  106. }
  107. else if (val < 0) {
  108. val = 0;
  109. }
  110. return val;
  111. }
  112.  
  113. /* crossFade() converts the percentage colors to a
  114. * 0-255 range, then loops 1020 times, checking to see if
  115. * the value needs to be updated each time, then writing
  116. * the color values to the correct pins.
  117. */
  118.  
  119. void crossFade(int color[3]) {
  120. // Convert to 0-255
  121. int R = (color[0] * 255) / 100;
  122. int G = (color[1] * 255) / 100;
  123. int B = (color[2] * 255) / 100;
  124.  
  125. int stepR = calculateStep(prevR, R);
  126. int stepG = calculateStep(prevG, G);
  127. int stepB = calculateStep(prevB, B);
  128.  
  129. for (int i = 0; i <= 1020; i++) {
  130. redVal = calculateVal(stepR, redVal, i);
  131. grnVal = calculateVal(stepG, grnVal, i);
  132. bluVal = calculateVal(stepB, bluVal, i);
  133.  
  134. setEyeColorRGB(redVal, grnVal, bluVal);
  135. delay(wait); // Pause for 'wait' milliseconds before resuming the loop
  136.  
  137. }
  138.  
  139. // Update current values for next loop
  140. prevR = redVal;
  141. prevG = grnVal;
  142. prevB = bluVal;
  143. delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
  144. }
  145.  
  146.  
  147. void setEyeColorRGB(int r, int g, int b){
  148. r = r / 10;
  149. g = g / 10;
  150. b = b / 10;
  151. analogWrite(l_r, map(r, 0, 255, 255, 0));
  152. analogWrite(l_g, map(g, 0, 255, 255, 0));
  153. analogWrite(l_b, map(b, 0, 255, 255, 0));
  154. analogWrite(r_r, map(r, 0, 255, 255, 0));
  155. analogWrite(r_g, map(g, 0, 255, 255, 0));
  156. analogWrite(r_b, map(b, 0, 255, 255, 0));
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement