Right now I´m working on a hotkey Keyboard for my son. Like a lot other Kids he is doing homeschooling an has some web conferences with the teacher. To make his life easier I´m triying to make a hotkey Keyboard with 3 Toggle Switches and on Push Button. (It looks like a Buttons in a racing car, this is the only reason for this setup). Target is to Mute/Unmute, open the school website or close all programs etc with the buttons. For all buttons I´m using a pull up logic. (Gnd with 10K Resistor + Digital Pin on one end, +5 on the other.)
The Sketch is running so far, but when I combine the code for the toggle switch and the push switch, it doesn´t work anymore. Could anybody give me a hint whats wrong?
< #include <Keyboard.h>
//Keyboards: GUI=Command, CTRL=Control
Please follow the advice on posting code given in posting code
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless
aarg:
While you're busy fixing the code format in your post, consider the obvious reason why it doesn't work:
while (digitalRead(Starter) == LOW) {
// do nothing until Starter goes HIGH
delay(500);
}
with emphasis on the "do nothing". :)
Good hint. Wenn I press "Starter" I can press ACC1 and something happens. This proofs you are right :).
So I need something different to "while..." hmmm.
Ok, here´s the update:
The latest sketch works great so far, thanks for your help.
Two things are still bothering me:
For the Switch ACC2 (A toggle Switch) I don´t that the command comes twice. Target ist the only when the toggle switch gets turned on, it should send a keyboard command. So: Turn Switch on-> Keyboard command x , turning off-> nothing -> turning on-> keyboard command x. Command x should only be send once, otherwise the command would be send the whole timer while the switch is on which would break the computer. The example sketches which I´ve found compare the states but don´t differ between state LOW and HIGH where I want difference things to happen.
For the MainSwitch, I´ve tested "Keyboard.press(KEY_RETURN);" and it turns out that the return command only comes the first time when I turn on the toggle switch. Turning off and on would write the "Keyboard.println" but ignore the Return command. -> I fixed this with "Keyboard.releaseAll();" -> is this a good way?
#include <Keyboard.h>
//Keyboards: GUI=Command, CTRL=Control
const byte MainSwitch = 0;
const byte Starter = 2;
const byte ACC1 = 4;
const byte ACC2 = 6;
void setup() {
// put your setup code here, to run once:
Keyboard.begin(); // setup keyboard
pinMode(MainSwitch, INPUT_PULLUP);//MainSwitch
pinMode(Starter, INPUT_PULLUP);//Starter
pinMode(ACC1, INPUT_PULLUP);//Acc1
pinMode(ACC2, INPUT_PULLUP);//Acc2
}
void loop() {
//Starter Button - Mute/Unmute
int StarterVal = digitalRead(Starter);
if (StarterVal == HIGH) {
//Keyboard.press(KEY_LEFT_CTRL);
//Keyboard.press(KEY_LEFT_ALT);
//Keyboard.press('m');
Keyboard.println("Hello Starter");
delay(200);
}
else{
delay(50);
}
// ACC1
static byte lastSwitchState1 = LOW;
static unsigned long lastSwitchStateTime1 = millis();
byte currentSwitchState1 = digitalRead(ACC1);
unsigned long currentTime1 = millis();
// Looks for state change while filtering out contact bounces
if (currentSwitchState1 != lastSwitchState1 && currentTime1 - lastSwitchStateTime1 > 10UL) {
lastSwitchState1 = currentSwitchState1;
lastSwitchStateTime1 = currentTime1;
Keyboard.println("Hello ACC1");
}
// ACC2
static byte lastSwitchState2 = LOW;
static unsigned long lastSwitchStateTime2 = millis();
byte currentSwitchState2 = digitalRead(ACC2);
unsigned long currentTime2 = millis();
// Looks for state change while filtering out contact bounces
if (currentSwitchState2 != lastSwitchState2 && currentTime2 - lastSwitchStateTime2 > 10UL) {
lastSwitchState2 = currentSwitchState2;
lastSwitchStateTime2 = currentTime2;
Keyboard.println("Hello ACC2");
delay(100);
}
// MainSwitch
static byte lastSwitchState3 = LOW;
static unsigned long lastSwitchStateTime3 = millis();
byte currentSwitchState3 = digitalRead(MainSwitch);
unsigned long currentTime3 = millis();
// Looks for state change while filtering out contact bounces
if (currentSwitchState3 != lastSwitchState3 && currentTime3 - lastSwitchStateTime3 > 10UL) {
lastSwitchState3 = currentSwitchState3;
lastSwitchStateTime3 = currentTime3;
Keyboard.println(" Hello Mainswitch ");
delay(500);
Keyboard.press(KEY_RETURN);
delay(500);
Keyboard.println(" How to proceed ");
delay(500);
}
}