Can anyone check my coding?

So my circuit includes L298P motor shield, arduino uno r3, 2 dc motor , bluethooth module and a Lipo battery
apparently I cant run my motor with the coding i have right now
I wanted to control the dc motor with the bluethooth module
Appreciate in advance

#include <SoftwareSerial.h>
char val;
int MOTOR1_DIRECTION_PIN = 12;
int M1 = 9;
int MOTOR2_DIRECTION_PIN = 13;
int M2 = 8;
int MOTOR1_SPEED_PIN = 10;
int MOTOR2_SPEED_PIN = 11;
int spd= 255;

void setup() {
pinMode(MOTOR1_DIRECTION_PIN,OUTPUT);
pinMode(MOTOR2_DIRECTION_PIN,OUTPUT);
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);
pinMode(MOTOR1_SPEED_PIN, OUTPUT);
pinMode(MOTOR2_SPEED_PIN, OUTPUT);
}

void loop() {
if(Serial.available()>0){
val = Serial.read();
Serial.println(val);
}
if(val=='F'){
digitalWrite(MOTOR1_DIRECTION_PIN,LOW);
digitalWrite(M1,HIGH);
analogWrite(MOTOR1_SPEED_PIN, spd);
digitalWrite(MOTOR2_DIRECTION_PIN,HIGH);
digitalWrite(M2,LOW);
analogWrite(MOTOR2_SPEED_PIN, spd);

}
else if(val=='B'){
  digitalWrite(MOTOR1_DIRECTION_PIN,HIGH);
  digitalWrite(M1,LOW);
  analogWrite(MOTOR1_SPEED_PIN, spd);
  digitalWrite(MOTOR2_DIRECTION_PIN,LOW);
  digitalWrite(M2,HIGH);
  analogWrite(MOTOR2_SPEED_PIN, spd);
}
else if(val=='L'){
  digitalWrite(MOTOR1_DIRECTION_PIN,HIGH);
  digitalWrite(M1,LOW);
  analogWrite(MOTOR1_SPEED_PIN, spd);
  digitalWrite(MOTOR2_DIRECTION_PIN,HIGH);
  digitalWrite(M2,LOW);
  analogWrite(MOTOR2_SPEED_PIN, spd);
}
else if(val=='R'){
  digitalWrite(MOTOR1_DIRECTION_PIN,LOW);
  digitalWrite(M1,HIGH);
  analogWrite(MOTOR1_SPEED_PIN, spd);
  digitalWrite(MOTOR2_DIRECTION_PIN,LOW);
  digitalWrite(M2,HIGH);
  analogWrite(MOTOR2_SPEED_PIN, spd);
}
else if(val=='S'){
  digitalWrite(MOTOR1_DIRECTION_PIN,LOW);
  digitalWrite(M1,LOW);
  analogWrite(MOTOR1_SPEED_PIN, 0);
  digitalWrite(MOTOR2_DIRECTION_PIN,LOW);
  digitalWrite(M2,LOW);
  analogWrite(MOTOR2_SPEED_PIN, 0);
}

}

void loop() {
    if(Serial.available()>0){
        val = Serial.read();
        Serial.println(val);
    }
    if(val=='F'){

have to hurry. but one problem is processing val regardless of receiving a new input

might consifer

void loop() {
    if (  ! Serial.available()>0) 
        return

    val = Serial.read();
    Serial.println(val);

    if(val=='F'){

while MOTOR_DIRECTION_PIN is certainly descriptive, it's too long. what about PinDIrM1?

and what are M1 and M2?

M1 and M2 is the counterclockwise rotation ? from what I saw in some explanation of using L298P motor driver.

I've tried it still doesnt work

I don't see SoftwareSerial being configured or used at all. I also don't see Serial being configured. What pins are the Bluetooth module connected to?!?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.