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

Someone took their foot off the switch between those two reads.

I'm wondering how the footswitch is wired. My bet is on a floating input when it is released so it still reads above the 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();
  }
}

I guess OP isn't wanting help after all or they'd answer this. What a waste of time.

@Delta_G

@Delta_G

  • Wires need soldering

  • Use pinMode(footSwitchPin, INPUT_PULLUP);


  • As mentioned by @Delta_G use digitalREAD

How are those wires connected? Are they just shoved through the holes? That won't make good contact. You need to solder them.

Why are you using analogRead and not digitalRead? The switch should give you either on or off, not anything in between.

When the footswitch is not pressed, black and white are connected inside so the pin A0 sees 0 volts.

What do you think is connected when the footswitch is not pressed? What should the pin read in that case? It looks like it is floating and can read anything it wants.

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

But it doesn't does it? It doesn't stop when you take your foot off does it? That is because your pin is floating.

I'm running out of ways to say that.

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();
  }
}

Right. Because the switch is floating. You really don't believe me? But you fixed it and now it works. See, I was right all along.

Just because it worked for one thing doesn't mean you have it 100% right.