Arduino showing an error for some reason

Hi,
I am making a macro keyboard using my arduino Uno. Before you say anything, I know that I have to use the software which shows my arduino as a keyboard to the PC in order for them to communicate to each other.
I have been using a youtube tutorial (Arduino Project Tutorial: Macro Keyboard - YouTube) and as well as the hardware goes it is all good (I ran checks for every button and the additional lcd screen). I cannot understand, why is my code saying that releaseKey() was not defined.

For some reason this works:

wpisz lub wklej tutaj kod

/*Arduino USB Keyboard for PPT Ink Controls
//*Tool and Color
//*8-14-15 update includes different delay speeds for colors
//Code based on example found here: http://mitchtech.net/arduino-usb-hid-keyboard/

#define KEY_LEFT_CTRL 1
#define KEY_LEFT_SHIFT 2
#define KEY_F10 67

uint8_t buf[8] = {
  0
}; //Keyboard buffer bit rate

#define PIN_ERASER 2
#define PIN_LASER 4
#define PIN_HIGHLIGHTER 7
#define PIN_PEN 8
#define PIN_ARROW 12

#define PIN_PURPLE 3
#define PIN_YELLOW 5
#define PIN_GREEN 6
#define PIN_BLUE 9
#define PIN_RED 10
#define PIN_BLACK 11

int state = 1;

void setup()
{
  Serial.begin(9600);

  pinMode(PIN_ERASER, INPUT);
  pinMode(PIN_LASER, INPUT);
  pinMode(PIN_HIGHLIGHTER, INPUT);
  pinMode(PIN_PEN, INPUT);
  pinMode(PIN_ARROW, INPUT);
  pinMode(PIN_PURPLE, INPUT);
  pinMode(PIN_YELLOW, INPUT);
  pinMode(PIN_GREEN, INPUT);
  pinMode(PIN_BLUE, INPUT);
  pinMode(PIN_RED, INPUT);
  pinMode(PIN_BLACK, INPUT);
  //internal pull-ups
  digitalWrite(PIN_ERASER, 1);
  digitalWrite(PIN_LASER, 1);
  digitalWrite(PIN_HIGHLIGHTER, 1);
  digitalWrite(PIN_PEN, 1);
  digitalWrite(PIN_ARROW, 1);
  digitalWrite(PIN_PURPLE, 1);
  digitalWrite(PIN_YELLOW, 1);
  digitalWrite(PIN_GREEN, 1);
  digitalWrite(PIN_BLUE, 1);
  digitalWrite(PIN_RED, 1);
  digitalWrite(PIN_BLACK, 1);
  
  delay(200);
}

void loop()
{
   state = digitalRead(PIN_ARROW);
  if (state != 1) {
    buf[0] = KEY_LEFT_CTRL;   // Ctrl
    buf[2] = 4;    // Letter A
    // buf[2] = 123;    // Cut key: Less portable
    Serial.write(buf, 8); // Send keypress
    releaseKey();
}
  state = digitalRead(PIN_PEN);
  if (state != 1) {
    buf[0] = KEY_LEFT_CTRL;   // Ctrl
    buf[2] = 19;    // Letter P
    // buf[2] = 124;    // Copy key: Less portable
    Serial.write(buf, 8); // Send keypress
    releaseKey();
  } 

  state = digitalRead(PIN_HIGHLIGHTER);
  if (state != 1) {
    buf[0] = KEY_LEFT_CTRL;   // Ctrl
    buf[2] = 12;    // Letter I
    // buf[2] = 125;    // Paste key: Less portable
    Serial.write(buf, 8); // Send keypress
    releaseKey();
  } 

    state = digitalRead(PIN_LASER);
  if (state != 1) {
    buf[0] = KEY_LEFT_CTRL;   // Ctrl
    buf[2] = 15;    // Letter L
    // buf[2] = 125;    // Paste key: Less portable
    Serial.write(buf, 8); // Send keypress
    releaseKey();
  }

    state = digitalRead(PIN_ERASER);
  if (state != 1) {
    buf[0] = KEY_LEFT_CTRL;   // Ctrl
    buf[2] = 8;    // Letter E
    // buf[2] = 125;    // Paste key: Less portable
    Serial.write(buf, 8); // Send keypress
    releaseKey();
  }
  
state = digitalRead(PIN_BLACK);
  if (state != 1) {
    {
    buf[0] = KEY_LEFT_SHIFT; 
    buf[2] = KEY_F10; // menu
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 18; // o
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 6; // c
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 79; // arrow
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 40; //enter
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  }
  state = digitalRead(PIN_RED);
  if (state != 1) {
    {
    buf[0] = KEY_LEFT_SHIFT; 
    buf[2] = KEY_F10; // menu
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 18; // o
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 6; // c
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 79; // arrow 1
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 2
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 3
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 40; //enter
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  }
    state = digitalRead(PIN_BLUE);
  if (state != 1) {
    {
    buf[0] = KEY_LEFT_SHIFT; 
    buf[2] = KEY_F10; // menu
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 18; // o
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 6; // c
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 79; // arrow 1
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 2
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 3
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 4
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 5
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 6
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 7
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 8
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 9
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 40; //enter
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  }
    state = digitalRead(PIN_GREEN);
  if (state != 1) {
    {
    buf[0] = KEY_LEFT_SHIFT; 
    buf[2] = KEY_F10; // menu
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 18; // o
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 6; // c
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 79; // arrow 1
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 2
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 3
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 4
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 5
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 6
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 7
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 40; //enter
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  }
    state = digitalRead(PIN_YELLOW);
  if (state != 1) {
    {
    buf[0] = KEY_LEFT_SHIFT; 
    buf[2] = KEY_F10; // menu
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 18; // o
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 6; // c
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 79; // arrow 1
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 2
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 3
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 4
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 5
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 40; //enter
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  }
    state = digitalRead(PIN_PURPLE);
  if (state != 1) {
    {
    buf[0] = KEY_LEFT_SHIFT; 
    buf[2] = KEY_F10; // menu
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 18; // o
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 6; // c
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 79; // arrow 1
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 2
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 3
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 4
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 5
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 6
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 7
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 8
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 9
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 10
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
   {
    buf[2] = 79; // arrow 11
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  {
    buf[2] = 40; //enter
    Serial.write(buf, 8); // Send keypress
    releaseColor();
  }
  }
}

void releaseKey() 
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key  
  delay(200);
}

void releaseColor() //seperate releases b/c need a faster response rate to reduce sensation of lag when going thru color menu
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); //release
  delay(50);
}

And this doesn't:



#define CTRL 224
#define SHIFT 225
#define ALT 226
uint8_t buf[8] = {
  0};

  #define PIN_YELLOW 6
  #define PIN_GREEN 5
  #define PIN_BLUE 4
  #define PIN_PURPLE 3
  #define PIN_BROWN 2

  
  int state = 1;
  
  void setup() {
    Serial.begin(9600);

    pinMode(PIN_YELLOW, INPUT);
    pinMode(PIN_GREEN, INPUT);
    pinMode(PIN_BLUE, INPUT);
    pinMode(PIN_PURPLE, INPUT);
    pinMode(PIN_BROWN, INPUT);
    digitalWrite(PIN_BROWN, 1);
  digitalWrite(PIN_YELLOW, 1);
  digitalWrite(PIN_GREEN, 1);
  digitalWrite(PIN_BLUE, 1);
  digitalWrite(PIN_PURPLE, 1);

    delay(200);
    
}

void loop() {
  state = digitalRead(PIN_YELLOW);
  if (state !=1) {
    {
    buf[0] = ALT;
    buf[2] = CTRL;
    buf[4] = 30;
    Serial.write(buf, 8);
    releaseKey();
    }
  }
  void releaseKey() 
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key  
  delay(200);
}

}

The idea is to have 5 keys, which all have a different shortcut assigned. Each starting with ctrl+alt+shift and ending with a number from 1 to 5. I know that the code isn't finished, but what's the point if the most important part isn't going to work anyway?
I am new to all of this, and I beg you for help.
Thanks in advance

You can not use the Uno as a HID. The processor doesn't support it.

Why does it work in the tutorial then?

Did you program the 16U2? ... and... do you have a genuine UNO, or a clone? Okay never mind, I found by plowing through your post that you have some working version.

Did you make incremental changes to the supplied program, or step by step and testing each step?

Post your code, in code tags
Post ALL the error messages you see.

Preferably, use the IDE's autoformat tool before posting the code.

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

1 Like

Please edit your post to add code tags.

1 Like
#define CTRL 224
#define SHIFT 225
#define ALT 226
uint8_t buf[8] = {
  0};

  #define PIN_YELLOW 6
  #define PIN_GREEN 5
  #define PIN_BLUE 4
  #define PIN_PURPLE 3
  #define PIN_BROWN 2

  
  int state = 1;
  
  void setup() {
    Serial.begin(9600);

    pinMode(PIN_YELLOW, INPUT);
    pinMode(PIN_GREEN, INPUT);
    pinMode(PIN_BLUE, INPUT);
    pinMode(PIN_PURPLE, INPUT);
    pinMode(PIN_BROWN, INPUT);
    digitalWrite(PIN_BROWN, 1);
  digitalWrite(PIN_YELLOW, 1);
  digitalWrite(PIN_GREEN, 1);
  digitalWrite(PIN_BLUE, 1);
  digitalWrite(PIN_PURPLE, 1);

    delay(200);
    
}

void loop() {
  state = digitalRead(PIN_YELLOW);
  if (state !=1) {
    {
    buf[0] = ALT;
    buf[2] = CTRL;
    buf[4] = 30;
    Serial.write(buf, 8);
    releaseKey();
    }
  }
  void releaseKey() 
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key  
  delay(200);
}

}

Try_on_arduino_uno.ino: In function 'void loop()':

Try_on_arduino_uno:42:5: error: 'releaseKey' was not declared in this scope

     releaseKey();

     ^~~~~~~~~~

Try_on_arduino_uno:46:1: error: a function-definition is not allowed here before '{' token

 {

 ^

exit status 1

'releaseKey' was not declared in this scope


Look where your loop function ends.

You have another function definition embedded in loop.

Two opening { after the if.
Lose one of them.

Do an auto format, it will clue you in to your apparent syntax error by indenting improperly, you have a problem with badly formed {} code blocks, or code in a forbidden place.

how do you do that?

Position the cursor on one of the { and press "delete"

I was asking about the auto format. Thanks it worked. Dysleksja

But I still don't understannd what these buf[0]/[2]/[4] mean. Can anybody explain?

They are elements of an array called buf

1 Like

I was gonna ask what do you mean by array, but thats probably a long answer and I don't want to take all day for you. Do I understand correctly that depending on how many keys i want I just have to do:

buf[0] = x
buf[2] = y
buf[4] = z
buf[6] = q
etc.?

An array is just a row of pigeonholes.
Each pigeonhole has a number, its index or subcript, which represents how far from the start of the row it is

The first subscript is zero.

You can put any value of the same type as the array into any pigeonhole.
If buf is a char array, you can put any char value into any of the array's elements.

1 Like

So what I wrote above is correct. Right?

I don't understand what you wrote above, so I can't say if it is right or wrong.

For example, I don't know why you're using only even-numbered subscripts.

So in the tutorial she only used two key pressed at the same time. for example:
buf[0] = ALT;
buf[2] = CTRL;
Now if I want to add 2 more keys pressed at the same time should I just do something like this:
buf[4] = SHIFT
buf[6] = SPACE

Answer to the question above:
I thought that it was the only correct way, because she did that.