Combining code (servo motor & l298N driver)

I have two codes. They work fine independently. I want to combine the two codes. As I have combined them, only code 1 responds, not the code 2. Im a newbie and could not figure it out. Would appreciate any help!

#define pin1 2 // motor #1 +
#define pin2 3 // motor #1 โ€“
#define pw1 7 // motor #1 pwm
#define pin3 4 // motor #2 +
#define pin4 5 // motor #2 โ€“
#define pw2 6 // motor #2 pwm
#include <Servo.h>
#include <SoftwareSerial.h>


Servo myservo;
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);


Servo ser; // create servo object to control a servo

int poser = 90; // initial position of servo

int value; // initial value of input


void setup() {
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pw1, OUTPUT);
  pinMode(pw2, OUTPUT);
  Serial.begin(9600);
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  analogWrite(pw1, 50);
  analogWrite(pw2, 50);
  myservo.attach(9);
  bluetooth.begin(9600);



}

void loop() {
//CODE 1
  if (bluetooth.available() >= 2 )
  {
    unsigned int a = bluetooth.read();
    unsigned int b = bluetooth.read();
    unsigned int val = (b * 256) + a;


    if (val == 200) // motor #1 stop
    {
      digitalWrite(pin1, LOW);
      digitalWrite(pin2, LOW);
    }
    else if (val == 300) // motor #1 forward
    {
      digitalWrite(pin1, HIGH);
      digitalWrite(pin2, LOW);
    }

    else if (val == 500) // motor #2 stop
    {
      digitalWrite(pin3, LOW);
      digitalWrite(pin4, LOW);
    }
    else if (val == 600) // motor #2 forward
    {
      digitalWrite(pin3, HIGH);
      digitalWrite(pin4, LOW);
    }
    else if (val >= 1000 && val <= 1255)
    {
      analogWrite (pw1, val - 1000);
    }
    else if (val >= 2000 && val <= 2255)
    {
      analogWrite (pw2, val - 2000);
      {

//CODE 2
  if(bluetooth.available()> 0 ) // receive number from bluetooth
  {
    int servopos = bluetooth.read(); // save the received number to servopos
    Serial.println(servopos); // serial print servopos current number received from bluetooth
    myservo.write(servopos); // roate the servo the angle received from the android app
  }
}  
}        
}        
}        



check your braces

#define pin1 2 // motor #1 +
#define pin2 3 // motor #1 ฮ“ร‡รด
#define pw1 7 // motor #1 pwm
#define pin3 4 // motor #2 +
#define pin4 5 // motor #2 ฮ“ร‡รด
#define pw2 6 // motor #2 pwm
#include <Servo.h>
#include <SoftwareSerial.h>
Servo myservo;
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
Servo ser; // create servo object to control a servo
int poser = 90; // initial position of servo
int value; // initial value of input
void setup() {
    pinMode(pin1, OUTPUT);
    pinMode(pin2, OUTPUT);
    pinMode(pin3, OUTPUT);
    pinMode(pin4, OUTPUT);
    pinMode(pw1, OUTPUT);
    pinMode(pw2, OUTPUT);
    Serial.begin(9600);
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, LOW);
    analogWrite(pw1, 50);
    analogWrite(pw2, 50);
    myservo.attach(9);
    bluetooth.begin(9600);
}

void loop() {
    //CODE 1
    if (bluetooth.available() >= 2 )
    {
        unsigned int a = bluetooth.read();
        unsigned int b = bluetooth.read();
        unsigned int val = (b * 256) + a;
        if (val == 200) // motor #1 stop
        {
            digitalWrite(pin1, LOW);
            digitalWrite(pin2, LOW);
        }
        else if (val == 300) // motor #1 forward
        {
            digitalWrite(pin1, HIGH);
            digitalWrite(pin2, LOW);
        }
        else if (val == 500) // motor #2 stop
        {
            digitalWrite(pin3, LOW);
            digitalWrite(pin4, LOW);
        }
        else if (val == 600) // motor #2 forward
        {
            digitalWrite(pin3, HIGH);
            digitalWrite(pin4, LOW);
        }
        else if (val >= 1000 && val <= 1255)
        {
            analogWrite (pw1, val - 1000);
        }
        else if (val >= 2000 && val <= 2255)
        {
            analogWrite (pw2, val - 2000);
            {
                //CODE 2
                if(bluetooth.available()> 0 ) // receive number from bluetooth
                {
                    int servopos = bluetooth.read(); // save the received number to servopos
                    Serial.println(servopos); // serial print servopos current number received from bluetooth
                    myservo.write(servopos); // roate the servo the angle received from the android app
                }
            }
        }
    }
}

Is pin 7 a PWM pin on your mystery Arduino board?

