Return key always being pressed

Hi,

I have a control pnale hooked up to my arduino. The button in question is a NO button but the return key is always being pressed even though the push button hasnt been pressed at all. Could you also tell me how to make analogue pins to be the same as the digital pins?

Here is my script:

#include <Keyboard.h>
// ====================================================================

//Set Pin Values for buttons
const int endofday = 1;
const int reset = 2;
const int switch1 = 3;
const int switch2 = 4;
const int switch3 = 5;
const int switch4 = 6;
const int sstop = 7;
const int preshow = 8;
const int estop = 9;
const int gates = 10;
const int restraintsOPEN = 11;
const int restraintsCLOSED = 12;
const int tilt = 13;
const int selector = A0;
const int launch = A1;
const int dispatch = A2;

// Variable states
int sstopmode = 0; // station stop is off
int power = 0; // Power disconnect is off
int automode = 1; // Auto Mode is ON
void setup(){
  // Pin modes
  pinMode(endofday, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);
  pinMode(sstop, INPUT_PULLUP);
  pinMode(preshow, INPUT_PULLUP);
  pinMode(estop, INPUT_PULLUP);
  pinMode(gates, INPUT_PULLUP);
  pinMode(restraintsOPEN, INPUT_PULLUP);
  pinMode(restraintsCLOSED, INPUT_PULLUP);
  pinMode(tilt, INPUT_PULLUP);
  pinMode(selector, HIGH);
  pinMode(launch, HIGH);
  pinMode(dispatch, HIGH);
  digitalWrite(A0, HIGH);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);}
 
