Advertisement
Guest User

Untitled

a guest
Aug 29th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. //define pins
  2. int triggerButton = 6;
  3. int relayPin = 8;
  4. #define inputCLK 2
  5. #define inputDT 3
  6.  
  7. //define variables
  8. int triggerVal = 1;
  9. int delayValue = 200;
  10.  
  11. int counter = 0;
  12. int currentStateCLK;
  13. int previousStateCLK;
  14.  
  15.  String encdir ="";
  16.  
  17. void setup() {
  18.   //define pins
  19. pinMode (triggerButton, INPUT_PULLUP);
  20. pinMode (relayPin, OUTPUT);
  21.  
  22. pinMode (inputCLK,INPUT_PULLUP);
  23. pinMode (inputDT,INPUT_PULLUP);
  24. Serial.begin(9600);
  25. previousStateCLK = digitalRead(inputCLK);
  26. }
  27.  
  28. void weld(){
  29.     triggerVal = digitalRead(triggerButton);
  30.   //Serial.println(triggerVal);
  31. if (triggerVal == 0) {
  32.   digitalWrite(relayPin, HIGH);
  33.   delay(delayValue);
  34.   digitalWrite(relayPin, LOW);
  35.   triggerVal = 1;
  36.   delay(2000);
  37. }
  38.   }
  39.  
  40. void loop() {
  41. // Read the current state of inputCLK
  42. //Serial.print(digitalRead(inputDT));
  43. //Serial.print("  ");
  44. //Serial.print(digitalRead(inputCLK));
  45. //Serial.print("  ");
  46.    currentStateCLK = digitalRead(inputCLK);
  47.    
  48.    // If the previous and the current state of the inputCLK are different then a pulse has occured
  49.    if (currentStateCLK != previousStateCLK){
  50.        
  51.      // If the inputDT state is different than the inputCLK state then
  52.      // the encoder is rotating counterclockwise
  53.      if (digitalRead(inputDT) != currentStateCLK) {
  54.        counter --;
  55.        encdir ="CCW";
  56.        
  57.      } else {
  58.        // Encoder is rotating clockwise
  59.        counter ++;
  60.        encdir ="CW";
  61.        
  62.      }
  63.      Serial.print("Direction: ");
  64.      Serial.print(encdir);
  65.      Serial.print(" -- Value: ");
  66.      Serial.println(counter);
  67.    }
  68.    // Update previousStateCLK with the current state
  69.    previousStateCLK = currentStateCLK;
  70. weld();
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement