Arduino to control DC motor

Discussion in 'Embedded Systems and Microcontrollers' started by J7NF, Apr 16, 2016.

  1. J7NF

    Thread Starter New Member

    Mar 12, 2016
    18
    0
    Hello everyone, I am new to arduino and I need your help in a code. I need to control a DC motor direction by 2 pushbutton connected to arduino and H-bridge. where I want to press and hold 3 second on SW 1 and release, the motor should turn in one direction and if the same for SW 2, the motor should turn in the opposite direction. My code is the following:
    const int buttonPin1 = 2;
    const int buttonPin2= 4;
    const int ledPin2 = 7;
    const int ledPin1 = 5;
    // variables will change:
    int buttonState1 = 0;
    int buttonState2 = 0;
    void setup() {
    pinMode(ledPin1, OUTPUT); // direction 1 for motor
    pinMode(ledPin2, OUTPUT); // direction 2 for motor
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
    }
    void loop() {
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);

    if (buttonState1 == HIGH){
    delay(3000);

    digitalWrite(ledPin1, HIGH);
    delay (10000);
    }
    if (buttonState2 == HIGH) {
    delay(3000);
    digitalWrite(ledPin2, HIGH);
    delay(10000);}
    }

    The problem in this code is that when ledPin1 is on , I cannot access ledPin2 only after 10 s which is in delay(10000), I take it as arbitrary value to keep the LED on for a while. so any help please to fix the issue where I want to switch the direction of the motor and remain in this direction until another change as specified above, without waiting .
     
  2. bertz

    Member

    Nov 11, 2013
    238
    31
    You'll get a better response if you post your question on this site.
     
  3. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    You just need to sample the switch for 3 seconds and only move when its been held down for your desired time..
    This should be what you need here.. just adjust for your required hold down time..
    https://www.arduino.cc/en/Tutorial/Debounce

    Simply google "arduino debounce" for more examples/videos,etc...
     
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,812
    834
    The Arduino had several pins which can be used as interrupts. Thus, your normal code goes about its merry way, until a pin changes state. Then an "Interrupt Service Routine) executes.

    This way, your main routine may look extremely simple

    void main() {
    If (turnCW) do that
    If (turnCCW) do this other thing
    }

    Everything else is taken care of in two functions defined as ISRs in the setup function.

    Let's look at pin 1. If it goes high, calculate the time 3 seconds hence and return. If it goes low, and the time has elapsed, set a flag to tell the main program to do something different, like turn the motor one way. If it goes low, and 3 seconds has not elapsed, do nothing and return.

    Do the same for button 2, only set a different flag to turn the motor the other way (and reset the first flag. Like wise for the first function)
     
  5. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,812
    834
    Here is some sample UNTESTED code, that shows how you can use interrupts.
    Code (C):
    1.  
    2. #define Button1Pin 2
    3. #define Buttton2Pin 3
    4.  
    5. boolean oneway = false, otherway = false;
    6.  
    7. unsigned long EndTime = 0;
    8.  
    9. void setup() {
    10.   // put your setup code here, to run once:
    11.   attachInterrupt(digitalPinToInterrupt(Button1Pin), Button1, CHANGE);
    12.   attachInterrupt(digitalPinToInterrupt(Button2Pin), Button2, CHANGE);
    13. }
    14.  
    15. void Button1() {
    16.   static int State, lastState
    17.   static unsigned long EndTime1
    18.   unsigned long Time = millis();
    19.   int State = digitalRead(Button1Pin);
    20.   if (State) EndTime1 = millis() + 3000ul;
    21.   if (!State && Time >= EndTime1) oneway = true;
    22.   return
    23. }
    24.  
    25. void Button2() {
    26.   static int State, lastState
    27.   static unsigned long EndTime2
    28.   unsigned long Time = millis();
    29.   int State = digitalRead(Button2Pin);
    30.   if (State) EndTime2 = millis() + 3000ul;
    31.   if (!State && Time >= EndTime1) otherway = true;
    32.   return
    33. }
    34.  
    35. void loop() {
    36.   if ( oneway ) turn the motor one way
    37.   if (otherway) turn the motor the other way
    38.  
    39. }
    40. {/CODE]
     
Loading...