Advertisement
belrey10

RotaryEncoderRGBController

Apr 9th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 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.  
  35.   if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){
  36.  
  37.     if (digitalRead(DT) != currentStateCLK)
  38.     {
  39.       currentLedValue = currentLedValue - 5;
  40.       currentDir ="CCW";
  41.     }
  42.     else
  43.     {
  44.       currentLedValue = currentLedValue + 5;
  45.       currentDir ="CW";
  46.     }
  47.  
  48.   if (currentLedValue > 255)
  49.   {
  50.     currentLedValue = 0;
  51.     analogWrite (currentLedPin, currentLedValue);
  52.     if (currentLedPin == 9)
  53.     {
  54.       currentLedPin = 10;
  55.     }
  56.     else if (currentLedPin == 10)
  57.     {
  58.       currentLedPin = 11;
  59.     }
  60.     else if (currentLedPin == 11)
  61.     {
  62.       currentLedPin = 9;
  63.     }
  64.   }
  65.  
  66.   if (currentLedValue < 0)
  67.   {
  68.     currentLedValue = 255;
  69.      if (currentLedPin == 9)
  70.     {
  71.       currentLedPin = 11;
  72.     }
  73.     else if (currentLedPin == 11)
  74.     {
  75.       currentLedPin = 10;
  76.     }
  77.     else if (currentLedPin == 10)
  78.     {
  79.       currentLedPin = 9;
  80.     }
  81.   }
  82.  
  83.     Serial.print("Direction: ");
  84.     Serial.print(currentDir);
  85.     Serial.print(" | Led Value: ");
  86.     Serial.println(currentLedValue);
  87.     analogWrite (currentLedPin, currentLedValue);
  88.   }
  89.  
  90.   lastStateCLK = currentStateCLK;
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement