Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //define pins
- int triggerButton = 6;
- int relayPin = 8;
- #define inputCLK 2
- #define inputDT 3
- //define variables
- int triggerVal = 1;
- int delayValue = 200;
- int counter = 0;
- int currentStateCLK;
- int previousStateCLK;
- String encdir ="";
- void setup() {
- //define pins
- pinMode (triggerButton, INPUT_PULLUP);
- pinMode (relayPin, OUTPUT);
- pinMode (inputCLK,INPUT_PULLUP);
- pinMode (inputDT,INPUT_PULLUP);
- Serial.begin(9600);
- previousStateCLK = digitalRead(inputCLK);
- }
- void weld(){
- triggerVal = digitalRead(triggerButton);
- //Serial.println(triggerVal);
- if (triggerVal == 0) {
- digitalWrite(relayPin, HIGH);
- delay(delayValue);
- digitalWrite(relayPin, LOW);
- triggerVal = 1;
- delay(2000);
- }
- }
- void loop() {
- // Read the current state of inputCLK
- //Serial.print(digitalRead(inputDT));
- //Serial.print(" ");
- //Serial.print(digitalRead(inputCLK));
- //Serial.print(" ");
- currentStateCLK = digitalRead(inputCLK);
- // If the previous and the current state of the inputCLK are different then a pulse has occured
- if (currentStateCLK != previousStateCLK){
- // If the inputDT state is different than the inputCLK state then
- // the encoder is rotating counterclockwise
- if (digitalRead(inputDT) != currentStateCLK) {
- counter --;
- encdir ="CCW";
- } else {
- // Encoder is rotating clockwise
- counter ++;
- encdir ="CW";
- }
- Serial.print("Direction: ");
- Serial.print(encdir);
- Serial.print(" -- Value: ");
- Serial.println(counter);
- }
- // Update previousStateCLK with the current state
- previousStateCLK = currentStateCLK;
- weld();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement