Why does it not work?

#include <Keyboard.h>
void setup() {
  Keyboard.begin();
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, INPUT);

}

void loop() {
  if (digitalRead (2) == HIGH) {

    Keyboard.press(KEY_LEFT_GUI);
    Keyboard.press('l');
    Keyboard.releaseAll();
    delay(1000 * 0.4);

  }
  //
  if (digitalRead (3) == HIGH) {
    Keyboard.press(KEY_LEFT_GUI);
    Keyboard.press(KEY_LEFT_SHIFT);
    Keyboard.press('s');
    Keyboard.releaseAll();
    delay(1000 * 0.4);

  }

  //
}
//
if (digitalRead (5) == HIGH) {
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('t');
  Keyboard.releaseAll();
  delay(1000 * 0.4);


}
//
if (digitalRead (6) == HIGH) {
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('m');
  Keyboard.releaseAll();
  delay(1000 * 0.4);


}
//
if (digitalRead (7) == HIGH) {
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press('m');
  Keyboard.releaseAll();
  delay(1000 * 0.4);


}


}

From inside the IDE, press Ctrl-T to properly indent the code and probabily you'll get it by yourself.

okay, I'll try.

it says no change is needed for the automatic formatting

code runs in loop() when the code is encased in loop()'s {}'s. code outside of the confines of loop() brackets, in this case, will not run and will produce errors.

which means this code


//
if (digitalRead (5) == HIGH) {
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('t');
  Keyboard.releaseAll();
  delay(1000 * 0.4);


}
//
if (digitalRead (6) == HIGH) {
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('m');
  Keyboard.releaseAll();
  delay(1000 * 0.4);


}
//
if (digitalRead (7) == HIGH) {
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press('m');
  Keyboard.releaseAll();
  delay(1000 * 0.4);


}


}

is the issue. because it's not in any function.

Auto-format clearly shows this issue.


  }

  //
}  // <- EXTRA '}' ENDS loop().  REMOVE IT.
//
if (digitalRead (5) == HIGH)
{
  //
} <----- remove this brace
//
if (digitalRead (5) == HIGH) {

Remove this brace and then it will compile.

Edit beaten to the punch.

1 Like

Have a look at row 36. You have a closed curly bracket closing the loop() function, so all the subsequent "if()"s start at first column. It should immediately say you there's something wrong if they aren't indented like the first two, don'you think? So they're formally correct (thus no further need to indent them) but wrong. Remove line 36.

PS: and always carefully read and post the whole error messages, not just a picture

yeeeeeeeeeeeeeeeeeees it works

A bit pompous don't you think?

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