Coding issue with my Dc motor

Hello evryone, I would like to know if it is possible to guide a simple dc motor on arduino using a H-bridge with only one button and 2 switch limits as demanded below

if button==HIGH && switch limit1==HIGH then motor right till it reachs switch limit 2 then stops
if button==HIGH && switch limit2==HIGH then motor left till it reachs switch limit 1 then stops
if switch limit1==LOW && Switch limit 2==LOW && button==HIGH then motor stops 
if switch limit1==LOW && Switch limit 2==LOW && button==HIGH and last_state of switch limit1 was HIGH && motor==OFF then motor back to switch limit 1 and stops there
if switch limit1==LOW && Switch limit 2==LOW && button==HIGH and last_state of switch limit2 was HIGH and motor==OFF then motor back to switch limit 2 and stops there

i tested that with more than one programmation method but i couldn't reach any result Thank you very mucuh for your opinions. Friendly zirus 109

Please post the program that you tried. Please use [­code] [­/code] tags around your code when you post it. Details of how the button and switches are wired would help too.

Hello , here is my code

int button=2;                
int FrontPin = 4;             
int BackPin = 5;           
int motor2Pin = 8;      
int enablePin = 9;    
int motor1Pin = 12;   
 boolean last_FrontPin=HIGH;
 boolean last_BackPin=HIGH;
 boolean state_FrontPin=LOW;
 boolean state_BackPin=LOW;
 boolean last_bout=HIGH;
 boolean currentbout=LOW;
 int val;
 int speed=255;
 
void setup() {
  pinMode(button,INPUT_PULLUP);
  pinMode(FrontPin,INPUT);
  pinMode(BackPin,INPUT);
  pinMode(motor1Pin,OUTPUT);
  pinMode(motor2Pin,OUTPUT);
  pinMode(enablePin,OUTPUT);
 
}
boolean Read(boolean last)
{boolean current=digitalRead(button);
  if(last != current)
  {delay(5);
  current=digitalRead(button);}
  return current;}
  
void loop() {
  digitalWrite(enablePin,HIGH);
  currentbout = Read(last_bout);
 if ((currentbout)==LOW && (last_bout)==HIGH && digitalRead(FrontPin)==HIGH) //
 {
  analogWrite(motor1Pin,speed);
  analogWrite(motor2Pin,!speed);
  }
  
else if ((currentbout)==LOW && (last_bout)==HIGH && digitalRead(BackPin)==HIGH)
  
   { analogWrite(motor1Pin,!speed);
  analogWrite(motor2Pin,speed);}
  
 
else if(digitalRead(BackPin)==HIGH && digitalRead(FrontPin)==LOW )
 
  {analogWrite(motor1Pin,!speed);
  analogWrite(motor2Pin,!speed);
  }
  
   else if( digitalRead(FrontPin)==HIGH && digitalRead(BackPin)==LOW)//
  { Read(currentbout);
  
  if ((currentbout)==LOW) 
  {analogWrite(motor1Pin,!speed);
  analogWrite(motor2Pin,!speed);
  }}
  else if (digitalRead(FrontPin)==LOW && digitalRead(BackPin)==LOW)
  {Read(last_FrontPin);}
  if ((state_FrontPin)==LOW && (last_FrontPin)==HIGH)
  { analogWrite(motor1Pin,!speed);
  analogWrite(motor2Pin,speed);}
  else if (digitalRead(FrontPin)==LOW && digitalRead(BackPin)==LOW)
   {Read(last_BackPin);
   if ((state_BackPin)==LOW && (last_BackPin)==HIGH)
  { analogWrite(motor1Pin,speed);
  analogWrite(motor2Pin,!speed);}
  }
  else if (digitalRead(FrontPin)==LOW && digitalRead(BackPin)==LOW && digitalRead(button)==LOW)
   { analogWrite(motor1Pin,!speed);
  analogWrite(motor2Pin,!speed);}
  }

I atteched a screen shot of my wiring below

testcon.PNG

Hi, We need a higher resolution picture. Also your button is pulling high when pressed and you have a pull_low resistor,so.

pinMode(button,INPUT_PULLUP);

Needs to be

pinMode(button,INPUT);

Also do a TOOLS >AutoFormat to align your code text. or CTRL-T Thanks.. Tom.. :)

Hi Tom,
I attached a copy of my code and a new screenshot for the wiring like you demanded
I hope everything will be clear.
Thank you very much for your answer & still waiting your reply
Friendly zirus109

test_condition.ino (2.09 KB)

I attached a copy of my code and a new screenshot for the wiring like you demanded

Asked. He politely ASKED for a better picture. There was no DEMAND for a better picture.

Thank you for the information Paul :)

In your circuit pin 2 is connected to the button and there is no LCD In your code pin 2 is used by an LCD.

Which is correct ?

Hi, I am sorry , which LCD!!!!

You have not posted your current code after: 1) putting every { on a line BY ITSELF 2) putting every } on a line BY ITSELF 3) using Tools + Auto Format.

The code you posted is VERY hard to read as is.

Use some Serial.print() statements to see which block of code IS being executed.

Ditch most of that code until you KNOW that the motors are working properly.

  analogWrite(motor1Pin,speed);
  analogWrite(motor2Pin,!speed);

What does not 255 mean? The analogWrite() function expects a numeric value between 0 and 255, inclusive. If all you are doing is hard on the brakes or balls to the wall, use digitalWrite() with HIGH or LOW, NOT "not 255".

zirus109:
Hi,
I am sorry , which LCD!!!

The LCD in test_condition.ino that you attached to reply #4

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 9, 5, 4, 3, 2);

and all the references to the lcd object.

Hi, When I open the post #4 I get

