Forward, Reverse and Stop for motor

Hi,

I want to control a motor as per following

When I press forward button digitalwrite(4) HIGH
When I press reverse button digitalwrite(5) HIGH
When I press stop button digitalwrite(4) or (5) LOW

I want to run until stop button press.

any help will be highly appreciated.

Thank you,
Nadeeka

Try something, show it here if it does not work and we will help you further. You already have found digitalWrite, how about digitalRead to read your buttons?

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(12,LOW);
}
}

this one I tried and it doesn't work.

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 :slight_smile:

Its working perfectly, Thank you very much. :slight_smile:

but I cant understand how does its works with #define :confused:

how does its works with #define :confused:

Your code

 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
#define MOTORFORWARD 13
#define MOTORREVERSE 12
#define BTNFORWARD 2
#define BTNREVERSE 3
#define BTNSTOP    4

Can you see how it works now?

'#define' defines something :wink: In this case it defines names for pin numbers. So instead of using pin numbers (that you need to remember or look up again) in pinMode and digitalRead and digitalWrite, you can now use sensible names.

Ok, I got that. thank you again.