L9110 with arduino problem

HELLO,

I have a dual motor driver L9110, arduino nano and a motor. I want to press a button one time to start the motor in a certain direction for 2 seconds then press the button again to reverse the direction of the motor. i searched for the Debounce code and copied it to IDE then modified it.

here is the problem:
at the first press the motor goes for a direction but it does not stop after 2 seconds it just keep running until the second press, but at the second press it changes the direction and stop after 2 seconds. WHAT IS THE PROBLEM HERE????

the code:


#define button 7
#define Mspeed 9
#define Mdirection 2
#define MXspd 200
#define MMspd 0

int bstate;
int lastButtonState = LOW;
int Mdir = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
pinMode (button, INPUT);
pinMode (Mspeed, OUTPUT);
pinMode (Mdirection, OUTPUT);

}

void loop() {
int reading = digitalRead(button);

if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != bstate) {
bstate = reading;

if (bstate == HIGH) {
Mdir = !Mdir;
digitalWrite (Mdirection, Mdir);
digitalWrite (Mspeed, MXspd);
delay (2000);
digitalWrite (Mspeed, MMspd);
}
}
}
lastButtonState = reading;
}

Is th…er…e an…y on…e on ea…rt…h over.

You should not be using delay() in that sort of program.

The timing of the motor is separate from the timing for debouncing but will be similar. When you press the button to start the motor you need to save the value of millis() as well as starting the motor. Then your code will keep checking and stop the motor when 2 seconds has elapsed. Something like

if (currentMillis - motorStartMillis >= 2000) {
  // code to make motor stop
}

You also need a variable to keep track of the direction and each time there is a valid button press that should change from 'F' to 'R' or from 'R' to 'F' as the case may be.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R

Thank you for your answer.

i tried your method, but the direction problem still the same
i think the problem is in the direction's code. i need a piece of code that store the value then reverse it in the next press of the button.

can you help me in that??

the code:

#define button 7
#define Mspeed 9
#define Mdirection 2

int bstate;
int lastButtonState = LOW;
int mdir;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long motorstart = 0;

void setup() {
pinMode (button, INPUT);
pinMode (Mspeed, OUTPUT);
pinMode (Mdirection, OUTPUT);
}

void loop() {
int reading = digitalRead(button);

if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != bstate) {
bstate = reading;
if (bstate == HIGH) {
mdir = !mdir;
motorstart = millis();
digitalWrite (Mspeed, HIGH);
digitalWrite (Mdirection, mdir);

}
}
}
lastButtonState = reading;
if (millis() - motorstart >= 2000){
digitalWrite (Mspeed, LOW);
}
}

Thanks Robin2 for your help.

I used millis to stop the motor instead of delay, and the direction problem was coming from misunderstanding the configuration of L9110 driver. now it works fine after i added this line ( spd = !spd; )

the final code:

#define button 7
#define Mspeed 9
#define Mdirection 2

int bstate;
int dir = LOW;
int spd = HIGH;
int lastButtonState = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long motorstart = 0;

void setup() {
pinMode (button, INPUT);
pinMode (Mspeed, OUTPUT);
pinMode (Mdirection, OUTPUT);
}

void loop() {
int reading = digitalRead(button);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != bstate) {
bstate = reading;
if (bstate == HIGH) {
motorstart = millis();
spd = !spd;
dir = !dir;
digitalWrite (Mspeed, spd);
digitalWrite (Mdirection, dir);
}
}
}

lastButtonState = reading;
if (millis() - motorstart >= 2000){
digitalWrite (Mspeed, LOW);
digitalWrite (Mdirection, LOW);
}
}