Hey, I am currently programming a macro pad with 5 buttons and a rotary encoder. The encoder should be able to change the volume up and down, as well as play/pause on click and next track when held. I am pretty new to coding so I'm a bit confused by the error. I am also using an Arduino Pro Micro
Here is my code, and the error is at the end:
#include <HID-Project.h>
#include <HID-Settings.h>
#include <Encoder.h>
#include <Bounce2.h>
// Rotary Encoder Inputs
#define inputCLK 16
#define inputDT 14
#define inputSW 15
int currentStateCLK;
int previousStateCLK;
//Encoder
int SW = 15;
int DT = 14;
int CLK = 16;
//Set up the button grid
const int numButtons = 6;
const int buttonPins[numButtons] = {9,8,7,6,5,15}; //defining the pins in an order from right to left
int state = 0; //Set the current state
//Set up all the buttons as bounce objects
Bounce buttons[] = {Bounce(buttonPins[0],10),Bounce(buttonPins[1],10),Bounce(buttonPins[2],10),Bounce(buttonPins[3],10),Bounce(buttonPins[4],10),Bounce(buttonPins[5],10)};
void setup() {
// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
pinMode(inputSW, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin (9600);
Consumer.begin();
Keyboard.begin();
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
Serial.begin(9600);
Keyboard.begin(); //Start the Keyboard object
for(int i = 0; i < numButtons; i++){
pinMode(buttonPins[i], INPUT_PULLUP);
}
void loop() {
//check all buttons
for(int j = 0; j < numButtons; j++){
if(buttons[j].update()){
if(buttons[j].fallingEdge()){
//Serial.write("Button");
//use the current state and the button number to find the command needed and send it.
switch (state) {
case 0: //Layout 1
switch (j) {
case 0: //F6
Keyboard.press(KEY_F1);
delay(100);
Keyboard.releaseAll();
break;
case 1: //
Keyboard.press(KEY_F2);
delay(100);
Keyboard.releaseAll();
break;
case 2: //
Keyboard.press(KEY_F3);
delay(100);
Keyboard.releaseAll();
break;
case 3: //
Keyboard.press(KEY_F4);
delay(100);
Keyboard.releaseAll();
break;
case 4: //
Keyboard.press(KEY_F5);
delay(100);
Keyboard.releaseAll();
break;
case 5: //
Keyboard.press(KEY_ESC);
delay(100);
Keyboard.releaseAll();
break;
}
}
//check the encoder button
if(encoderButton.update()) {
if(encoderButton.fallingEdge()) {
int fall = millis();
while(!encoderButton.update()){}
if(encoderButton.risingEdge()){
int rise = millis();
//Serial.println(rise - fall);
if(rise - fall > timeLimit){
Consumer.write(MEDIA_NEXT);
Serial.print("Next");
} else {
Consumer.write(MEDIA_PLAY_PAUSE);
Serial.print("Play/Pause");
}
}
Keyboard.releaseAll();
}
}
// Read the current state of inputCLK
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) {
rotateRight();
} else {
rotateLeft();
}
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
void rotateRight() {
// Increase the volume.
Consumer.write(MEDIA_VOLUME_UP);
}
void rotateLeft() {
// Decrease the volume.
Consumer.write(MEDIA_VOLUME_DOWN);
}
}
}
}
The error message showing up when I try verifying:
Arduino: 1.8.13 (Windows 10), Board: "SparkFun Pro Micro, ATmega32U4 (5V, 16 MHz)"
In file included from c:\users\190240\documents\arduino\libraries\hid-project\src\hid-apis\KeyboardAPI.h:29:0,
from c:\users\190240\documents\arduino\libraries\hid-project\src\hid-apis\defaultkeyboardapi.h:27,
from C:\Users\190240\Documents\Arduino\libraries\HID-Project\src/SingleReport/BootKeyboard.h:30,
from C:\Users\190240\Documents\Arduino\libraries\HID-Project\src/HID-Project.h:50,
from C:\Users\190240\Desktop\Electronic Tech Macropad\Test_Code_1\Test_Code_1.ino:1:
c:\users\190240\documents\arduino\libraries\hid-project\src\keyboardlayouts\improvedkeylayouts.h:54:21: note: #pragma message: Using default ASCII layout for keyboard modules
#pragma message "Using default ASCII layout for keyboard modules"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\190240\Desktop\Electronic Tech Macropad\Test_Code_1\Test_Code_1.ino: In function 'void setup()':
Test_Code_1:48:13: error: a function-definition is not allowed here before '{' token
void loop() {
^
Test_Code_1:143:1: error: expected '}' at end of input
}
^
exit status 1
a function-definition is not allowed here before '{' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Any help would be appreciated!