Rotary Encoder control for led

// Rotary Encoder Inputs
#define CLK 4
#define DT 2
#define SW 3

#define DPR 50 // Debounce timer for press and release
#define LPR 3000 // Long press/release in milliseconds

// LED Outputs
int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;

unsigned char readBits = 0b111; // 8 bits
unsigned char rotationBits = 0b011; // 8 bits
unsigned char buttonBits = 0b111; // 8 bits
int counter = 0;
String currentDir = "";
unsigned long lastActionButton = 0;

void setup() {

// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);

// Set LED pins as Outputs
pinMode (led1,OUTPUT);
digitalWrite (led1, LOW);

pinMode (led2,OUTPUT);
digitalWrite(led2, LOW);

pinMode (led3,OUTPUT);
digitalWrite(led3, LOW);

pinMode (led4,OUTPUT);
digitalWrite(led4, LOW);

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

readBits = digitalRead(SW) << 2 | digitalRead(CLK) << 1 | digitalRead(DT);

if ((rotationBits & 0b11) != (readBits & 0b11)) {

rotationBits = rotationBits << 2 | readBits & 0b11;

// Bit Pairs Cyclic Sequence (CLK DT Pair Bits):
// 1.   2.   3.   4.   5.
// 11 | 01 | 00 | 10 | 11 for CCW
// 11 | 10 | 00 | 01 | 11 for CW
if (rotationBits == 0b01001011 || rotationBits == 0b10000111) {

  if (rotationBits == 0b01001011) {
    currentDir = "CCW";
    counter--;
  } else {
    currentDir = "CW";
    counter++;    
  }

  Serial.print("Direction: ");
  Serial.print(currentDir);
  Serial.print(" | Counter: ");
  Serial.println(counter);
}

}

if ((buttonBits & 0b001) != (readBits >> 2 & 0b001)) {

// Bit Pairs Press Release Cyclic Sequence (OFF ON Pair Bits):
// 1.    2.    3.    4.  (long press/release)
// 000 | 001 | 011 | 111 for OFF
// 111 | 110 | 100 | 000 for ON
if ((buttonBits & 0b001) == 0b000) {
  buttonBits = 0b001;
} else {
  buttonBits = 0b110;
}

// Starts counting time since button physically operated
lastActionButton = millis();

// LONG Press/Release Actions
} else if (abs(millis() - lastActionButton) > (unsigned long)LPR) {
if (buttonBits == 0b011) {
buttonBits = 0b111;
Serial.println("LONG released!");
} else if (buttonBits == 0b100) {
buttonBits = 0b000;
Serial.println("LONG pressed!");
}

// SIMPLE Press/Release Actions
} else if (abs(millis() - lastActionButton) > (unsigned long)DPR) {
if (buttonBits == 0b001) {
buttonBits = 0b011;
Serial.println("Button released!");
} else if (buttonBits == 0b110) {
buttonBits = 0b100;
Serial.println("Button pressed!");
}
}

// Put in a slight delay to help debounce the reading
delay(1);

}

i need controleed this led sometime help me ?

Do you have a question?
Code tags?

i want to controlled this led
when push botton led is on

Please define "control"

this sketch is ok for this opeation ?

ok , when rotate encoder led is on

Are you saying the LED (which one?) should be on only when the encoder is being rotated, and off when stationary?

Please remember that as a new user, your posting rate is throttled, so it is in your best interests to give as much information as you can in as few posts as possible.

quando ruoto la manopola in senso orario i led si accendono in sequenza , in senso antiorario si spengono , perĂ² facendo pressione nella manopola tengo acceso il led selezionato , con un'altra pressione lo spengo

Would it be easier if you posted in the Italian section of the forum?
They're quite strict on rules there, so make sure you introduce yourself properly.

Hello
Did you post the sketch in code tags <"/"> already?

guarda non sono pratico

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.