void loop() {
// Set variables for button states

int endofdayPOS = digitalRead(endofday);
int resetPOS = digitalRead(reset);
int switch1POS = digitalRead(switch1);
int switch2POS = digitalRead(switch2);
int switch3POS = digitalRead(switch3);
int switch4POS = digitalRead(switch4);
int sstopPOS = digitalRead(sstop);
int preshowPOS = digitalRead(preshow);
int estopPOS = digitalRead(estop);
int gatesPOS = digitalRead(gates);
int tiltPOS = digitalRead(tilt);
int selectorPOS = digitalRead(selector);
int launchPOS = digitalRead(launch);
int dispatchPOS = digitalRead(dispatch);


// |------------------------------------------------------------------------------|
// |===============================Station Controls===============================|
// |------------------------------------------------------------------------------|

//Launch Button
if (launchPOS == LOW && power == 0 && sstopmode == 0){ // If launch is pressed
  Keyboard.press('l'); // Press L
  delay (300);
  Keyboard.releaseAll();}

// Dispatch Button
if (dispatchPOS == LOW  && power == 0 && sstopmode == 0){ // If dispatch is pressed
  Keyboard.press(' '); // Press spacebar
  delay (300);
  Keyboard.releaseAll();}

// Tilt Button
if (tiltPOS == LOW  && power == 0 && sstopmode == 0){ // If tilt is pressed
  Keyboard.press(KEY_RETURN); // Press enter for 4 seconds
  delay (4000); 
  Keyboard.releaseAll();}

//Preshow doors/start
if (preshowPOS == LOW  && power == 0 && sstopmode == 0){ // If preshow is pressed
  Keyboard.press(KEY_RETURN); // press return
  delay (100);
  Keyboard.releaseAll();}


//Gates
if (gatesPOS == LOW){
  Keyboard.press(KEY_LEFT_ARROW);
  delay (100);
  Keyboard.releaseAll();}
else{
  Keyboard.press(KEY_RIGHT_ARROW);
  delay (100);
  Keyboard.releaseAll();}


//Restraints
if (restraintsOPEN == LOW){
  Keyboard.press(KEY_UP_ARROW);
  delay(100);
  Keyboard.releaseAll();}
  
if (restraintsCLOSED == LOW){
  Keyboard.press(KEY_DOWN_ARROW);
  delay(100);
  Keyboard.releaseAll();}  


//Selector
if (selectorPOS == LOW){
  Keyboard.press(KEY_LEFT_ARROW);
  delay (100);
  Keyboard.releaseAll();}
else{
  Keyboard.press(KEY_RIGHT_ARROW);
  delay (100);
  Keyboard.releaseAll();}


// |------------------------------------------------------------------------------|
// |===============================Stop Controls==================================|
// |------------------------------------------------------------------------------|

// Station Stop Button
if (sstopPOS == HIGH && power == 0){ // If sstop is pressed
  sstopmode = 1;} // Set sstop mode to 1 (On)
  
// Station Stop Reset
if (sstopmode == 1 && resetPOS == LOW){ // If sstop is engaged and reset button is pressed
  sstopmode = 0;} // Turn sstop to 0 (off)

// Estop button
if (estopPOS == LOW){ // If Estop is pressed
  sstopmode = 1; // Turn on Station Stop
  Keyboard.press('s'); // Press S
  delay(100);
  Keyboard.releaseAll();}

//Reset Estop
if (estopPOS == HIGH && resetPOS == LOW){ // If estop is up and reset is pushed
  Keyboard.press('r'); // press R
  delay(100);
  Keyboard.releaseAll();}

// End Of Day
if (endofdayPOS == HIGH){ // If endofdayestop is pressed down
  power = 1;} // Panel disable is on
if (endofdayPOS == LOW){ // if endofdayestop is off
  power = 0;} // Panel disable is off

// |--------------------------------------------------------------------------------------|
// |===============================Maintainance Controls==================================|
// |--------------------------------------------------------------------------------------|

// Switch 1
if (switch1POS == LOW && power == 0 && sstopmode == 0 && automode == 0){ // If switch 1 is pressed       // |====================================|
  Keyboard.press('1'); // Press 1                                                                        // |      Only available in Manual Mode |
  delay (100);                                                                                           // |====================================|
  Keyboard.releaseAll();}

// Switch 2
if (switch2POS == LOW && power == 0 && sstopmode == 0 && automode == 0){ // If switch 2 is pressed
  Keyboard.press('2'); // Press 2
  delay (100);
  Keyboard.releaseAll();}

// Switch 3
if (switch2POS == LOW && power == 0 && sstopmode == 0 && automode == 0){ // If switch 3 is pressed
  Keyboard.press('3'); // Press 3
  delay (100);
  Keyboard.releaseAll();}

//Switch 4
if (switch2POS == LOW && power == 0 && sstopmode == 0 && automode == 0){ // If switch 4 is pressed
  Keyboard.press('4'); // Press 4
  delay (100);
  Keyboard.releaseAll();}

// |--------------------------------------------------------------------------------------|
// |=======================================Mode Controls==================================|
// |--------------------------------------------------------------------------------------|

//Switch to auto mode:
if (resetPOS == LOW && dispatchPOS == LOW){ // If reset and dispatch have been pressed
  automode = 1;} // Set auto mode to ON
if (resetPOS == LOW && launchPOS){ // If reset and launch have been pressed
  automode = 0;} // Auto mode off.

}

Thanks

Could you also tell me how to make analogue pins to be the same as the digital pins?

Just set their pinMode() to INPUT or INPUT_PULLUP. Job done.

How are your inputs wired ?

  pinMode(selector, HIGH);
  pinMode(launch, HIGH);
  pinMode(dispatch, HIGH);
  digitalWrite(A0, HIGH);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);

The hell are you doing here?!

HIGH is not a valid option for pinMode() - in practice, since HIGH is #defined as 1, it will be interpreted as OUTPUT. And you're referring to pins by the name, and three lines away, by the pin number - don't do that, that just makes your code confusing to read and maintain.

If those are buttons that connect to ground when the button is pressed, replace those six lines with:

pinMode(selector, INPUT_PULLUP);
  pinMode(launch, INPUT_PULLUP);
  pinMode(dispatch, INPUT_PULLUP);

