Motor does not switch reverse in function while getting the signal for switch reverse bye can

Hello People!

i have some code here… the motor does not switch reverse when it gets a signal down when (down==1).it just go up when up==1 and stops when up=0 and down =0 but dont go down( turn the other side)while down==1 and up =0;.Can anyone tell me why and how you can write a working code?It works normal and goes up when up==1 after reach the enddestination in one way .It does not reverse.Whats the problem?

void motor()
{
if (up==1){

digitalWrite(m1,HIGH);
digitalWrite(m2,LOW);

}

if (up==0&&down==0||(down==1&&up==1)){

digitalWrite(m1,LOW);
digitalWrite(m2,LOW);

}

if (down==1){

digitalWrite(m1,LOW);
digitalWrite(m2,HIGH);

}

}

You need to post your entire sketch. What are you using to drive your motor? a shield of some type? a schematic would also be helpful

You have given the incomplete information. Provide more to understand and analize it

Since the (down == 1) is last, even if they are both 1 and the middle ‘if’ turns the motor off, the motor will immediately turn back on because down == 1.

Try this:

void motor()
{
  if (up==down)
  {
    // Stop
    digitalWrite(m1,LOW);
    digitalWrite(m2,LOW);
  }
  else
  {
  if (up)
  {
    // Up
    digitalWrite(m1,HIGH);
    digitalWrite(m2,LOW);
  }
  else
  {
    // Down
    digitalWrite(m1,LOW);
    digitalWrite(m2,HIGH);
  }
}

Hi thanks for your reply when i try your code it gives an error like below.
Arduino: 1.8.13 (Windows 10), Board: “Arduino Nano, ATmega328P”

C:\Users\Cihan\OneDrive\Documenten\Arduino\send_encoder\send_encoder.ino: In function ‘void motor()’:

send_encoder:113:1: error: a function-definition is not allowed here before ‘{’ token

{

^

send_encoder:123:14: error: a function-definition is not allowed here before ‘{’ token

void setup() {

          ^

send_encoder:153:13: error: a function-definition is not allowed here before ‘{’ token

void loop() {

         ^

send_encoder:180:1: error: expected ‘}’ at end of input

}

^

exit status 1

a function-definition is not allowed here before ‘{’ token

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

i made it error free and upload it, by taking out some brackets like below:
only the engine still does not reverse while the down is 1 when, i look at the plotter and down==1 and engine does not do anything?

void motor()
{
if (up==down)
{
// Stop
digitalWrite(m1,LOW);
digitalWrite(m2,LOW);
}
else
if (up)
{
// Up
digitalWrite(m1,HIGH);
digitalWrite(m2,LOW);
}

else

{
// Down
digitalWrite(m1,LOW);
digitalWrite(m2,HIGH);
}

If down == 1 AND up == 1, that means STOP. Did you check the value of up?

void motor()
{
  if (up != down)
  {
    if(up)
    {
      digitalWrite(m2,LOW);
      digitalWrite(m1,HIGH);
    }
    else
    {
      digitalWrite(m1,LOW);
      digitalWrite(m2,HIGH);   
    {
  else
  {
    digitalWrite(m1,LOW);
    digitalWrite(m2,LOW);
  }
}
   else
    {
      digitalWrite(m1,LOW);
      digitalWrite(m2,HIGH);   
    { // <---- This should be '}'
   // <---- And there should be a '}' here
  else
  {

hi if i put:

if (up != dowqn)
{
if(up)
{
digitalWrite(m2,LOW);
digitalWrite(m1,HIGH);
}
else
if(down)

{
  
  digitalWrite(m1,HIGH);
  digitalWrite(m2,LOW);   
}

}
else{
digitalWrite(m2,LOW);
digitalWrite(m1,LOW);
}

}

it does go up and when i push to down it also goes up untill -32357 far behind the highest destination and then stops, while i can see at serial monitor down=1.

UP and DOWN both set m1 HIGH and m2 LOW, just in different order. That’s why both move in the same direction.

yes i changed it and the code still blocks and doesnt goes down.only up

thank you for the contribution and help,i found the problem it was a disconnected wire from the motor driver to the nano :slight_smile:

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