Hi, I am working on a project that uses DC motors, and I need some kind of "fail-safe" condition in my code. I was planning to use CS pin on my VNH2SP30 controller, but readings I get make no sense. I connected CS PIN to A2 of my Arduino Uno. When I start serial monitoring, values frpm A2 pin start at ~700, then it stabilizes at ~360. The problem is values stay at 360 all the time, even if I lock the motor with my hand so it can't spin.
Here is my code:
#define BRAKE 0
#define CW 1
#define CCW 2
//MOTOR 1
#define MOTOR_A1_PIN 11 //INA
#define MOTOR_B1_PIN 10 //INB
#define PWM_MOTOR_1 9 //PWM
#define CURRENT_SEN_1 A2 //CS
#define EN_PIN_1 A0 //EN
#define MOTOR_1 0
#define CS_THRESHOLD 15
const int buttonPin = 4; // the number of the pushbutton pin
const int buttonPin1 = 3;
int buttonState = LOW; // variable for reading the pushbutton status
int buttonState1 = LOW;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(MOTOR_A1_PIN, OUTPUT);
pinMode(MOTOR_B1_PIN, OUTPUT);
pinMode(PWM_MOTOR_1, OUTPUT);
pinMode(CURRENT_SEN_1, INPUT);
pinMode(EN_PIN_1, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
if (buttonState == HIGH)
{
digitalWrite(EN_PIN_1, HIGH);
digitalWrite(MOTOR_A1_PIN, LOW);
digitalWrite(MOTOR_B1_PIN, HIGH);
analogWrite(PWM_MOTOR_1, 50);
delay(100);
if (analogRead(CURRENT_SEN_1) > CS_THRESHOLD)
{
Serial.println(analogRead(CURRENT_SEN_1));
}
}
else if (buttonState1 == HIGH)
{
digitalWrite(EN_PIN_1, HIGH);
digitalWrite(MOTOR_A1_PIN, HIGH);
digitalWrite(MOTOR_B1_PIN, LOW);
analogWrite(PWM_MOTOR_1, 130);
if (analogRead(CURRENT_SEN_1) > CS_THRESHOLD)
{
Serial.println(analogRead(CURRENT_SEN_1));
delay(100);
}
}
else if (buttonState == LOW and buttonState1 == LOW)
{
digitalWrite(MOTOR_A1_PIN, LOW); //triba li ovde pwm??
digitalWrite(MOTOR_B1_PIN, LOW);
}
else if (buttonState == HIGH and buttonState1 == HIGH)
{
digitalWrite(MOTOR_A1_PIN, LOW);
digitalWrite(MOTOR_B1_PIN, LOW);
}
else
{
digitalWrite(MOTOR_A1_PIN, LOW);
digitalWrite(MOTOR_B1_PIN, LOW);
}
}