Advertisement
Guest User

Azuretek Arduino Random Color Change

a guest
Nov 1st, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #define RING_BLUE 9
  2. #define RING_GREEN 10
  3. #define RING_RED 11
  4.  
  5. #define CIRCLE_BLUE 6
  6. #define CIRCLE_GREEN 5
  7. #define CIRCLE_RED 3
  8.  
  9. #define MAX 255
  10.  
  11. #define DELAY_ITERATION 50
  12. #define DELAYTIME 20
  13.  
  14. int ring_iteration = 0;
  15. int ring_cur_colors[3] = { 0, 0, 0 };
  16. int ring_new_colors[3] = { 0, 0, 0 };
  17.  
  18. int circle_iteration = 0;
  19. int circle_cur_colors[3] = { 0, 0, 0 };
  20. int circle_new_colors[3] = { 0, 0, 0 };
  21.  
  22. void setup() {
  23. pinMode( RING_BLUE, OUTPUT );
  24. pinMode( RING_GREEN, OUTPUT );
  25. pinMode( RING_RED, OUTPUT );
  26. digitalWrite( RING_BLUE, HIGH );
  27. digitalWrite( RING_GREEN, HIGH );
  28. digitalWrite( RING_RED, HIGH );
  29.  
  30. pinMode( CIRCLE_BLUE, OUTPUT );
  31. pinMode( CIRCLE_GREEN, OUTPUT );
  32. pinMode( CIRCLE_RED, OUTPUT );
  33. digitalWrite( CIRCLE_BLUE, HIGH );
  34. digitalWrite( CIRCLE_GREEN, HIGH );
  35. digitalWrite( CIRCLE_RED, HIGH );
  36.  
  37. // Pick random color to start on
  38. randomSeed(analogRead(0));
  39. randomColor( 0 );
  40. delay( 1000 );
  41.  
  42. randomSeed(analogRead(0));
  43. randomColor( 1 );
  44. }
  45.  
  46. void loop() {
  47. // Color change for the ring
  48. // If the current color matches new colors pick a new color
  49. // Stay on new color for a second
  50. if ( ring_cur_colors[0] == ring_new_colors[0] &&
  51. ring_cur_colors[1] == ring_new_colors[1] &&
  52. ring_cur_colors[2] == ring_new_colors[2]) {
  53. if ( ring_iteration > DELAY_ITERATION ) {
  54. randomColor( 0 );
  55. ring_iteration = 0;
  56. } else {
  57. ring_iteration += 1;
  58. }
  59. }
  60.  
  61. // If color is less than or greater than new color adjust current color + or - 1
  62. if ( ring_cur_colors[0] > ring_new_colors[0] ) {
  63. ring_cur_colors[0] -= 1;
  64. }
  65. if ( ring_cur_colors[0] < ring_new_colors[0] ) {
  66. ring_cur_colors[0] += 1;
  67. }
  68.  
  69. if ( ring_cur_colors[1] > ring_new_colors[1] ) {
  70. ring_cur_colors[1] -= 1;
  71. }
  72. if ( ring_cur_colors[1] < ring_new_colors[1] ) {
  73. ring_cur_colors[1] += 1;
  74. }
  75.  
  76. if ( ring_cur_colors[2] > ring_new_colors[2] ) {
  77. ring_cur_colors[2] -= 1;
  78. }
  79. if ( ring_cur_colors[2] < ring_new_colors[2] ) {
  80. ring_cur_colors[2] += 1;
  81. }
  82.  
  83. analogWrite( RING_BLUE, ring_cur_colors[0] );
  84. analogWrite( RING_GREEN, ring_cur_colors[1] );
  85. analogWrite( RING_RED, ring_cur_colors[2] );
  86.  
  87.  
  88. // Color change for the circle
  89. // If the current color matches new colors pick a new color
  90. // Stay on new color for a second
  91. if ( circle_cur_colors[0] == circle_new_colors[0] &&
  92. circle_cur_colors[1] == circle_new_colors[1] &&
  93. circle_cur_colors[2] == circle_new_colors[2]) {
  94. if ( circle_iteration > DELAY_ITERATION ) {
  95. randomColor( 1 );
  96. circle_iteration = 0;
  97. } else {
  98. circle_iteration += 1;
  99. }
  100. }
  101.  
  102. // If color is less than or greater than new color adjust current color + or - 1
  103. if ( circle_cur_colors[0] > circle_new_colors[0] ) {
  104. circle_cur_colors[0] -= 1;
  105. }
  106. if ( circle_cur_colors[0] < circle_new_colors[0] ) {
  107. circle_cur_colors[0] += 1;
  108. }
  109.  
  110. if ( circle_cur_colors[1] > circle_new_colors[1] ) {
  111. circle_cur_colors[1] -= 1;
  112. }
  113. if ( circle_cur_colors[1] < circle_new_colors[1] ) {
  114. circle_cur_colors[1] += 1;
  115. }
  116.  
  117. if ( circle_cur_colors[2] > circle_new_colors[2] ) {
  118. circle_cur_colors[2] -= 1;
  119. }
  120. if ( circle_cur_colors[2] < circle_new_colors[2] ) {
  121. circle_cur_colors[2] += 1;
  122. }
  123.  
  124. analogWrite( CIRCLE_BLUE, circle_cur_colors[0] );
  125. analogWrite( CIRCLE_GREEN, circle_cur_colors[1] );
  126. analogWrite( CIRCLE_RED, circle_cur_colors[2] );
  127.  
  128. delay( DELAYTIME );
  129. }
  130.  
  131. void randomColor( int light ) {
  132. // light 0 == ring
  133. // light 1 == circle
  134. if ( light == 0 ) {
  135. ring_new_colors[0] = random( MAX );
  136. ring_new_colors[1] = random( MAX );
  137. ring_new_colors[2] = random( MAX );
  138. } else {
  139. circle_new_colors[0] = random( MAX );
  140. circle_new_colors[1] = random( MAX );
  141. circle_new_colors[2] = random( MAX );
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement