Im having trubble finishing this code

So i wrote this code with some help, what i cant figure out is how to make, say "x" be written 3 times with 1 click of a single button? Im using adrino Leonardo with this and toggle switches. If anyone knows how to add something to make it write 3 letters with 1 button(I only want x to be written 3 times, everything else i want to be written once, Thanks.

Heres the code i used

#include "Keyboard.h"

boolean states[] = {false, false, false, false, false, false, false, false, false, false, false, false, 
false, false};
char hotkeysOn[] = {'g', 'c', 'q', 'a', 'b', 'w', 'd', 'l', 's', 'j', 'k', 'm', 'x', 'z'};
char hotkeysOff[] = {'g', 'c', 'q', 'a', 'b', 'w', 'd', 'l', 's', 'j', 'k', 'n', 'x', 'z'};

void setup() {
for (int i = 0; i <= 13; i++) {
    pinMode(i, INPUT);
}
Keyboard.begin();
Serial.begin(9600);
}

void loop() {
for (int i = 0; i <= 0; i++) {
    if (digitalRead(i) == HIGH && !states[i]) {
        states[i] = !states[i];
        Keyboard.write(hotkeysOn[i]);

    }
    else if (digitalRead(i) == LOW && states[i]) {
        states[i] = !states[i];
        Keyboard.write(hotkeysOff[i]);
    }
}
delay(10);

}

should this be the same as in setup()

do you need to worry about switch bounce?

where do you test for 'x'?

Please enclose the WHOLE code in the code tag (everything after "Heres the code i used"), or it's hard to check it out.

When is the assignment due??

It a personal project @outbackhut

1 Like

How do i do that

"Its a letter that is printed when i put a switch into the on position and i want this letter to be printed 3 times instead of 1 time."

something like this maybe...
(Compiles, NOT tested!)

#include "Keyboard.h"

boolean states[] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false};
char hotkeysOn[] = {'g', 'c', 'q', 'a', 'b', 'w', 'd', 'l', 's', 'j', 'k', 'm', 'x', 'z'};
char hotkeysOff[] = {'g', 'c', 'q', 'a', 'b', 'w', 'd', 'l', 's', 'j', 'k', 'n', 'x', 'z'};

void setup() {
  for (int i = 0; i <= 13; i++) {
    pinMode(i, INPUT);
  }
  Keyboard.begin();
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i <= 13; i++) {
    if (digitalRead(i) == HIGH && !states[i]) {
      states[i] = !states[i];
      Keyboard.write(hotkeysOn[i]);
      //write 'x' 2 more times if button is ON
      if(i==12){
        Keyboard.write(hotkeysOn[i]);
        Keyboard.write(hotkeysOn[i]);
      }

    }
    else if (digitalRead(i) == LOW && states[i]) {
      states[i] = !states[i];
      Keyboard.write(hotkeysOff[i]);
    }
  }
  delay(10);
}

hope that helps...

PS: plz study more on arrays! :wink:

I think this should work, I think i get what I have to do if it doesn't work. I have a lot to learn when i comes to C++ lol, im currently still learning a lot, Thanks.

Hi, @jakeepoo
Welcome to the forum.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

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