I'm trying to build a multi media controller for vol and custom buttons functions and menu navigations open apps or jump from an app to another app with a rotary encoder with an Arduino micro with ATmega32u4
already got the vol work and 4 custom buttons works too im trying find the code sketch or somebody can guide on the right direction how to make the rotary encoder move from one app to another app
I'm really new programing i apologize if i bother somebody with my newbie question i attach the code that i used in the best of my knowledge thanks
I edited my post to make it right
[code]
#include <ClickEncoder.h>
#include <TimerOne.h>
#include <HID-Project.h>
#define ROTARY_D 14
#define ROTARY_E 10
#define ROTARY_F 7
int buttonPina = 5;
int buttonPinb = 6;
int buttonPinc = 3;
int buttonPind = 2;
ClickEncoder *encoder; // variable representing the rotary encoder
int16_t last, value; // variables for current and last rotation value
void timerIsr() {
encoder->service();
}
void setup() {
Serial.begin(9600); // Opens the serial connection used for communication with the PC.
Consumer.begin(); // Initializes the media keyboard
Keyboard.begin();
//Serial.write("Starting...\n")
encoder = new ClickEncoder(ROTARY_D, ROTARY_E, ROTARY_F ); // Initializes the rotary encoder with the mentioned pins
Timer1.initialize(1000); // Initializes the timer, which the rotary encoder uses to detect rotation
Timer1.attachInterrupt(timerIsr);
last = -1;
pinMode(ROTARY_D, INPUT_PULLUP);
pinMode(ROTARY_E, INPUT_PULLUP);
pinMode(ROTARY_F, INPUT_PULLUP);
pinMode(buttonPina, INPUT_PULLUP);
pinMode(buttonPinb, INPUT_PULLUP);
pinMode(buttonPinc, INPUT_PULLUP);
pinMode(buttonPind, INPUT_PULLUP);
}
void loop() {
value += encoder->getValue();
// This part of the code is responsible for the actions when you rotate the encoder
if (value != last) { // New value is different than the last one, that means to encoder was rotated
if (last < value) // Detecting the direction of rotation
Keyboard.press(KEY_MINUS); // Replace this line to have a different function when rotating counter-clockwise
Keyboard.releaseAll();
delay(500);
}
else {
Keyboard.press(KEY_LEFT_SHIFT); // Replace this line to have a different function when rotating clockwise
Keyboard.press('+');
Keyboard.releaseAll();
delay(1000);
last = value; // Refreshing the "last" varible for the next loop with the current value
Serial.print("Encoder Value: "); // Text output of the rotation value used manily for debugging (open Tools - Serial Monitor to see it)
Serial.println(value);
}
// This next part handles the rotary encoder BUTTON
ClickEncoder::Button b = encoder->getButton(); // Asking the button for it's current state
if (b != ClickEncoder::Open) { // If the button is unpressed, we'll skip to the end of this if block
//Serial.print("Button: ");
//#define VERBOSECASE(label) case label: Serial.println(#label); break;
switch (b) {
case ClickEncoder::Clicked: // Button was clicked once
Consumer.write(MEDIA_PLAY_PAUSE); // Replace this line to have a different function when clicking button once
break;
case ClickEncoder::DoubleClicked: // Button was double clicked
Consumer.write(MEDIA_NEXT); // Replace this line to have a different function when double-clicking
break; {
}
delay(10); // Wait 10 milliseconds, we definitely don't need to detect the rotary encoder any faster than that
// The end of the loop() function, it will start again from the beggining (the begginging of the loop function, not the whole document)
}
if (digitalRead(buttonPina))
{
Keyboard.write(KEY_F7);
} else {
Serial.println("A");
delay(500);
}
}
if (digitalRead(buttonPinb))
{
Keyboard.write(KEY_F7);
} else {
Serial.println("B");
delay(500);
}
if (digitalRead(buttonPinc))
{
Keyboard.write(KEY_F7);
} else {
Serial.println("C");
delay(500);
}
if (digitalRead(buttonPind))
{
Keyboard.write(KEY_F7);
} else {
Serial.println("D");
delay(500);
}
}
[/code]