int button = 2;
int FrontPin = 4;
int BackPin = 5;
int motor2Pin = 8;
int enablePin = 9;
int motor1Pin = 12;
boolean last_FrontPin = HIGH;
boolean last_BackPin = HIGH;
boolean state_FrontPin = HIGH;
boolean state_BackPin = HIGH;
boolean last_bout = HIGH;
boolean currentbout = LOW;
int val;
int speed = 255;

void setup() {
  pinMode(button, INPUT);
  pinMode(FrontPin, INPUT);
  pinMode(BackPin, INPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);

}
boolean Read(boolean last)
{ boolean current = digitalRead(button);
  if (last != current)
  { delay(5);
    current = digitalRead(button);
  }
  return current;
}

void loop() {
  digitalWrite(enablePin, HIGH);
  currentbout = Read(last_bout);
  if ((currentbout) == HIGH && (last_bout) == LOW && digitalRead(FrontPin) == HIGH) //
  {
    analogWrite(motor1Pin, speed);
    analogWrite(motor2Pin, !speed);
  }

  else if ((currentbout) == HIGH && (last_bout) == LOW && digitalRead(BackPin) == HIGH)

  { analogWrite(motor1Pin, !speed);
    analogWrite(motor2Pin, speed);
  }


  else if (digitalRead(currentbout) == HIGH && digitalRead(BackPin) == LOW && digitalRead(FrontPin) == LOW )

  { analogWrite(motor1Pin, !speed);
    analogWrite(motor2Pin, !speed);
  }

  else if ( digitalRead(FrontPin) == HIGH && digitalRead(BackPin) == LOW)
  { Read(currentbout);

    if ((currentbout) == LOW)
    { analogWrite(motor1Pin, !speed);
      analogWrite(motor2Pin, !speed);
    }
  }
  else if (digitalRead(FrontPin) == LOW && digitalRead(BackPin) == LOW)
  {
    Read(last_FrontPin);
  }
  if ((state_FrontPin) == LOW && (last_FrontPin) == HIGH)
  { analogWrite(motor1Pin, !speed);
    analogWrite(motor2Pin, speed);
  }
  else if (digitalRead(FrontPin) == LOW && digitalRead(BackPin) == LOW)
  { Read(last_BackPin);
    if ((state_BackPin) == LOW && (last_BackPin) == HIGH)
    { analogWrite(motor1Pin, speed);
      analogWrite(motor2Pin, !speed);
    }
  }
  else if (digitalRead(FrontPin) == LOW && digitalRead(BackPin) == LOW && digitalRead(button) == LOW)
  { analogWrite(motor1Pin, !speed);
    analogWrite(motor2Pin, !speed);
  }
}

So I don't know what you guys are smokin'

Tom... :)

When I open the post #4 I get

So do I now so I don't know what happened. Sorry for any confusion.

Hi, You are trying to analogWrite to pins 8 and 12, they are not PWM pins.

I would suggest you research how to drive the H-Bridge.

http://communityofrobots.com/tutorial/kawal/how-drive-dc-motor-using-l293d-arduino

Then write some code that JUST drives the motor, nothing else in the code. Lets establish that you can control the motor before adding all the switches.

Tom.. :)

Hi,
Using your connections, see if this code works, it compiles, and “should” work.

int motor2Pin = 8;
int enablePin = 9;
int motor1Pin = 12;

void setup() {
  Serial.begin(9600);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
}


void loop() {
  Serial.println("START");
  digitalWrite(motor1Pin, HIGH); //setup IN1 and IN2 to drive in one direction
  digitalWrite(motor2Pin, LOW);
  for (int i = 0; i < 250; i = i + 10)
  {
    analogWrite(enablePin, i);  // PWM enable to control the speed of the motor
    delay(250);
    Serial.print("one direction  ");
    Serial.println(i);
  }
  analogWrite(enablePin, 0);
  Serial.println("STOP");
  delay(1000);
  Serial.println("START");
  digitalWrite(motor1Pin, LOW); //setup IN1 and IN2 to drive in other direction
  digitalWrite(motor2Pin, HIGH);
  for (int i = 0; i < 250; i = i + 10)
  {
    analogWrite(enablePin, i);  // PWM enable to control the speed of the motor
    delay(250);
    Serial.print("other direction  ");
    Serial.println(i);
  }
  analogWrite(enablePin, 0);
  Serial.println("STOP");
  delay(1000);
}

It should

  • Start your motor spinning on one direction with increasing speed.
  • Then stop.
  • Then start your motor spinning in the other direction with increasing speed.
  • Then stop.
  • And repeat.

Open the IDE monitor and set to 9600 to monitor the code as it works.

Tom… :slight_smile:

Then the motor should continue his way when button is pressed again

I hear the sound of moving goalposts.

Hi tom, Before all thank you very much foe your answer and thank you for spending time thinking about my problem. your code works pretty good but my issue is how to control the DC motor with one button and 2 switch limits Four steps to do:

Stop :both switch limits are low && button is HIGH or switch limit 2 is HIGH
Turn right : switch limit 2 and button are both high
Stop:both switch limits are LOW && button is HIGH or switch limit 1 is HIGH
Turn left:switch limt 1 and button are both HIGH

Thank you again for your support Friendly Zirus109

Hi, Can I suggest you have a START and a STOP button, it makes the coding less complex and a START and STOP button makes more sense.

In process control you always have a START and STOP, not one button doing both.

I gather you want to run a motor between two limit switches. Press START. The limit switches change direction of the motor so it goes forwards and backwards continuously. Press STOP and it stops.

Is that what you are trying to do?

Tom.... :)

HI, I did it already with start and stop buttons and that works perfectly I am allowed to use just one button now to command all the tasks for that project and that's where i find difficulties Any way thank you again and again Very happy to find peoples like you here Have a great day Tom Friendly Zirus109