If things still behave incorrectly, you should verify that those pins are still working - if you had them connected to a switch that connected directly to ground, but were setting them to OUTPUT and HIGH, pressing the button could blow the pin (the maximum rated current through any pin is 40mA, recommended 20mA - you will get more than 40mA if you write a pin high then short it to ground, and this can damage the pin, though it usually doesn't)

So the analogue pins are still like this; PinMode(Dispatch, INPUT_PULLUP);

Why is the enter key being pressed though?

Is the "tilt" switch pressed? I would expect it to keep pressing return if it is. So check that switch with a multimeter, and check the value on the pin it's going to with a multimeter as well. If the pin is low, disconnect the wire to switch and check it again.

If the pin is still low, do a sanity check by disconnect everything, upload sketch that does nothing but set that pin to be INPUT_PULLUP, and measure voltage on that pin with multimeter. If it's ~0V, instead of ~5v, chip is damaged.

This might be the cause. INPUT_PULLUP Doesn't work on pin13 with onboard LED on most Arduinos and clones https://www.arduino.cc/en/Tutorial/DigitalPins

NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.

I have managed to fix it...

However, now the left and right arrows are constantly being pressed. the 3 pos switch (restraints) isnt working now either. Can you help?

Can you post code?

The LED on current Unos and Megas (since 2010) is driven by a comparator connected to D13 and requires next to no current to drive it. The internal pullup resistor works just fine. Other models have varying resistor values driving an LED directly, so the resistor/LED may present a load of 2.5mA to 8.5mA to an external driver (assuming Vf of 2.2V for the LED and 330 to 1K series resistor) and the note in reply #5 may come into play.

mxttymitch: I have managed to fix it...

However, now the left and right arrows are constantly being pressed. the 3 pos switch (restraints) isnt working now either. Can you help?

if (selectorPOS == LOW){
  Keyboard.press(KEY_LEFT_ARROW);
  delay (100);
  Keyboard.releaseAll();}
else{
  Keyboard.press(KEY_RIGHT_ARROW);
  delay (100);
  Keyboard.releaseAll();}

Code says selector pressed returns left arrow. Selector not pressed returns right arrow. You will always have one or the other.

const int restraintsOPEN = 11;
const int restraintsCLOSED = 12;

....

//Restraints
if (restraintsOPEN == LOW){
  Keyboard.press(KEY_UP_ARROW);
  delay(100);
  Keyboard.releaseAll();}
  
if (restraintsCLOSED == LOW){
  Keyboard.press(KEY_DOWN_ARROW);
  delay(100);
  Keyboard.releaseAll();}

You are testing your integer variables for LOW. You want

if (digitalRead(restraintsOPEN) == LOW){
...

Gabriel_swe: if (selectorPOS == LOW){   Keyboard.press(KEY_LEFT_ARROW);   delay (100);   Keyboard.releaseAll();} else{   Keyboard.press(KEY_RIGHT_ARROW);   delay (100);   Keyboard.releaseAll();}

Code says selector pressed returns left arrow. Selector not pressed returns right arrow. You will always have one or the other.

const int restraintsOPEN = 11;
const int restraintsCLOSED = 12;

....

//Restraints if (restraintsOPEN == LOW){   Keyboard.press(KEY_UP_ARROW);   delay(100);   Keyboard.releaseAll();}   if (restraintsCLOSED == LOW){   Keyboard.press(KEY_DOWN_ARROW);   delay(100);   Keyboard.releaseAll();}




You are testing your integer variables for LOW. You want 


if (digitalRead(restraintsOPEN) == LOW){ ... ```

Hi, because the enter/arrows are constantly being pressed it is extremely difficult to upload new code... I get the error: couldn't find a Board on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's reset button after initiating the upload.

It is plugged in and correct port is selected. However, while it is plugged in I cannot use the buttons at the top (tools ect) because pf the keys constantly being pressed. Any suggestions?

Hi,

Having extreme difficulties now…
Only Estop and reset is working now…
Here is the new code. I have completely removed all variables now and I do not understand…
I have attached code:

#include <Keyboard.h>
// ====================================================================

//Set Pin Values for buttons
const int endofday = 1;
const int reset = 2;
const int switch1 = 3;
const int switch2 = 4;
const int switch3 = 5;
const int switch4 = 6;
const int sstop = 7;
const int preshow = 8;
const int estop = 9;
const int gates = 10;
const int restraintsOPEN = 11;
const int restraintsCLOSED = 12;
const int tilt = 13;
const int selector = A0;
const int launch = A1;
const int dispatch = A2;

// Variable states
int sstopmode = 0; // station stop is off
int power = 0; // Power disconnect is off
int automode = 1; // Auto Mode is ON

void setup(){
  // Pin modes
  pinMode(endofday, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);
  pinMode(sstop, INPUT_PULLUP);
  pinMode(preshow, INPUT_PULLUP);
  pinMode(estop, INPUT_PULLUP);
  pinMode(gates, INPUT_PULLUP);
  pinMode(restraintsOPEN, INPUT_PULLUP);
  pinMode(restraintsCLOSED, INPUT_PULLUP);
  pinMode(tilt, INPUT_PULLUP);
  pinMode(selector, INPUT_PULLUP);
  pinMode(launch, INPUT_PULLUP);
  pinMode(dispatch, INPUT_PULLUP);}
 
void loop() {
// Set variables for button states

int endofdayPOS = digitalRead(endofday);
int resetPOS = digitalRead(reset);
int switch1POS = digitalRead(switch1);
int switch2POS = digitalRead(switch2);
int switch3POS = digitalRead(switch3);
int switch4POS = digitalRead(switch4);
int sstopPOS = digitalRead(sstop);
int preshowPOS = digitalRead(preshow);
int estopPOS = digitalRead(estop);
int gatesPOS = digitalRead(gates);
int tiltPOS = digitalRead(tilt);
int selectorPOS = digitalRead(selector);
int launchPOS = digitalRead(launch);
int dispatchPOS = digitalRead(dispatch);


// |------------------------------------------------------------------------------|
// |===============================Station Controls===============================|
// |------------------------------------------------------------------------------|

//Launch Button
if (digitalRead(launchPOS) == LOW){ // If launch is pressed
  Keyboard.press('l'); // Press L
  delay (300);
  Keyboard.releaseAll();}

// Dispatch Button
if (digitalRead(dispatchPOS) == LOW){ // If dispatch is pressed
  Keyboard.press(' '); // Press spacebar
  delay (300);
  Keyboard.releaseAll();}

// Tilt Button
if (digitalRead(tiltPOS) == LOW){ // If tilt is pressed
  Keyboard.press(KEY_RETURN); // Press enter for 4 seconds
  delay (4000); 
  Keyboard.releaseAll();}

//Preshow doors/start
if (digitalRead(preshowPOS) == LOW){ // If preshow is pressed
  Keyboard.press(KEY_RETURN); // press return
  delay (10000); // delay 10 seconds
  Keyboard.releaseAll();}


//Gates
if (digitalRead(gatesPOS) == LOW){
  Keyboard.press(KEY_RIGHT_ARROW);
  delay (100);
  Keyboard.releaseAll();}
else{
  Keyboard.press(KEY_LEFT_ARROW);
  delay (100);
  Keyboard.releaseAll();}


//Restraints
if (digitalRead(restraintsOPEN) == LOW){
  Keyboard.press(KEY_UP_ARROW);
  delay(100);
  Keyboard.releaseAll();}
  
if (digitalRead(restraintsCLOSED) == LOW){
  Keyboard.press(KEY_DOWN_ARROW);
  delay(100);
  Keyboard.releaseAll();}  


//Selector
//if (digitalRead(selectorPOS) == LOW){
 // Keyboard.press('x'); // Do nothing. Reprogram if needed  // |===================|
 // delay (100);                                             // |     Disabled      |
 // Keyboard.releaseAll();}                                  // |===================|
//else{
  //Keyboard.press('z');
  //delay (100);
  //Keyboard.releaseAll();}


// |------------------------------------------------------------------------------|
// |===============================Stop Controls==================================|
// |------------------------------------------------------------------------------|

// Station Stop Button
  //Removed for time being
  
// Station Stop Reset
//if (sstopmode == 1 && resetPOS == LOW){ // If sstop is engaged and reset button is pressed
//  sstopmode = 0;} // Turn sstop to 0 (off)

// Estop button
if (estopPOS == LOW){ // If Estop is pressed
  sstopmode = 1; // Turn on Station Stop
  Keyboard.press('s'); // Press S
  delay(100);
  Keyboard.releaseAll();}

//Reset Estop
if (estopPOS == HIGH && resetPOS == LOW){ // If estop is up and reset is pushed
  Keyboard.press('r'); // press R
  delay(100);
  Keyboard.releaseAll();}

// End Of Day
if (endofdayPOS == HIGH){ // If endofdayestop is pressed down
  Keyboard.press('s');
  sstopmode = 1;}
if (endofdayPOS == LOW){ // if endofdayestop is off
  sstopmode = 0;}

// |--------------------------------------------------------------------------------------|
// |===============================Maintainance Controls==================================|
// |--------------------------------------------------------------------------------------|

// Switch 1
if (digitalRead(switch1POS) == LOW){ // If switch 1 is pressed                                           // |====================================|
  Keyboard.press(KEY_RIGHT_ARROW); // Press 1                                                                        // |      Only available in Manual Mode |
  delay (100);                                                                                           // |====================================|
  Keyboard.releaseAll();}

// Switch 2
if (digitalRead(switch2POS) == LOW){ // If switch 2 is pressed
  Keyboard.press(KEY_LEFT_ARROW); // Press 2
  delay (100);
  Keyboard.releaseAll();}

// Switch 3
if (digitalRead(switch2POS) == LOW){ // If switch 3 is pressed
  Keyboard.press('1'); // Press 3
  delay (100);
  Keyboard.releaseAll();}

//Switch 4
if (digitalRead(switch2POS) == LOW){ // If switch 4 is pressed
  Keyboard.press('2'); // Press 4
  delay (100);
  Keyboard.releaseAll();}

// |--------------------------------------------------------------------------------------|
// |=======================================Mode Controls==================================|
// |--------------------------------------------------------------------------------------|

//Switch to auto mode:
//if (resetPOS == LOW && dispatchPOS == LOW){ // If reset and dispatch have been pressed
//  automode = 1;} // Set auto mode to ON
//if (resetPOS == LOW && launchPOS){ // If reset and launch have been pressed
//automode = 0;} // Auto mode off.

}
// 
/code]
Please help. It is annoying me now  :slightly_frowning_face: LOL!

It was only restraintsOPEN and restraintsCLOSED that needed to be fixed.

All other you read at the beginning of loop() and store in variables with POS in the end of the name.

You didn't have restraintsOPENPOS nor restraintsCLOSEDPOS. Thats why I only included those two in code describing what you did there.

The reason for estop and reset still working is that you haven't added digitalRead to those lines.

if (estopPOS == LOW){ // If Estop is pressed

Gabriel_swe: It was only restraintsOPEN and restraintsCLOSED that needed to be fixed.

All other you read at the beginning of loop() and store in variables with POS in the end of the name.

You didn't have restraintsOPENPOS nor restraintsCLOSEDPOS. Thats why I only included those two in code describing what you did there.

The reason for estop and reset still working is that you haven't added digitalRead to those lines.

if (estopPOS == LOW){ // If Estop is pressed

So only the restraints need the digitalread?

That is correct.

Another option is to do as the other. Put the digitalRead at beginning of loop () and store the value in variables. Personally I don't see much use to store a keypress in a variable.

Personally I don't see much use to store a keypress in a variable.

Unless you are going to use it later in the program.

Hi, Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? So we can see how you have wired your inputs and outputs.

You appear from your code to use digital i/o pin 1, this is the Tx pin used to program the 328, this will be why you are having problems programming your UNO. You should be keeping pins 0 and 1 unused so you can program the controller.

Thanks.. Tom.. :)

If you are having trouble downloading, press & hold Reset when you start the download from the IDE. When it shows "compiled xxx of 32xxx bytes", then release Reset. Timing can be easier to see if select File:Preferences and turn on Verbose outputs.