Problem with script

Not sure if this is the right section but I am having a problem with my script on my micro. It is a simple project where when a ground signal is sensed it hits the f12 key on a laptop. Instead it is hitting space bar non stop no matter what as soon as the code is uploaded.

// sets Emergency Key to F12 char Emergency = KEY_F12;

void setup() { // make pin 2 an input and turn on the // pullup resistor so it goes high unless // connected to ground: pinMode(2, INPUT_PULLUP); // initialize control over the keyboard: Keyboard.begin(); }

void loop() { while (digitalRead(2) == LOW) { // Emergency Button is ground switched so pin will go low when button is pushed Delay is to confirm signal is real delay(1000); // Once K9 popper button is pressed and signal wire sends pulse keyboard designated Emergency button is pushed Keyboard.press(Emergency); delay(300); // release control of keyboard Keyboard.releaseAll(); Keyboard.begin(); }

I don't know why it is hitting space instead of F-12, but I know why it is going over and over.

With INPUT_PULLUP, the switch connects to ground. So LOW is pressed and HIGH is not pressed.

Your code basically says, while the button is pressed, delay for a second and check again. If the button is not pressed, it runs the rest of the code which sends the keystroke. The loop function repeats over and over, so it does that over and over.

You need to reverse the logic on that while statement. And you need to look at the edge detection examples.

ok good catch. I will fix that. Is there a way to limit the presses to 2 or 3?

the space bar thing is driving me nuts.

// sets Emergency Key to F12
char Emergency = KEY_F12;    

void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  while (digitalRead(2) == LOW) {
    // Emergency Button is ground switched so pin will go low when button is pushed
    delay(1000);
  }
  // Once K9 popper button is pressed and signal wire sends pulse keyboard designated Emergency button is pushed
  Keyboard.press(Emergency);
  delay(300);
  // release control of keyboard
  Keyboard.releaseAll();
  Keyboard.begin();
}

Do those comments mean anything to you? Why no comment on why you delay for one second if the switch is closed? Did you want the switch to be held for one second before acting? Is it a momentary switch? Are we just waiting for a button press to type the emergency key?

  while (digitalRead(2) == LOW) {
    // Emergency Button is ground switched so pin will go low when button is pushed
    delay(1000);
  }

This is STUPID!

If you are reading an emergency switch, sticking your head in the sand for one second at a time is stupid. There does NOT have to be anything between the { and the }. When there is nothing to do, one usually puts a comment that says that, so others will know that the empty block is not a mistake.

Look at the state change detection example. You want to do something ONCE when the state [u]changes[/u], not every time the state [u]is[/u] something.

reason for the one second delay is we had alot of false alarms so they set the main emergency system for a 1 sec delay so we dont want the backup to go off before the main. the system has a wireless button that triggers a relay to send a constant ground to a motorola radio to alert dispatch. im integrating the computers emergency system into it as a backup. the ground does not get released on the relay till somone goes up to it and manualy resets it. the f12 only can be hit once due to the software on the computer. so it needs to delay because we have had things fall onto the relay in the trucks and really briefly give a false ground signal so the 1 sec makes sure it is a real signal.

the first reply is getting fixed. I have not had a chance to test since.

Then you need debouncing, not a delay. See if the switch goes low, then wait and check it again. If the second check shows it is still low, then go ahead with the action.

You could also change that basic debouncing algorithm to continuously check during the wait period - it can only trigger the action if it's continuously low for the entire wait period.