How to stop a macro

Hello, I am coding a macro. In short, the macro presses the 9w0w keys on the keyboard. But when I stop pressing the switch, it does not finish the loop. In other words, 9w0w is written like this every time. What should happen is that as long as I keep it pressed, it will be 9w0w9w0w9w0w, but when I stop pressing it, it may end like 9w0. Can you help me with this?

My Codes :

#include <Keyboard.h>

const int footSwitchPin = A0;
const int threshold = 512;

void setup() {
  pinMode(footSwitchPin, INPUT);
  Keyboard.begin();      
  Serial.begin(9600);
}

void loop() {
  int footSwitchState = analogRead(footSwitchPin);

  if (footSwitchState > threshold) {

    Keyboard.press('9');
    delay(435);
    Keyboard.release('9');
    
    if (analogRead(footSwitchPin) <= threshold) {
      Keyboard.releaseAll(); 
      return;
    }

    Keyboard.press('w');
    delay(30);
    Keyboard.release('w');
    
    if (analogRead(footSwitchPin) <= threshold) {
      Keyboard.releaseAll();
      return;
    }

    Keyboard.press('0');
    delay(410);
    Keyboard.release('0');
    
    if (analogRead(footSwitchPin) <= threshold) {
      Keyboard.releaseAll();
      return;
    }

    Keyboard.press('w');
    delay(30);
    Keyboard.release('w');
    
    if (analogRead(footSwitchPin) <= threshold) {
      Keyboard.releaseAll();
      return;
    }

  } else {
    Keyboard.releaseAll();
  }
}

int footSwitchState = analogRead(footSwitchPin);
 
  if (footSwitchState > threshold) {

    Keyboard.press('9');
    delay(435);
    Keyboard.release('9');
    
    if (analogRead(footSwitchPin) <= threshold) {
      Keyboard.releaseAll(); 
      return;
    }

  • How can this happen ? :thinking:
    footSwitchState > threshold
    analogRead(footSwitchPin) <= threshold
  • And of course this will be rock solid. :woozy_face:

  • Awe, didn’t read this delay(435); must have been the 435 number that I ignored.
    I trained my brain (what’s left of it) to ignore delay(. . .)

Yes, I'm new to this. So I'm trying to learn.

I missed what you mentioned because I was struggling with the problem so much. I updated my code. I still have the problem. The switch should work when pressed and stay where it is when I release it.

#include <Keyboard.h>

const int footSwitchPin = A0;
const int threshold = 512;

void setup() {
  pinMode(footSwitchPin, INPUT);
  Keyboard.begin();      
  Serial.begin(9600);
}

void loop() {
  int footSwitchState = analogRead(footSwitchPin);

  if (footSwitchState > threshold) {

    Keyboard.press('9');
    delay(435);
    Keyboard.release('9');

    Keyboard.press('w');
    delay(30);
    Keyboard.release('w');

    Keyboard.press('0');
    delay(410);
    Keyboard.release('0');

    Keyboard.press('w');
    delay(30);
    Keyboard.release('w');

  } else {
    Keyboard.releaseAll();
  }
}

@Delta_G

@Delta_G

  • Wires need soldering

  • Use pinMode(footSwitchPin, INPUT_PULLUP);


  • As mentioned by @Delta_G use digitalREAD

But everything works on the hardware side. When I run the codes I wrote above, when I press the switch, it writes 9w0w to the screen. But when I take my foot off the switch, it completes the loop. I want it to stop as soon as I take my foot off.
@Delta_G @LarryD

It was already working as I said. But I uploaded new codes to it and I am experiencing the problem I mentioned with these codes.

@Delta_G @LarryD

  • If this is a dry N.O. contact, wire as S3 below.
    Press should give LOW, release gives HIGH.

Thank you. I thought about what you said and now it works without any problems. When I press it, the 9w0w macro works and when I take my foot off it stops. (So it doesn't force the loop to complete.)

My Last Code :

#include <Keyboard.h>

const int footSwitchPin = A0;
const int threshold = LOW;

void setup() {
  pinMode(footSwitchPin, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  int footSwitchState = digitalRead(footSwitchPin);

  if (footSwitchState == HIGH) {
    
    Keyboard.press('9');
    delay(435);
    Keyboard.release('9');
    
    if (digitalRead(footSwitchPin) == LOW) {
      Keyboard.releaseAll();
      return;
    }

    Keyboard.press('w');
    delay(30);
    Keyboard.release('w');
    
    if (digitalRead(footSwitchPin) == LOW) {
      Keyboard.releaseAll();
      return;
    }

    Keyboard.press('0');
    delay(410);
    Keyboard.release('0');
    
    if (digitalRead(footSwitchPin) == LOW) {
      Keyboard.releaseAll();
      return;
    }

    Keyboard.press('w');
    delay(30);
    Keyboard.release('w');
    
    if (digitalRead(footSwitchPin) == LOW) {
      Keyboard.releaseAll();
      return;
    }
    
  } else {
    Keyboard.releaseAll();
  }
}

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