I want to control a linear actuator 12v with my UNO and motor controller NH3SP30: https://github.com/bmellink/VNH3SP30 , I have connected the buttons using this guide: https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
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); }
}