Hi everyone, I have a simple question for you. I want to build a RC car controlled by PS3 gamepad with usb host shield to the arduino uno and mpu6050 gyro sensor for the move the camera on the RC car at the transmitter side. At the same time my RC car should collect datas from MQ2 sensor and recieves that data to transmitter side. I couldn't implement Robin2's ackPayload method but I tried radio.startListening and radio.stopListening. I want to control my rc car with ps3 gamepad and change the camera angle with the mpu6050 sensor and I successfully done it with one way communication but I cant recieve sensor data at the reciever side on the RC car. Please guide me how to do that. (When I add verici.stop listening at the end of the transmitter code, servo at the reciever side not moving the camera and this is where the fun beggins)
Here is the Transmitter code:
#include <PS3BT.h>
#include <usbhub.h>
#include <SPI.h>
#include "RF24.h"
#include <nRF24L01.h>
#include "Wire.h"
#include "I2Cdev.h"
#include <MPU6050.h>
#define buttonPin1 A0
int buttonState1 = 0;
USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
RF24 verici(7,8);
const byte addresses [][6] = {"00001", "00002"};
byte data[4];
int sensorValue[1];
void setup(){
pinMode(buttonPin1, INPUT);
Wire.begin();
mpu.initialize();
Serial.begin(9600);
verici.begin();
verici.setDataRate( RF24_250KBPS );
verici.openWritingPipe(addresses[1]);
verici.openReadingPipe(1, addresses[0]);
verici.setPALevel(RF24_PA_MAX);
//Serial.begin(115200);
// if (Usb.Init() == -1) {
// Serial.print(F("\r\nOSC did not start"));
// while(1); //halt
// }
// Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}
void loop(void){
delay(5);
verici.stopListening();
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
data[0] = map(ay, -17000, 17000, 0, 179);
data[1] = PS3.getAnalogHat(LeftHatY);
data[2] = PS3.getAnalogHat(RightHatX);
data[3] = digitalRead(buttonPin1);
if (data[3] == 1)
{
data[3] = 1;
}
else if (data[3] == 0)
{
data[3] = 0;
}
Serial.print(data[3]);
Serial.print("\t");
Serial.print(data[0]);
Serial.print("\t");
delay(5);
verici.write(data,sizeof(data));
radio.startListening();
sensorValue[1] = digitalRead(MQ2pin);
verici.read(&sensorValue,sizeof(sensorValue));
}
Reciever Code:
#include <PS3BT.h>
#include <Servo.h>
#include <SPI.h>
#include "RF24.h"
#include <nRF24L01.h>
#define enA 6
#define in1 1
#define in2 5
#define enB 3
#define in3 4
#define in4 2
#define MQ2pin A1
#define relayPin1 A0
int newData;
int buttonState = 0;
USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);
Servo Servo_motor_y;
RF24 alici(7,8);
const byte addresses [][6] = {"00001", "00002"};
byte data[4];
int sensorValue[1];
int xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup()
{
pinMode(relayPin1, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
digitalWrite(relayPin1, LOW);
Serial.begin(9600);
Servo_motor_y.attach(9);
alici.begin();
alici.setDataRate( RF24_250KBPS );
alici.openWritingPipe(addresses[0]);
alici.openReadingPipe(1, addresses[1]);
alici.setPALevel(RF24_PA_MAX);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void loop()
{
alici.startListening();
while(!alici.available()){
bool done = false;
while (!done)
done = alici.read(&data, sizeof(data));
alici.read( data, sizeof(data) );
//Serial.print(buttonState);
Servo_motor_y.write(data[0]);
Serial.println(data[0]);
delay(50);
}
if (buttonState == 1) {
digitalWrite(relayPin1, HIGH);
}
else if (buttonState == 0) {
digitalWrite(relayPin1, LOW);
}
yAxis = data[1];
xAxis = data[2];
// Serial.print("Digital Output: ");
// Serial.print(sensorValue);
// delay(2000);
// Serial.println(yAxis);
// Serial.println(xAxis);
if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
if (yAxis < 117) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
motorSpeedA = map(yAxis, 117, 0, 0, 255);
motorSpeedB = map(yAxis, 117, 0, 0, 255);
}
else if (yAxis > 138) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
motorSpeedA = map(yAxis, 138, 255, 0, 255);
motorSpeedB = map(yAxis, 138, 255, 0, 255);
}
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
if (xAxis < 117) {
int xMapped = map(xAxis, 117, 0, 0, 255);
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 138) {
int xMapped = map(xAxis, 138, 255, 0, 255);
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA);
analogWrite(enB, motorSpeedB);
}
alici.stopListening();
sensorValue[1] = digitalRead(MQ2pin);
alici.write(&sensorValue,sizeof(sensorValue));
}
Thank you.