hello, i've been working on a project for my school and now im stuck in a code. I have to controll the servo motor by using the accelerometer mpu6050. i've writen a code master and slave but it doesnt work. The servo motor is like stuck when the 2 hc-modules connect. Can someone help me check the code to see if there is something wrong?
//MASTER
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 mpu;
int ax, ay, az;
int16_t gx, gy, gz;
#include <SoftwareSerial.h>
#define BT_SERIAL_TX 4
#define BT_SERIAL_RX 3
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);
int servoValue1;
int gyro_value = 0;
void setup()
{
Wire.begin();
mpu.initialize();
Serial.begin(9600);
BluetoothSerial.begin(9600);
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
gyro_value = map(ax, 0, 170, 0, 9);
switch (gyro_value) {
case 0: BluetoothSerial.print("0"); break;
case 1: BluetoothSerial.print("1"); break;
case 2: BluetoothSerial.print("2"); break;
case 3: BluetoothSerial.print("3"); break;
case 4: BluetoothSerial.print("4"); break;
case 5: BluetoothSerial.print("5"); break;
case 6: BluetoothSerial.print("6"); break;
case 7: BluetoothSerial.print("7"); break;
case 8: BluetoothSerial.print("8"); break;
case 9: BluetoothSerial.print("9"); break;
default: BluetoothSerial.print("0"); break;
}
delay(100);
}
//SLAVE
#include <SoftwareSerial.h>
#define BT_SERIAL_TX 4
#define BT_SERIAL_RX 3
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);
#include <Servo.h>
Servo myservo;
char servo_val = ' ';
void setup()
{
Serial.begin(9600);
BluetoothSerial.begin(9600);
myservo.attach(9);
}
void loop()
{
if (BluetoothSerial.available()>0){
servo_val = BluetoothSerial.read();
int servo_deger = servo_val - '0';
servo_deger=servo_deger*20;
if(servo_deger>180){servo_deger=180;}
myservo.write(servo_deger);
}
}