Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CLK 2
- #define DT 3
- #define R 9
- #define G 10
- #define B 11
- int currentLedValue = 0;
- int currentStateCLK;
- int lastStateCLK;
- String currentDir ="";
- int currentLedPin;
- void setup()
- {
- pinMode(CLK,INPUT);
- pinMode(DT,INPUT);
- pinMode(R,OUTPUT);
- pinMode(G,OUTPUT);
- pinMode(B,OUTPUT);
- Serial.begin(9600);
- currentLedPin = 9;
- lastStateCLK = digitalRead(CLK);
- }
- void loop()
- {
- currentStateCLK = digitalRead(CLK);
- if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
- if (digitalRead(DT) != currentStateCLK)
- {
- currentLedValue = currentLedValue - 5;
- currentDir ="CCW";
- }
- else
- {
- currentLedValue = currentLedValue + 5;
- currentDir ="CW";
- }
- if (currentLedValue > 255)
- {
- currentLedValue = 0;
- analogWrite (currentLedPin, currentLedValue);
- if (currentLedPin == 9)
- {
- currentLedPin = 10;
- }
- else if (currentLedPin == 10)
- {
- currentLedPin = 11;
- }
- else if (currentLedPin == 11)
- {
- currentLedPin = 9;
- }
- }
- if (currentLedValue < 0)
- {
- currentLedValue = 255;
- if (currentLedPin == 9)
- {
- currentLedPin = 11;
- }
- else if (currentLedPin == 11)
- {
- currentLedPin = 10;
- }
- else if (currentLedPin == 10)
- {
- currentLedPin = 9;
- }
- }
- Serial.print("Direction: ");
- Serial.print(currentDir);
- Serial.print(" | Led Value: ");
- Serial.println(currentLedValue);
- analogWrite (currentLedPin, currentLedValue);
- }
- lastStateCLK = currentStateCLK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement