DC motor control

Afternoon all,

I have come to the conclusion that this kind of thing is one of those talents I may never even get a basic grasp of :slight_smile: .

Would you be able to assist? I have no doubt that what I Have written in code terms is probably a bit of ham fisted junk so forgive my rather poor attempt and for making your eyes bleed..

it should be very easy but I'm afraid i don't seem to be grasping this very well.... and the more i look at the examples the worse it seems to get :confused:

here is what i am attempting and failing miserably at.

I should add that i do have 10k resistors on the switches and they do seem to function well.

1 simple dc motor (not a servo) with a single limit switch. i would like to use a single point in its rotation as a reference/calibration point.

2 it will rotate in one direction until it hits the microswitch and this will be the reference point - if its not already hitting the switch that is....

3 using another 2nd switch i would like to advance the motor for 2 seconds or there about and then stop the motor.

4 i would then like to use the same 2nd switch to reverse the motor until.... yup you guessed it. until it reaches the microswitch.

that's it. thus far....

here is my mess of a code.... go easy on me i just started a few days ago

                                              // constants wont change there set here and show the pin numbers
const int buttonPin = 2;                      // the number of the internal microswitch digital pin
int buttonState = 0;                          // the variable for reading the button state. this variable will change

const int buttonPin2 = 3;                     // the number of the single hand operation switch open/close
int buttonState2 = 0;

const int Ena = 9;                      // the enable input on the digital pin
const int in1 = 5;                      // the motor direction control pin 1 
const int in2 = 4;                      // the motor direction control pin 2

                                        // the following variables are unsigned long's because the time, measured in miliseconds,
                                        // will quickly become a bigger number than can be stored in an int.

unsigned long lastDebounceTime = 0;     // the last time the output pin was toggled
unsigned long debounceDelay = 50;       // the debounce time. increase if jittery

void setup() {
                                        // put your setup code here, to run once: 
  pinMode (buttonPin, INPUT);           // microswitch pin input
  pinMode (buttonPin2, INPUT);          // Microswitch button
  pinMode (Ena, OUTPUT);                // motor enable pin
  pinMode (in1, OUTPUT);                // motor direction control pin in1
  pinMode (in2, OUTPUT);                // motor direction control pin in2
void loop() {
  buttonState = digitalRead(buttonPin);       // read the push-button input pin to set the microswitch for callibration/reference of the drive motor 
  buttonState2 = digitalRead(buttonPin2);     // read the operation switch to determine its instruction.

    if(buttonState == LOW){                  // If button state is HIGH (5v) then  the motor should do nothing as itr has already moved to the wheel to the microswitch and set it to +5v 
    digitalWrite (Ena, HIGH);
    digitalWrite (in1, HIGH);
    digitalWrite (in2, LOW);
     } else {                                 // if the button is NOT HIGH  or in  a LOW state then rotate the motor (check physical direction) until the microswitch contacts and goes high
     digitalWrite (Ena, HIGH);
     digitalWrite (in1, LOW);
     digitalWrite (in2, LOW);
     if (buttonState2 == HIGH){               // If button 2 is pressed then this should operate the motor for 2 seconds in one direction and then stop
        digitalWrite (Ena, HIGH);
        digitalWrite (in1, LOW);
        digitalWrite (in2, HIGH);
          digitalWrite (Ena, HIGH);
          digitalWrite (in1, LOW);
          digitalWrite (in2, LOW);

            if (buttonState2 == HIGH){          // If button 2 is pressed again then this should operate the motor in reverse until it hits microswitch
        digitalWrite (Ena, HIGH);
        digitalWrite (in1, HIGH);
        digitalWrite (in2, LOW);

I should add that in its present form it does the correct function to begin with i.e. it rotates the motor until it hits the switch - if i press the second switch it advances for 2 seconds but then just returns to the looking for the switch function. if i hold the microswitch and press the second switch it works how i would like but doesn't switch direction... . i think it has to do with the fact that the 2nd button being pressed while the first is still high is not playing well... as well as the rest of my junk code

please for the love of god point me in the right direction....

please also keep this so a chump like me might ba able to grasp it a bit.

cheers lads n lassies

What are the specs of your DC motor?

The one I want to use is 12 volts which draws just over half an amp when under load but I'm just using a small motor to test on bench and breadboard and running it at 8 volts. I have been running both through a drok motor shield...



This is the motor and shield. It a very old motor :slight_smile:

I would write a digitalWrite (Ena, HIGH); in the setup code.

I believe the problem is that you need to add logic to your if statements. For example, after you delay the motor for 2 seconds, your if statement needs to say if (button2 == HIGH && button ==HIGH) to consider the states of both buttons.

Ah... I will give this a go in the morning. Thanks for the heads up :slight_smile: