Controlling DC motor using encoder and push buttons

Ok thanks a lot I will try this

So far I have coded

int state1 = 0; 
int state2 = 0;
int dc1 = 5; // negative
int dc2 = 6; // positive
int deploybutton = 7;
int retract button = 4;

void setup() {
  pinMode (dc1, OUTPUT);
  pinMode (dc2, INPUT);
  pinMode (deploybutton, INPUT);
  pinMode (retractbutton, INPUT);


}

void loop() {
  state 1 = digitalRead (deploybutton);
  state 2 = digitalRead (retractbutton);

  if (state1 == HIGH)
  {
    digitalWrite (dc1, LOW);
    digitalWrite (dc2, HIGH);
  }
  else {
    digitalWrite (dc1, LOW);
    digitalWrite (dc2, LOW);
  }

  if (state2 == HIGH)
  {
    digitalWrite (dc1, HIGH);
    digitalWrite (dc2, LOW);
  }
  else {
    digitalWrite (dc1, LOW);
    digitalWrite (dc2, LOW);
  }
}

All pins are placeholders for the moment, this section of the code is basically for the motor to turn either clockwise or anti depending on which button your press.

Basically I would appreciate if someone could link any similar examples for using the hall effect sensor and the best approach it within the bounds of the requirements.Use code tags to format code for the forum

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

  • the button pins should be configure as INPUT_PULLUP (see above)

  • why isn't dc2 configures as an OUTPUT like dc1?

  • a button should be tested for LOW if pressed (see above)

  • the else conditions don't make sense since the motor would be turned off if either button is not pressed. if may make sense that the 2nd button test is an else if and only if Neither button is pressed the motor is turned off

    if (button1 pressed)
        motor (forward)
    else if (button2 pressed)
        motor (reverse)
    else
        motor (off)

have you googled for hall effect encoder code?

Using a library would break the rules as would calling an ISR

Although strictly an ISR would not be called from the loop() function

I had thought calling a library would not be allowed as per the requirements but I will have to get that double checked.

I have looked at examples and had wrote some code but I thought it made no sense.

what is this referring to, code for recognizing buttons, controlling motors or handling encoders? what doesn't make sense?

assuming the encoder can be processed at relatively slow motor speed and without a lot of other processing, the rising edge of one encoder input needs to be detected and the state of the other encoder input used to determine whether to inc/decrement the encoder position

It refers to handling the encoder. I cant find any examples of code that doesn't use additional functions. So I am stuck on how to use ISR without creating another void function.

think about

    byte encInp1 = digitalRead (PinEnc1);
    if (encInp1Lst != encInp1)  {
        encInp1Lst = encInp1)
        ...
    }

sorry what does this part mean?

sorry. it's a variable like encInp1 that is set to the previous value of enc1Inp so that the condition detects when it changes.

byte encInp1Lst;

it should be initialized when both encInp1 and encInp2 are configured

    pinMode (encInp2, INPUT);
    pinMode (encInp1, INPUT);
    encInp1Lst = digitalRead (encInp1);

hope you're able to put the pieces together

sorry, I am confused about the logic behind this and how this would essentially allow to count amnount of revolutions and get the motor to stop once a certain number of revs has been met.

sounds like you're not familiar with an encoder. read Quadrature Encoder Overview

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