Homeschooling Keyboard - Push and Toggle Switches

Dear All,

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

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() {
// put your main code here, to run repeatedly:

//Starter Button - Mute/Unmute
while (digitalRead(Starter) == LOW) {
// do nothing until Starter goes HIGH
delay(500);
}
delay(500);

//Keyboard.press(KEY_LEFT_GUI);
//Keyboard.press(KEY_LEFT_SHIFT);
// Keyboard.press('m');
Keyboard.println("Hello Starter");
delay(100);
Keyboard.releaseAll();
// wait for new window to open:
delay(1000);

// ACC1

static byte lastSwitchState = LOW;
static unsigned long lastSwitchStateTime = millis();

byte currentSwitchState = digitalRead(ACC1);
unsigned long currentTime = millis();

// Looks for state change while filtering out contact bounces
if (currentSwitchState != lastSwitchState && currentTime - lastSwitchStateTime > 10UL) {
lastSwitchState = currentSwitchState;
lastSwitchStateTime = currentTime;
Keyboard.println("Hello ACC1");

}
}
/>

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

Could anybody give me a hint whats wrong?

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". :slight_smile:

#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() {
 // put your main code here, to run repeatedly:


 //Starter Button - Mute/Unmute
   while (digitalRead(Starter) == LOW) {
    // do nothing until Starter goes HIGH
    delay(500);
  }
  delay(500);
  
//Keyboard.press(KEY_LEFT_GUI);
//Keyboard.press(KEY_LEFT_SHIFT);
 // Keyboard.press('m');
  Keyboard.println("Hello Starter");
  delay(100);
  Keyboard.releaseAll();
  // wait for new window to open:
  delay(1000);


// ACC1

static byte lastSwitchState = LOW;
    static unsigned long lastSwitchStateTime = millis();

    byte currentSwitchState = digitalRead(ACC1);
    unsigned long currentTime = millis();


    // Looks for state change while filtering out contact bounces
    if (currentSwitchState != lastSwitchState && currentTime - lastSwitchStateTime > 10UL) {
        lastSwitchState = currentSwitchState;
        lastSwitchStateTime = currentTime;
        Keyboard.println("Hello ACC1");

 }
 }

Arduino foresaw your problem, and provided example sketches with the IDE for reading button switches successfully.

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.

How about that?

int StarterVal = digitalRead(Starter);

if (StarterVal == HIGH) {
  Keyboard.println("Hello Starter");
  delay(200);
    }
  else{
    delay(50);
  }

I don't know, you didn't post a complete sketch, without the full context of that code I have no idea.

Fair enough :slight_smile:

#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');
  delay(200);
    }
  else{
    delay(50);
  }
  
// ACC1

static byte lastSwitchState = LOW;
    static unsigned long lastSwitchStateTime = millis();

    byte currentSwitchState = digitalRead(ACC1);
    unsigned long currentTime = millis();


    // Looks for state change while filtering out contact bounces
    if (currentSwitchState != lastSwitchState && currentTime - lastSwitchStateTime > 10UL) {
        lastSwitchState = currentSwitchState;
        lastSwitchStateTime = currentTime;
        Keyboard.println("Hello ACC1");

 }
 }

You didn't specify how that behaves. What works/ doesn't work? Please bring us up to date.

Ok, here´s the update:
The latest sketch works great so far, thanks for your help.
Two things are still bothering me:

  1. 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.

  2. 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);

 }
 }

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