0

I have an infinite loop issue that keeps persisting in this code on my Teensy 3.1. This program is intended to take an input on Pin 0 and press the Enter key on the computer it is hooked into.

It is connected to a wire with no current but once a switch is flipped it should send a 5v Digital Signal blip to the Teensy and trigger it to run. It does function but it keeps getting caught in an infinite loop for some reason. Any help would be greatly appreciated!

// Global Variable
int signal_receiving_pin = 0;

void setup() {                
  // Outputting for error checking
  Serial.begin(38400);

  // Readying the pin for input
  pinMode(signal_receiving_pin, INPUT);
}

void loop() 
{  
// If statement to check whether an input has been received
if (digitalRead(signal_receiving_pin) == HIGH)
{
  // If the signal has been detected..
  Serial.println("The signal has been received \n");
  Serial.println("Now sending the ENTER signal \n");


  // .. The ENTER key is sent to the computer 
  //Keyboard.press(KEY_ENTER);
  //Keyboard.release(KEY_ENTER);

  // Waiting 1 minute before allowing more input
  //delay(60000); // Pause
}
else
{
  // If the signal is not detected..
  // it will let you know it isn't there
  Serial.println("Input not detected \n");
}
}
1
  • Do you have a pulldown resistor?
    – gre_gor
    Jan 8, 2018 at 21:29

1 Answer 1

0

Try using the internal Pullup/Pulldown resistor to control the input when the pin is not connected to anything. If you do not have a pullup/pulldown, then the input capacitance on the input pin circuitry can sample and hold the 5V unreliably.

...
void setup() {                
  // Outputting for error checking
  Serial.begin(38400);

  // Readying the pin for input
  pinMode(signal_receiving_pin, INPUT_PULLDOWN);
}
...

You may want to uncomment the delay() or add some state logic trigger only one activation per each cycle of signal_receiving_pin

1
  • 1
    Just changing to INPUT_PULLDOWN solved my problem! Thank you good sir!
    – Flash2017
    Jan 10, 2018 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.