This is my code…
#define BRAKE 0
#define CW 1
#define CCW 2
#define CS_THRESHOLD 15 // Definition of safety current (Check: "1.3 Monster Shield Example").
//MOTOR 1
#define MOTOR_A1_PIN 7
#define MOTOR_B1_PIN 8
//MOTOR 2
#define MOTOR_A2_PIN 4
#define MOTOR_B2_PIN 9
#define PWM_MOTOR_1 5
#define PWM_MOTOR_2 6
#define CURRENT_SEN_1 A2
#define CURRENT_SEN_2 A3
#define EN_PIN_1 A0
#define EN_PIN_2 A1
#define MOTOR_1 0
#define MOTOR_2 1
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if (sensorValue > 721 && sensorValue < 779){
Serial.println("stred");
digitalWrite(LED_BUILTIN, LOW);
motorGo(MOTOR_1, BRAKE, 0);
}
if (sensorValue < 720){
Serial.println("doleva");
digitalWrite(LED_BUILTIN, HIGH);
motorGo(MOTOR_1, CW, 150);
}
if (sensorValue > 780){
Serial.println("doprava");
digitalWrite(LED_BUILTIN, HIGH);
motorGo(MOTOR_1, CCW, 150);
}
if (sensorValue > 950){
Serial.println("stop");
digitalWrite(LED_BUILTIN, LOW);
motorGo(MOTOR_1, BRAKE, 0);
}
if (sensorValue < 150){
Serial.println("stop");
digitalWrite(LED_BUILTIN, LOW);
motorGo(MOTOR_1, BRAKE, 0);
}
}
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm) //Function that controls the variables: motor(0 ou 1), direction (cw ou ccw) e pwm (entra 0 e 255);
{
if(motor == MOTOR_1)
{
if(direct == CW)
{
digitalWrite(MOTOR_A1_PIN, LOW);
digitalWrite(MOTOR_B1_PIN, HIGH);
}
else if(direct == CCW)
{
digitalWrite(MOTOR_A1_PIN, HIGH);
digitalWrite(MOTOR_B1_PIN, LOW);
}
else
{
digitalWrite(MOTOR_A1_PIN, LOW);
digitalWrite(MOTOR_B1_PIN, LOW);
}
analogWrite(PWM_MOTOR_1, pwm);
}
else if(motor == MOTOR_2)
{
if(direct == CW)
{
digitalWrite(MOTOR_A2_PIN, LOW);
digitalWrite(MOTOR_B2_PIN, HIGH);
}
else if(direct == CCW)
{
digitalWrite(MOTOR_A2_PIN, HIGH);
digitalWrite(MOTOR_B2_PIN, LOW);
}
else
{
digitalWrite(MOTOR_A2_PIN, LOW);
digitalWrite(MOTOR_B2_PIN, LOW);
}
analogWrite(PWM_MOTOR_2, pwm);
}
}
it is really simple and for examle it is supposed to stop if pot value is less than 150 (motor stops at 400-300 , led dims), if it is in between 721 and 779 (works as supposed to, and if it is above 950 (motor slows down but never stops)
The pot is connected to A0
Also i used codes from different finished projects with no success. Also acts on its own