Hi all
I’m making RC car, controlled by accelometer. I sent sensor value to slave arduino with bluetooth and I checked the value with my Serial monitor. But motor just don’t move at all. I used MPU 6050, L298N and HC-06
master code:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
SoftwareSerial btSerial(8,9);
const int MPU=0x68;
int AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
void get6050();
int xpos;
int ypos;
void setup()
{
Serial.begin(9600);
btSerial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
}
void loop()
{
get6050();
xpos=map(AcX,-16383,16383,80,255);
ypos=map(AcY,-16383,16383,0,180);
btSerial.println(xpos);
Serial.print("xpos=");
Serial.println(xpos);
delay(100);
}
void get6050(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
}
and slave code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(8,9);
int val;
void setup() {
// put your setup code here, to run once:
BTSerial.begin(9600);
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(4,OUTPUT);
}
void loop() {
digitalWrite(2,HIGH);
digitalWrite(4,LOW);
if(BTSerial.available()){
val=BTSerial.read();
Serial.write(val);
analogWrite(3,val);
}
}
code is so simple cuz i try to control one wheel first. pin 3 of slave arduino control the speed of DC motor. When I put those code to each arduino, slave arduino prints right sensor value at Serial monitor but motor doesn’t move and just make a sound like ‘beeeeeeeeeep’. I can control the speed when I use just one arduino and no communication. I need your help to control motor when I use bluetooth communication!