Please post code between code tags
** **[code]** **
your code here
** **[/code]** **
so it will look like
void setup() {
// put your setup code here, to run once:
pinMode (13, OUTPUT); // for forward contactor shield
pinMode (12, OUTPUT); // for reverse contactor shield
pinMode (2, INPUT_PULLUP); // for forward push button
pinMode (3, INPUT_PULLUP); // for reverse push button
pinMode (4, INPUT_PULLUP); // for stop push button
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(2) == LOW) {
while (digitalRead(4) == LOW);
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
if (digitalRead(3) == LOW) {
while (digitalRead(4) == LOW);
digitalWrite(12, HIGH);
}
else {
digitalWrite(13, LOW);
}
}
'It does not work' does not mean anything. You need to be far more clear in what you expect it to do and what it actually does.
if (digitalRead(2) == LOW) {
while (digitalRead(4) == LOW);
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
The above is executed if button 2 (forward; please use sensible names instead of number) is pressed. Next it does not do anything while button 4 (stop) is pressed and next it will activate the forward contactor.
If button 2 is not pressed, you while de-activate the contactor.
What I think that you want is
press forward once, de-activate reverse and activate forward
press reverse once, de-activate forward and activate reverse
press stop once, de-activate both
#define MOTORFORWARD 13
#define MOTORREVERSE 12
#define BTNFORWARD 2
#define BTNREVERSE 3
#define BTNSTOP 4
void setup() {
// put your setup code here, to run once:
pinMode (MOTORFORWARD, OUTPUT); // for forward contactor shield
pinMode (MOTORREVERSE, OUTPUT); // for reverse contactor shield
pinMode (BTNFORWARD, INPUT_PULLUP); // for forward push button
pinMode (BTNREVERSE, INPUT_PULLUP); // for reverse push button
pinMode (BTNSTOP, INPUT_PULLUP); // for stop push button
}
void loop() {
// if forward pressed
if(digitalRead(BTNFORWARD) == LOW)
{
// stop reverse (just in case)
digitalWrite(MOTORREVERSE, LOW);
// go forward
digitalWrite(MOTORFORWARD, HIGH);
}
// if reverse pressed
if(digitalRead(BTNREVERSE) == LOW)
{
// stop forward (just in case)
digitalWrite(MOTORFORWARD, LOW);
// go reverse
digitalWrite(MOTORREVERSE, HIGH);
}
// if stop pressed
if(digitalRead(BTNSTOP) == LOW)
{
// stop forward
digitalWrite(MOTORFORWARD, LOW);
// stop reverse
digitalWrite(MOTORREVERSE, LOW);
}
}
What you need to add is some intelligence if multiple buttons are pressed at the same time; pressing forward and reverse will make the motor go crazy 