Hi. My problem is Afmotor continiously is rotating. After starting rotation of the motor , 433 Mhz receiver stopped to get message from transmitter.
I use Gyroscope with transmitter.
How can i fix it?. please help me!
//RECEIVER
#include <VirtualWire.h>
#include <AFMotor.h>
char*mesaj;
int a;
double ort;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup() {
Serial.begin(9600);
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
vw_set_rx_pin(6);
vw_set_ptt_inverted(true);
vw_setup(4000);
vw_rx_start();
}
void loop() {
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) //
{
Serial.print(buf[0]);Serial.print(" ");
Serial.print(buf[1]); Serial.print(" ");
Serial.print(buf[2]); Serial.print("\n");
int x=buf[0];
int y=buf[1];
int z=buf[2];
if (x>300 && x<359) //back
{
motor1.run(FORWARD);
// motor2.run(BACKWARD);
// motor3.run(BACKWARD);
// motor4.run(BACKWARD);
Serial.println("");
}
else //back
{
motor1.run(BACKWARD);
// motor2.run(BACKWARD);
// motor3.run(BACKWARD);
// motor4.run(BACKWARD);
Serial.println("geri");
}
}
}
//TRANSMITTER
//MPU-6050(GY-521)
#include <VirtualWire.h> // RF modül için gerekli Arduino kütüphanesi
const char *mesaj; // Gönderilecek mesajın yazıldığı değişken
#include<Wire.h>
const int MPU_adres=0x68;
int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
void setup(){
//gyro settings
Wire.begin();
Wire.beginTransmission(MPU_adres);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
//Transmitter settings
vw_set_ptt_inverted(true);
vw_set_tx_pin(12); // Verici modülün data pin bağlantısı
vw_setup(4000);
Serial.begin(9600);
}
void msg(const char *msg){
Serial.println(mesaj); //Oluşturduğumuz mesaj değişkenini serial ekranda yazdırdık.
vw_send((uint8_t *)msg, strlen(msg)); //Mesaj değişkenini RF 433 ile alıcı modüle gönderiyoruz.
vw_wait_tx();
}
void loop(){
Wire.beginTransmission(MPU_adres);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_adres,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
int msg[3];
msg[0] = x;
msg[1] = y;
msg[3] = y;
vw_send((uint8_t *)msg, 3);
vw_wait_tx(); // Wait until the whole message is gone
Serial.println(String("x:") + x + String(" y:") + y+ String(" z:") +z);
// delay(300);
}