Switch triggers by itself sometimes

Hello.

I have this switch connected to an Arduino pin (internally pulled up) and GND.
The idea is to move a stepper while the switch is not activated.
It does not work.
Sooner or later the switch reads low by itself and the moving stops. First I thought this is some due to parasite energy and what not, also because it tended to read a 0 when I touched the switch / wires.
Then I tried this sketch and it works ok... . If any 0 is read, it turn the light on for 2 secs. The led only lights when i press the button. So this is ok. Even if I slam the darn thing or chew on the cables.

const int buttonPin = 14;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
digitalWrite(buttonPin,HIGH);  
}

void loop(){
  // read the state of the pushbutton value:
  digitalWrite(ledPin, LOW);
  buttonState = digitalRead(buttonPin);
  while(buttonState) {buttonState = digitalRead(buttonPin);};
  digitalWrite(ledPin, HIGH);
  delay(2000);  

}

My stepper motor code looks like this:

int button;
	while (button)
	{
		digitalWrite(step,HIGH);
		delay(10);
		
		digitalWrite(step,LOW);
		delay(10);

		button = digitalRead(limit_east);
	
	}

This is weird for me, but it is probably one of those simple things that your brain keeps missing however hard you look. Any help appreciated

well my application with the stepper is actually a lot larger than that. I use a rtc, interrupts, EEPROM and some heavy computation. Maybe some settings influence it? The pin is pin 14 on an Arduino Mega , TX3 that is, but I dont use Serial3...
But this stepping part is only initialization. It is called from the setup. Could that be a problem?

You have an extra semicolon where there should not be one. It may fix it, it may not, but it does stand out.

while(buttonState) {buttonState = digitalRead(buttonPin);};

that quote is from the first piece of code, which is the one that works... the second one I am struggling with...

A schematic would help, and posting the full code will too. If your code is too large to post normally, then make it a .txt file and make it an attachment.
Are you trying to control the stepper motor yourself or are you using the stepper library?

int button;
while (button) //when is "button" HIGH?
{
digitalWrite(step,HIGH);
delay(10);

digitalWrite(step,LOW);
delay(10);

button = digitalRead(limit_east);

}

i control the stepper myself. The stepper driver has 3 pins: dir , enable, and step.
I didn't place the whole code cause i think it has nothing to do with anything...
but here you go:

check for function initialize();

licenta.ino (7.84 KB)

Ok, I dont have time right now to check the code, so I will do it when I get home tonight. However I do have a quick question, have you tried the stepper by itself in its own sketch? Test the stepper by itself and cofirm that it works with the code you wrote. If it doesn't then redesign the code to make it work. Make those delays longer say 100 - 500.

no problem thank you

Just a guess...

	pinMode(limit_east, INPUT);           
	digitalWrite(limit_east, HIGH);       
	pinMode(limit_east, INPUT);          
	digitalWrite(limit_west, HIGH);

Did you really mean to put limit_east in the third line?

alkalin:
I have this switch connected to an Arduino pin (internally pulled up) and GND.
The idea is to move a stepper while the switch is not activated.
It does not work.
Sooner or later the switch reads low by itself and the moving stops

When it seems that your application is defying the laws of physics, you're probably making a false assumption somewhere.

You have concluded that the switch 'reads low by itself'. To test that theory, you could write a trace message when the switch reads low and see if it happens when the problem occurs.

You have concluded that the switch 'reads low by itself'. To test that theory, you could write a trace message when the switch reads low and see if it happens when the problem occurs.

Yes that was the apparent problem. But like I said it is not. Because i tested with the first sketch I placed in my first post. When the switch is low a led lights for 2 secs. And that does not happen by itself.
So it must be soft... which is the only difference between the 2...

HazardsMind:
Ok, I dont have time right now to check the code, so I will do it when I get home tonight. However I do have a quick question, have you tried the stepper by itself in its own sketch? Test the stepper by itself and cofirm that it works with the code you wrote. If it doesn't then redesign the code to make it work. Make those delays longer say 100 - 500.

Everything works, the switch is a small safety add-on to the project.

TanHadron:
Just a guess...

	pinMode(limit_east, INPUT);           
digitalWrite(limit_east, HIGH);       
pinMode(limit_east, INPUT);          
digitalWrite(limit_west, HIGH);


Did you really mean to put limit_east in the third line?

nice call. Solved. Probably setting the pin to input again disables the pull-up, so the floating pin is unstable?
Almost sorry i bothered you with this stupid error. But like i said it's the most difficult to see your own mistakes...

You now got 1 karma!

Probably setting the pin to input again disables the pull-up, so the floating pin is unstable?

That's what I was guessing. And it appears to be the case. Here is the pinMode code from the system:

void pinMode(uint8_t pin, uint8_t mode)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	volatile uint8_t *reg, *out;

	if (port == NOT_A_PIN) return;

	// JWS: can I let the optimizer do this?
	reg = portModeRegister(port);
	out = portOutputRegister(port);

	if (mode == INPUT) { 
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out &= ~bit;
		SREG = oldSREG;
	} else if (mode == INPUT_PULLUP) {
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out |= bit;
		SREG = oldSREG;
	} else {
		uint8_t oldSREG = SREG;
                cli();
		*reg |= bit;
		SREG = oldSREG;
	}
}

It looks like you could use pinMode(limit_east, INPUT_PULLUP);

TanHadron:

It looks like you could use pinMode(limit_east, INPUT_PULLUP);

I knew about that thanks.