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.
#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.