The buttons is connected to pin2 and pin7. I want the motor to move forward when button 1 is pressed down and backwards when button 2 is pressed down. It all works fine but the motor runs slow and is not getting the full 12v, only around 5-6 volts. When removing one button in the code the motors runs at full speed, until stop.
My thought is that it has something to do with the "else" saying that motor should go down to speed 0 if the button is not pressed, but I am new to code so not sure, just a guess. It is almost as it brakes the motor and interfere with the speed (+400) and (-400) command.
/*
* VNH3SP30 motor driver library - demo for a single motor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Created 2 June 2019 Bart Mellink
*/
#include <VNH3SP30.h>
VNH3SP30 Motor1; // define control object for 1 motor
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 7; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
// motor pins
#define M1_PWM 3 // pwm pin motor
#define M1_INA 4 // control pin INA
#define M1_INB 5 // control pin INB
#define M1_DIAG 6 // diagnose pins (combined DIAGA/ENA and DIAGB/ENB)
#define M1_CS A0 // current sense pin
void setup() {
Motor1.begin(M1_PWM, M1_INA, M1_INB, M1_DIAG, M1_CS); // Motor 1 object connected through specified pins
Serial.begin(115200);
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);
// check if the pushbutton 1 is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Turn motor on backward:
Motor1.setSpeed(-400);
} else {
// Turn motor off:
Motor1.setSpeed(0); }
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin2);
// check if the pushbutton 2 is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Turn motor on forward:
Motor1.setSpeed(+400);
} else {
// Turn motor off:
Motor1.setSpeed(0); }
}
slipstick:
How are the buttons connected? Do you have pull-down resistors between the pin and GND to make certain that they are truly LOW when not pressed?
Steve
Yes 10K resistor connected between button and GND.
The program gives conflicting commands to the motor.
If one button is HIGH, the program says to turn one direction, and if the other button is LOW, the program says to stop.
What do you think the motor controller supposed to do in that situation? What happens if both buttons are pushed, and the motor is told to go in both directions?
You need to decide exactly what the buttons are supposed to do, and program accordingly. There are four possible states of two buttons, and they all need to be considered.
I think I got it. Now it outputs +12v and -12v, but still 2.6v when nothing is pressed. Is that normal?
/*
* VNH3SP30 motor driver library - demo for a single motor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Created 2 June 2019 Bart Mellink
*/
#include <VNH3SP30.h>
VNH3SP30 Motor1; // define control object for 1 motor
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 7; // the number of the pushbutton pin
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
// motor pins
#define M1_PWM 3 // pwm pin motor
#define M1_INA 4 // control pin INA
#define M1_INB 5 // control pin INB
#define M1_DIAG 6 // diagnose pins (combined DIAGA/ENA and DIAGB/ENB)
#define M1_CS A0 // current sense pin
void setup() {
Motor1.begin(M1_PWM, M1_INA, M1_INB, M1_DIAG, M1_CS); // Motor 1 object connected through specified pins
Serial.begin(115200);
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton 2 is pressed. If it is, the buttonState is HIGH:
if ((buttonState2 == HIGH) && (buttonState1 == LOW)) {
// Turn motor on forward:
Motor1.setSpeed(+400);
}
// check if the pushbutton 1 is pressed. If it is, the buttonState is HIGH:
if ((buttonState1 == HIGH) && (buttonState2 == LOW)) {
// Turn motor on backward:
Motor1.setSpeed(-400);
}
// check if nonn of the buttons is pressed
if ((buttonState2 == LOW) && (buttonState1 == LOW)) {
// Turn motor off:
Motor1.setSpeed(0);
}
}
Have you tried running the example program to see if your driver and actuator are OK? Set the actuator in middle position first, I changed the delays to 1 second so (hopefully) the actuator won't run to the end of travel.
/*
* VNH3SP30 motor driver library - demo for a single motor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Created 2 June 2019 Bart Mellink
*/
#include <VNH3SP30.h>
VNH3SP30 Motor1; // define control object for 1 motor
// motor pins
#define M1_PWM 3 // pwm pin motor
#define M1_INA 4 // control pin INA
#define M1_INB 5 // control pin INB
#define M1_DIAG 6 // diagnose pins (combined DIAGA/ENA and DIAGB/ENB)
#define M1_CS A0 // current sense pin
void setup() {
Motor1.begin(M1_PWM, M1_INA, M1_INB, M1_DIAG, M1_CS); // Motor 1 object connected through specified pins
Serial.begin(115200);
}
void loop() {
Serial.println("3/4 speed forward");
Motor1.setSpeed(300); // motor 3/4-speed "forward"
delay(1000); // wait for 2 seconds
Serial.print("Current="); Serial.println(Motor1.motorCurrent());
Serial.println("Motor stop (coast)");
Motor1.setSpeed(0); // motor stop (coasting)
delay(1000); // wait for 2 seconds
Serial.print("Current at stop="); Serial.println(Motor1.motorCurrent());
Serial.println("Half speed backward");
Motor1.setSpeed(-200); // motor half-speed "backward"
delay(1000); // wait for 2 seconds
Serial.println("Motor stop (coast)");
Motor1.setSpeed(0); // motor stop
delay(1000); // wait for 2 seconds
Serial.println("Full speed backward");
Motor1.setSpeed(-400); // motor full-speed "backward"
delay(1000); // wait for 2 seconds
Serial.print("Current="); Serial.println(Motor1.motorCurrent());
Serial.println("Brake at 3/4 power");
Motor1.brake(300); // motor brake at 3/4 power
delay(10);
Serial.print("Current during brake="); Serial.println(Motor1.motorCurrent());
delay(4000); // wait for 4 seconds
Serial.print("Current after brake="); Serial.println(Motor1.motorCurrent());
}
Don't try to instantly switch between rotating forward and backward. Doing so causes an enormous current draw and can damage the motor and motor driver.
JCA34F:
Have you tried running the example program to see if your driver and actuator are OK? Set the actuator in middle position first, I changed the delays to 1 second so (hopefully) the actuator won't run to the end of travel.
/*
VNH3SP30 motor driver library - demo for a single motor
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Created 2 June 2019 Bart Mellink
*/
#include <VNH3SP30.h>
VNH3SP30 Motor1; // define control object for 1 motor
// motor pins #define M1_PWM 3 // pwm pin motor #define M1_INA 4 // control pin INA #define M1_INB 5 // control pin INB #define M1_DIAG 6 // diagnose pins (combined DIAGA/ENA and DIAGB/ENB) #define M1_CS A0 // current sense pin
void setup() {
Motor1.begin(M1_PWM, M1_INA, M1_INB, M1_DIAG, M1_CS); // Motor 1 object connected through specified pins
Serial.begin(115200);
}
void loop() {
Serial.println("3/4 speed forward");
Motor1.setSpeed(300); // motor 3/4-speed "forward"
delay(1000); // wait for 2 seconds
Serial.print("Current="); Serial.println(Motor1.motorCurrent());
Serial.println("Motor stop (coast)");
Motor1.setSpeed(0); // motor stop (coasting)
delay(1000); // wait for 2 seconds
Serial.print("Current at stop="); Serial.println(Motor1.motorCurrent());
Serial.println("Half speed backward");
Motor1.setSpeed(-200); // motor half-speed "backward"
delay(1000); // wait for 2 seconds
Serial.println("Motor stop (coast)");
Motor1.setSpeed(0); // motor stop
delay(1000); // wait for 2 seconds
Serial.println("Full speed backward");
Motor1.setSpeed(-400); // motor full-speed "backward"
delay(1000); // wait for 2 seconds
Serial.print("Current="); Serial.println(Motor1.motorCurrent());
Serial.println("Brake at 3/4 power");
Motor1.brake(300); // motor brake at 3/4 power
delay(10);
Serial.print("Current during brake="); Serial.println(Motor1.motorCurrent());
delay(4000); // wait for 4 seconds
Serial.print("Current after brake="); Serial.println(Motor1.motorCurrent());
}
Yes it works great, but same 3V to the motor after it is set to:
Motor1.setSpeed(0); // motor stop
delay(1000); // wait for 2 seconds
jremington:
Don't try to instantly switch between rotating forward and backward. Doing so causes an enormous current draw and can damage the motor and motor driver.
Thanks for the tip, so adding delay in the beginning of each start I guess will solve that?
Something like this?
/*
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton 1 is pressed ad if: run motor forward
if ((buttonState1 == HIGH) && (buttonState2 == LOW)) {
// Brake Motor
delay(10);
//Turn motor on forward:
Motor1.setSpeed(+400);
}
// check if the pushbutton 2 is pressed ad if: run motor backward
if ((buttonState2 == HIGH) && (buttonState1 == LOW)) {
// Brake Motor
delay(10);
//Turn motor on backward:
Motor1.setSpeed(-400);
}
// check if both buttons is pressed and if: forward motor 2 sec
if ((buttonState2 == HIGH) && (buttonState1 == HIGH)) {
// Brake Motor
delay(10);
// motor forward
Motor1.setSpeed(+400);
// wait for 2 seconds
delay(2000);
}
// check if nothig is pressed and if: Stop motor
if ((buttonState2 == LOW) && (buttonState1 == LOW)) {
// Turn motor off:
Motor1.setSpeed(0);
}
}