I have been working on this robot which is voice controlled but I want to make it controllable by a controller as well. I have a makeshift controller with a joystick 3 buttons ands a potentiometer controlled by an Arduino Nano. The actual robot is controlled by a mega board For future reference When I refer to robot that's the main project with the mega. The controller is the nano. Both of the sections have an NRF24L01 for communication. So my big problem is that the nano/controller is able to read the joystick positions but once it is sent it turns to garbage and just 0s and numbers in the thousands. WHICH IT SHOULD NOT BE. I've checked the wiring about 50 times. The two NR24L01 modules are powered by the same external 3.3V power supply WITH a 10uF capacitor. I am just trying to get the joystick X and Y to print on the Robot serial monitor. The first code below is the controller/sender and the second is the Robot/Receiver. For anyone who has some tips to help I'm just really frustrated since its taken me so long.
Controller Code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "printf.h"
RF24 radio(8,9);// ce and csn pins
int16_t data\[6\];
const byte address\[6\] = "12345";
void setup() {
printf_begin();
Serial.begin(115200);
while (!Serial) {
// some boards need to wait to ensure access to serial over USB
}
radio.begin();
radio.setDataRate(RF24_1MBPS);
radio.setPALevel(RF24_PA_MIN);
RF24 radio(8, 9, 1000000);
radio.printDetails();
radio.openWritingPipe(address);
radio.stopListening();
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(2, INPUT);
}
void loop() {
data\[0\] = analogRead(A0);
data\[1\] = analogRead(A2);
data\[2\] = analogRead(A1);
data\[3\] = digitalRead(6);
data\[4\] = digitalRead(7);
data\[5\] = digitalRead(2);
// Print the ACTUAL values being read
Serial.print("X: ");
Serial.print(data\[0\]);
Serial.print(" | Y: ");
Serial.print(data\[1\]);
Serial.print(" | Z: ");
Serial.print(data\[2\]);
Serial.print(" | B1: ");
Serial.print(data\[3\]);
Serial.print(" | B2: ");
Serial.print(data\[4\]);
Serial.print(" | B3: ");
Serial.println(data\[5\]);
bool sent = radio.write(&data, sizeof(data));
Serial.print("Write: ");
Serial.println(sent ? "SUCCESS" : "FAILED");
Serial.println("----------------");
delay(100);
}
RECEIVER/ROBOT CODE
#include <Servo.h>
Servo Myservo;
#define RPWM 11
#define LPWM 10
#include <Wire.h>
#define TRIG 48
#define ECHO 46
long duration;
float distance;
int Speed = 180;
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte address\[6\] = "12345"; // Mailbox
int16_t data\[6\];
RF24 radio(7,8);
int CanPrint = true;
int Mode = 6;
#include "DFRobot_DF2301Q.h"
DFRobot_DF2301Q_UART asr(&Serial);
uint8_t lastCmd = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 29, 27, 26, 24, 25);
#include "printf.h"
void setup() {
printf_begin();
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(Mode, INPUT_PULLUP);
lcd.begin(16, 2);
Myservo.attach(9);
Myservo.write(90);
radio.begin();
radio.setDataRate(RF24_1MBPS);
radio.setPALevel(RF24_PA_MIN);
// Match the transmitter!
radio.printDetails();
radio.openReadingPipe(0, address);
radio.startListening();
lcd.setCursor(0, 0);
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);
RF24 radio(7, 8, 1000000);
while (!asr.begin()) {
Serial.println("Communication failed!");
delay(3000);
}// DFRobot
Serial.println("Begin OK!");
// Increase listening time
asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_WAKE_TIME, 180);
}
void loop() {
if (digitalRead(Mode) == HIGH) {
if (CanPrint == false) {
CanPrint = true ;
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Controller Mode ");
}
if (radio.available()) {
radio.read(&data, sizeof(data));
lcd.clear(); // Clear the screen
lcd.home();
lcd.print(data\[0\]);
Serial.print("X: ");
Serial.print(data\[0\]);
Serial.print(" Y: ");
Serial.println(data\[1\]);
}
} // Controller mode is High
if (digitalRead(Mode) == LOW) { // Voice mode is Low
digitalWrite(TRIG, LOW); // Make sure it starts off
delayMicroseconds(2);
digitalWrite(TRIG, HIGH); // Send the trigger pulse
delayMicroseconds(10);
digitalWrite(TRIG, LOW); // Stop the pulse
duration = pulseIn(ECHO, HIGH);
distance = duration \* 0.034 / 2;
if (distance < 10.16 && distance > 0) {
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Too Close");
}
uint8_t cmdID = asr.getCMDID();
if (CanPrint == true) {
cmdID = 0;
CanPrint = false;
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Voice Mode ");
}
if (cmdID == 0) {
delay(100);
return;
}
// Print command received
Serial.print("Command ID: ");
Serial.println(cmdID);
if (cmdID == 0) {
return;
}
Serial.print("Command ID: ");
Serial.println(cmdID);
lastCmd = cmdID;
if (cmdID == 5) {
// BACKWARDS
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Going Backwards");
Serial.println("Going backwards");
analogWrite(RPWM, 0);
analogWrite(LPWM, Speed);
}
else if (cmdID == 22) {
// FORWARD
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Going Forwards");
Serial.println("Going forwards");
analogWrite(RPWM, Speed);
analogWrite(LPWM, 0);
}
else if (cmdID == 7) {
// LEFT
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Turning Left");
Serial.println("Turning Left");
Myservo.write(65);
}
else if (cmdID == 8) {
// RIGHT
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Turning Right");
Serial.println("Turning Right");
Myservo.write(123);
}
else if (cmdID == 9) {
// STRAIGHT
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Straightening");
lcd.setCursor(0, 1);
lcd.print("Out");
Serial.println("Straightening");
Myservo.write(90);
}
else if (cmdID == 6) {
// STOP
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Stopping");
Serial.println("Stopping");
}
else if (cmdID == 10) { //MX SPEED
// MAX SPEED
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Maximum Speed");
Serial.println("Maximum speed ");
Speed = 255;
}
else if (cmdID == 11) { // Low speed
if (Speed > 5) {
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Lowering Speed");
Speed = Speed - 25;
}
}
else if (cmdID == 12) { // Inc speed
if (Speed <= 230) {
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Increasing Speed");
Speed = Speed + 25;
}
}
else if (cmdID == 13) { //Half Speed
lcd.clear(); // Clear the screen
lcd.home();
lcd.print("Half Speed");
Speed = 180;
}
}
// Wait for module to settle
delay(45);
lastCmd = 0;
}
if anyone could tell me whats wrong I would INSANELY appreciate it.