Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why arduino doesn't read data from bluetooth when i use motor declaration?

I add only one line to declare motor, i don’t use this motor in code, just declare, and data from my phone is no longer visible to arduino

This code works. I am sending data from phone and it’s showing in port monitor.

#include <AFMotor.h>
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(11, 10); // RX | TX

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(9600);
}

void loop() {
  if (BTSerial.available())
  {
    char data = BTSerial.read();
    Serial.write(data);
    if (data == '1') {
      Serial.println("Button 1 pressed");
    }
  }
  delay(20);
}

But, when i add a motor declaration line, programm stop working right. Monitor port doesn’t show the msg.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <AFMotor.h>
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(11, 10); // RX | TX
AF_DCMotor motor1(1);

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(9600);
}

void loop() {
  if (BTSerial.available())
  {
    char data = BTSerial.read();
    Serial.write(data);
    if (data == '1') {
      Serial.println("Button 1 pressed");
    }
  }
  delay(20);
}

Does anyone have any ideas how that can be fixed?

>Solution :

Since you didn’t specify, I am going to guess that the libraries you use are:

If this is correct, and you also use an ATmega Arduino, then the problem can probably be explained by this line in the Adafruit Motor Shield Library:

// use PWM from timer2A on PB3 (Arduino pin #11)

It is in the function initPWM1, which is called from the AF_DCMotor constructor when you init motor 1.

So, my suggestion is to either use another motor port, or move the SoftwareSerial library to use some other pins. Having two libraries fighting over a common pin is never going to result in anything good.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading