Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CLK 2
- #define DT 3
- #define SW 4
- int currentStateCLK;
- int lastStateCLK;
- int counter = 0;
- String currentDir ="";
- unsigned long lastButtonPress = 0;
- void setup()
- {
- pinMode(CLK,INPUT);
- pinMode(DT,INPUT);
- pinMode(SW, INPUT_PULLUP);
- Serial.begin(9600);
- lastStateCLK = digitalRead(CLK);
- Serial.print("Direction: ");
- Serial.print(currentDir);
- Serial.println(counter);
- }
- void loop()
- {
- currentStateCLK = digitalRead(CLK);
- if (currentStateCLK != lastStateCLK && currentStateCLK == 1)
- {
- if (digitalRead(DT) != currentStateCLK)
- {
- counter = counter + 1;
- if (counter > 100)
- {
- counter = 0;
- }
- currentDir ="UP";
- } else
- {
- counter = counter - 1;
- if (counter < 0)
- {
- counter = 100;
- }
- currentDir ="DOWN";
- }
- Serial.print("Direction: ");
- Serial.print(currentDir);
- Serial.print(" - Counter: ");
- Serial.println(counter);
- }
- lastStateCLK = currentStateCLK;
- int btnState = digitalRead(SW);
- if (btnState == LOW)
- {
- if (millis() - lastButtonPress > 50)
- {
- Serial.println("Button pressed!");
- }
- lastButtonPress = millis();
- }
- delay(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement