Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // Define variables:
  2. /*
  3. byte ledG=5;
  4. byte ledR=3;
  5. byte ledB=6;
  6. */
  7.  
  8. byte current[3]={255,255,255};
  9.  
  10. void setup(){
  11. pinMode(3, OUTPUT);
  12. pinMode(5, OUTPUT);
  13. pinMode(6, OUTPUT);
  14. // Make random seed to generate colours:
  15. randomSeed(analogRead(0));
  16. }
  17.  
  18. void loop(){
  19. // Assign values to each color
  20. byte newR=(byte) random() % 255;
  21. byte newG=(byte) random() % 255;
  22. byte newB=(byte) random() % 255;
  23.  
  24. while(current[0] != newR || current[1] != newG || current[2] != newB){
  25. current[0]=adjust(current[0], newR, 3);
  26. current[1]=adjust(current[1], newG, 5);
  27. current[2]=adjust(current[2], newB, 6);
  28. }
  29. /* more efficient but
  30. /* while(current[0] != newR){ */
  31. /* current[0]=adjust(current[0], newR, 3); */
  32. /* } */
  33. /* while(current[1] != newG){ */
  34. /* current[1]=adjust(current[1], newG, 5); */
  35. /* } */
  36. /* while(current[2] != newB){ */
  37. /* current[2]=adjust(current[2], newB, 6); */
  38. /* } */
  39. delay(180000);
  40. }
  41.  
  42. byte adjust(byte oldVal, byte newVal, byte pinNo){
  43. if(oldVal > newVal){
  44. analogWrite(pinNo, oldVal-1);
  45. return oldVal-1;
  46. }else if(oldVal < newVal){
  47. analogWrite(pinNo, oldVal+1);
  48. return oldVal+1;
  49. }else{
  50. return oldVal;
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement