Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
raikanrule
Published

Encoder Volume Control

Encoder Volume control with Arduino LEONARDO

BeginnerProtip747
Encoder Volume Control

Things used in this project

Hardware components

Arduino Leonardo
Arduino Leonardo
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Easy cabling

short cut try

Code

Encoder Volume IDE code

INI
#include <HID-Project.h>
#include <HID-Settings.h>

#define REVERSED false //if your controller is reversed change it to true

#define CLK 4
#define DT 5
#define SW 3

int volume = 0;
int currentStateCLK;
int lastStateCLK;
unsigned long lastButtonPress = 0;

void setup() {
	
	pinMode(CLK,INPUT);
	pinMode(DT,INPUT);
	pinMode(SW, INPUT_PULLUP);


	lastStateCLK = digitalRead(CLK); //CLK value
}

void loop() {
	
	currentStateCLK = digitalRead(CLK); //CLK current value
  
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){  //CLK change && CLK only 1state change

		if (digitalRead(DT) != currentStateCLK) { //Encoder CCW Rotation
      Consumer.write(MEDIA_VOLUME_UP); //Volume Up
      volume++;
      delay(2);

		} else {			// Encoder CW Rotation
      Consumer.write(MEDIA_VOLUME_DOWN); //Volume Down
      volume--
      ;
      delay(2);
		}

	}

	lastStateCLK = currentStateCLK; // Last CLK Value

		int btnState = digitalRead(SW); // Button Value

		if (btnState == LOW) { // Switch pushed

    if (millis() - lastButtonPress > 50) { // Over 50ms
    Consumer.write(MEDIA_VOLUME_MUTE);
		}
    lastButtonPress = millis();
	}

	delay(1); // Push swithc delay
}

Credits

raikanrule
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.