Good job posting code properly in your first post. Most new members do not bother reading the rules and have to be reminded.

1 Like

yes

Where in the code are you figuring out where (to which of the two programs) the incoming characters are supposed to go?

Seems like CODE 2 will never see anything available as you are reading from the same port earlier.

   //CODE 2
                if(bluetooth.available()> 0 ) // receive number from bluetooth
                {

a7

sorry i meant only the the code 2 works and code 1 doesnt

The way that you read from the software serial port is not very robust. There is no way for it to tell if the data is valid or not. I would suggest that you have a look at Robin2's serial input basics tutorial.

In the code 1, put in a serial print to see what is coming in and being written to the servo.

SERVO

#include <SoftwareSerial.h> // TX RX software library for bluetooth

#include <Servo.h> // servo library 
Servo myservo; // servo name

int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  myservo.attach(9); // attach servo signal wire to pin 9
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()> 0 ) // receive number from bluetooth
  {
    int servopos = bluetooth.read(); // save the received number to servopos
    Serial.println(servopos); // serial print servopos current number received from bluetooth
    myservo.write(servopos); // roate the servo the angle received from the android app
  }


}

L298N DRIVER


#define pin1 2 // motor #1 +
#define pin2 3 // motor #1 โ€“
#define pw1 7 // motor #1 pwm
#define pin3 4 // motor #2 +
#define pin4 5 // motor #2 โ€“
#define pw2 6 // motor #2 pwm
void setup() {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pw1, OUTPUT);
pinMode(pw2, OUTPUT);
Serial.begin(9600);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
analogWrite(pw1, 50);
analogWrite(pw2, 50);

}

void loop() {

if (Serial.available() >= 2 )
{
unsigned int a = Serial.read();
unsigned int b = Serial.read();
unsigned int val = (b * 256) + a;

if (val == 100) // motor 1 reverse
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);

}
else if (val == 200) // motor #1 stop
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
else if (val == 300) // motor #1 forward
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
}
else if (val == 400) // motor #2 reverse
{
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
}
else if (val == 500) // motor #2 stop
{
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
}
else if (val == 600) // motor #2 forward
{
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
}
else if (val >= 1000 && val <= 1255)
{
analogWrite (pw1, val - 1000);
}
else if (val >= 2000 && val <= 2255)
{
analogWrite (pw2, val - 2000);
}
}
}

Does the second sketch in post #10 function?

What are you typing in the serial input area to get it to work?

256 * b + a looks like you are reading bytes meant to make up an integer.

a7

yes it does.i've copied it from ARDUINO How To Bluetooth Control 2 DC Motors Speed And Direction With A L298N - YouTube

Serial print some values so you can see what characters end up informing the process.

Do the same thing with the bluetooth characters received.

a7

is it like this?

#define pin1 2 // motor #1 +
#define pin2 3 // motor #1 ฮ“ร‡รด
#define pw1 7 // motor #1 pwm
#define pin3 4 // motor #2 +
#define pin4 5 // motor #2 ฮ“ร‡รด
#define pw2 6 // motor #2 pwm
#include <Servo.h>
#include <SoftwareSerial.h>
Servo myservo;
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
Servo ser; // create servo object to control a servo
int poser = 90; // initial position of servo
int value; // initial value of input
void setup() {
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pw1, OUTPUT);
  pinMode(pw2, OUTPUT);
  Serial.begin(9600);
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  analogWrite(pw1, 50);
  analogWrite(pw2, 50);
  myservo.attach(9);
  bluetooth.begin(9600);
}

void loop() {
  //CODE 1
  if (bluetooth.available() >= 2 )
  {
    unsigned int a = bluetooth.read();
    unsigned int b = bluetooth.read();
    unsigned int val = (b * 256) + a;
    if (val == 200) // motor #1 stop
    {
      digitalWrite(pin1, LOW);
      digitalWrite(pin2, LOW);
    }
    else if (val == 300) // motor #1 forward
    {
      digitalWrite(pin1, HIGH);
      digitalWrite(pin2, LOW);
    }
    else if (val == 500) // motor #2 stop
    {
      digitalWrite(pin3, LOW);
      digitalWrite(pin4, LOW);
    }
    else if (val == 600) // motor #2 forward
    {
      digitalWrite(pin3, HIGH);
      digitalWrite(pin4, LOW);
    }
    else if (val >= 1000 && val <= 1255)
    {
      analogWrite (pw1, val - 1000);
    }
    else if (val >= 2000 && val <= 2255)
    {
      analogWrite (pw2, val - 2000);
      {
        int servopos = bluetooth.read(); // save the received number to servopos
        Serial.println(servopos); // serial print servopos current number received from bluetooth
        myservo.write(servopos); // roate the servo the angle received from the android app
      }
    }
  }
}

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