Random button presses

I have a project that has been working for some time now and when I first put it together I had a problem with phantom button presses, I switched over to a shielded cable(about 4ft long) and the problem went away.
I have been replacing non shielded cable with shielded cable on all my connections(stepper motors/Drivers and some Hall sensors) and now I have phantom button presses again.
I have a standard limit switch type foot switch with a digital pin(2) going to one side of the switch and a GND to the other with the internal PullUP turned on(Arduino Due).

Is that not the correct way to do it?

If it is what should I try next?

Here is the button debounce code I'm using.

const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis;

void setup() {
 
  pinMode(button, INPUT_PULLUP);
  Serial.begin(115200);
}
void loop() {
  if ( (millis() - previousMillis) >= 50) {    //state machine for button & debounce
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = millis();
    old_val = val;
    Serial.println(buttonstate);
  }
}

Here's a wiring diagram

should be
unsigned long previousMillis = 0;

should be
previousMillis += 50;
As that is the timer window you need to maintain.

I don't think that would effect debounce though.

I think the issue is between val, old_val, and buttonstate.

That code doesn't compile.

All I see is that your button reading solution checks the status of the button at 50 ms intervals. That is not really debouncing.
This example I use from time to time. It requires that any change of state is stable for a defined number on milliseconds before accepting it and so is quite suited for noisy environments.

bool  getKey() {
  // get debounced key press
  static bool currentKeyState = HIGH ;
  static bool newKeyState = HIGH ;
  static uint32_t newKeyStateAtMs = millis()  ;

  bool dr =  digitalRead(inputPin) ;
  if ( dr != newKeyState ) {
    newKeyState = dr ;
    newKeyStateAtMs = millis() ;
  }
  if ( currentKeyState != newKeyState && millis() - newKeyStateAtMs > 50) {   // configure this as required.
    // debounce: accept only a mature change ( > X ms )
    currentKeyState = newKeyState ;
  }
  return currentKeyState ;
}

Thanks,
That seems to have done the trick, I also increased the millis delay which helped out.
I have been moving things around trying to get better performance and must have deleted it. This was code written by Massimo Banzi for debouncing a button and I made it work for my project.
Here's what I ended up with.

const int button = 2;            // pin button is on
int val = 0;                     // current button state
int old_val = 0;
int buttonstate = 0;
unsigned long previousMillis = 0;

void setup() {
 
  pinMode(button, INPUT_PULLUP);
  Serial.begin(115200);
}
void loop() {
  val = digitalRead(button);
  if ( (millis() - previousMillis) >= 200) {    //state machine for button & debounce
    if ((val == 0) && (old_val == 1)) {
      buttonstate = 1 - buttonstate;
    }
    previousMillis = millis();
    old_val = val;
    Serial.println(buttonstate);
  }
}

It compiles now, I missed the closing bracket on the Setup.

I will give your code a try.
Thanks

Turns out that hasn't solved my problem.

I'm getting some type of RF/EMF interference that seems mess up my code from operating properly.

I have recently upgraded all my cables to shielded cables, all cable drains connect to Arduino GND and all stepper motors have the motor chassis grounded back to the high power supply. The Arduino has a separate power supply than the motors.

The motors all have 1000uf cap's on their power connections with shielded cables between the motor and driver(Gecko 210x drivers)and the cable drain is connected to the Arduino GND.

Any thoughts on what to try next, I thought it was just a button problem so do you think I should get the thread moved to the Motors,CNC section?

Well. you could try a 1k external pull up resistor on the button to make it less responsive to transient spikes.

I will give it a try and see what happens.

That took care of it, I ran the machine for an hour and no ghost presses.

Thanks Everybody.

Internal pullups are very weak, for a medium/long cable you want a stiffer pullup, say in the 1k to 4k7 range. The internal pullup is really just for stuff on the same pcb, not signals coming in from the big bad EMI-strewn world.

Noted.

I need to stop being so lazy and just add resistor's and Cap's to all my circuit's up front instead of chasing problems after.

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