Advertisement
belrey10

RotaryRGBLed

Apr 9th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1.  
  2. #define CLK 2
  3. #define DT 3
  4. #define R 9
  5. #define G 10
  6. #define B 11
  7.  
  8.  
  9. int currentLedValue = 0;
  10. int currentStateCLK;
  11. int lastStateCLK;
  12. String currentDir ="";
  13. int currentLedPin;
  14.  
  15. void setup()
  16. {
  17.  
  18. pinMode(CLK,INPUT);
  19. pinMode(DT,INPUT);
  20. pinMode(R,OUTPUT);
  21. pinMode(G,OUTPUT);
  22. pinMode(B,OUTPUT);
  23.  
  24. Serial.begin(9600);
  25.  
  26. currentLedPin = 9;
  27. lastStateCLK = digitalRead(CLK);
  28. }
  29.  
  30. void loop()
  31. {
  32. currentStateCLK = digitalRead(CLK);
  33.  
  34. if (currentStateCLK != lastStateCLK && currentStateCLK == 1)
  35. {
  36. if (digitalRead(DT) != currentStateCLK)
  37. {
  38. currentLedValue = currentLedValue - 5;
  39. currentDir ="CCW";
  40. }
  41. else
  42. {
  43. currentLedValue = currentLedValue + 5;
  44. currentDir ="CW";
  45. }
  46.  
  47. if (currentLedValue > 255)
  48. {
  49. currentLedValue = 0;
  50. analogWrite (currentLedPin, currentLedValue);
  51. if (currentLedPin == 9)
  52. {
  53. currentLedPin = 10;
  54. }
  55. else if (currentLedPin == 10)
  56. {
  57. currentLedPin = 11;
  58. }
  59. else if (currentLedPin == 11)
  60. {
  61. currentLedPin = 9;
  62. }
  63. }
  64.  
  65. if (currentLedValue < 0)
  66. {
  67. currentLedValue = 255;
  68. if (currentLedPin == 9)
  69. {
  70. currentLedPin = 11;
  71. }
  72. else if (currentLedPin == 11)
  73. {
  74. currentLedPin = 10;
  75. }
  76. else if (currentLedPin == 10)
  77. {
  78. currentLedPin = 9;
  79. }
  80. }
  81.  
  82. Serial.print("Direction: ");
  83. Serial.print(currentDir);
  84. Serial.print(" | Led Value: ");
  85. Serial.println(currentLedValue);
  86. analogWrite (currentLedPin, currentLedValue);
  87. }
  88.  
  89. lastStateCLK = currentStateCLK;
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement