Arduino to control DC motor

Thread Starter

J7NF

Joined Mar 12, 2016
18
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 .
 

bertz

Joined Nov 11, 2013
327
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 .
You'll get a better response if you post your question on this site.
 

djsfantasi

Joined Apr 11, 2010
5,958
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)
 

djsfantasi

Joined Apr 11, 2010
5,958
Here is some sample UNTESTED code, that shows how you can use interrupts.
C:
#define Button1Pin 2
#define Buttton2Pin 3

boolean oneway = false, otherway = false;

unsigned long EndTime = 0;

void setup() {
  // put your setup code here, to run once:
  attachInterrupt(digitalPinToInterrupt(Button1Pin), Button1, CHANGE);
  attachInterrupt(digitalPinToInterrupt(Button2Pin), Button2, CHANGE);
}

void Button1() {
  static int State, lastState
  static unsigned long EndTime1
  unsigned long Time = millis();
  int State = digitalRead(Button1Pin);
  if (State) EndTime1 = millis() + 3000ul;
  if (!State && Time >= EndTime1) oneway = true;
  return
}

void Button2() {
  static int State, lastState
  static unsigned long EndTime2
  unsigned long Time = millis();
  int State = digitalRead(Button2Pin);
  if (State) EndTime2 = millis() + 3000ul;
  if (!State && Time >= EndTime1) otherway = true;
  return
}

void loop() {
  if ( oneway ) turn the motor one way
  if (otherway) turn the motor the other way

}
{/CODE]
 
Top