Hello,
So the codes are below,
The transmitter:
#include <SPI.h>
#include <RF24.h>
#include <RF24_config.h>
// Radio pin setup
#define cePin 9
#define csnPin 10
#define joyX A0
#define joyY A1
// Init RF24 radio
RF24 radio(cePin, csnPin);
const uint64_t pipe0 = 0xF0F0F0AA;
uint8_t p0 = 0;
// Variables
int chr[1];
int terminateChar[1];
String dataToSend = "";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe0);
}
void loop() {
dataToSend += map(analogRead(joyX), 0, 1023, 0, 9);
dataToSend += map(analogRead(joyY), 0, 1023, 0, 9);
int stringSize = dataToSend.length();
for (int i = 0; i < stringSize; i++) {
int charToSend[1];
charToSend[0] = dataToSend.charAt(i);
radio.write(charToSend,1);
}
terminateChar[0] = 2;
radio.write(terminateChar, 1);
dataToSend = "";
}
The receiver:
#include <SPI.h>
#include <RF24.h>
#include <RF24_config.h>
// Radio pin setup
#define cePin 9
#define csnPin 10
// US sensor pin setup
#define trigPin 14
#define echoPin 2
#define accuracy 4
// Motor pin setup
#define enableLeft 6
#define leftForward 4
#define leftBackward 3
#define enableRight 5
#define rightForward 8
#define rightBackward 7
// Init RF24 radio
RF24 radio(cePin, csnPin);
const uint64_t pipe0 = 0xF0F0F0AA;
uint8_t p0 = 0;
// Variables
String receivedMessage = "";
int chr[1];
int joyX = 4;
int joyY = 4;
int speedCoeff = 0;
int left = 0;
int right = 0;
void setup(){
// Setup RF24 radio
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(p0, pipe0);
radio.startListening();
// Setup US sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Setup LEFT whel
pinMode(enableLeft, OUTPUT);
analogWrite(enableLeft, 255);
pinMode(leftForward, OUTPUT);
digitalWrite(leftForward, LOW);
pinMode(leftBackward, OUTPUT);
digitalWrite(leftBackward, LOW);
// Setup RIGHT wheel
pinMode(enableRight, OUTPUT);
analogWrite(enableRight, 255);
pinMode(rightForward, OUTPUT);
digitalWrite(rightForward, LOW);
pinMode(rightBackward, OUTPUT);
digitalWrite(rightBackward, LOW);
}
void loop(){
// Get Distance sensor
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Watch control messages
while(radio.available()){
radio.read(chr, 1);
char receivedChar = chr[0];
if (chr[0] != 2){
receivedMessage.concat(receivedChar);
} else {
joyX = receivedMessage.substring(0,1).toInt();
joyY = receivedMessage.substring(1,2).toInt();
if(distance < 15){
joyX = 4;
joyY = 4;
}
if(joyY < 4){
// Backward
digitalWrite(rightBackward, HIGH);
digitalWrite(rightForward, LOW);
digitalWrite(leftBackward, HIGH);
digitalWrite(leftForward, LOW);
speedCoeff = map(joyY, 0, 4, 255, 0);
speedCoeff = speedCoeff < 0 ? 0 : speedCoeff;
speedCoeff = speedCoeff > 255 ? 255 : speedCoeff;
} else if(joyY > 4){
// Forward
digitalWrite(rightBackward, LOW);
digitalWrite(rightForward, HIGH);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, HIGH);
speedCoeff = map(joyY, 4, 9, 0, 255);
speedCoeff = speedCoeff < 0 ? 0 : speedCoeff;
speedCoeff = speedCoeff > 255 ? 255 : speedCoeff;
}
right = map(joyX, 4, 9, 255, 0);
right = right < 0 ? 0 : right;
right = right > 255 ? 255 : right;
right = (right + speedCoeff) / 2;
analogWrite(enableRight, right);
left = map(joyX, 0, 4, 0, 255);
left = left < 0 ? 0 : left;
left = left > 255 ? 255 : left;
left = (left + speedCoeff) / 2;
analogWrite(enableLeft, left);
if(joyY == 4){
// Stopped
if(joyX < 4){
// Left
digitalWrite(rightBackward, LOW);
digitalWrite(rightForward, HIGH);
digitalWrite(leftBackward, HIGH);
digitalWrite(leftForward, LOW);
right = map(joyX, 0, 4, 255, 0);
right = right < 0 ? 0 : right;
right = right > 255 ? 255 : right;
analogWrite(enableRight, right);
left = map(joyX, 0, 4, 255, 0);
left = left < 0 ? 0 : left;
left = left > 255 ? 255 : left;
analogWrite(enableLeft, left);
} else if(joyX > 4){
// Right
digitalWrite(rightBackward, HIGH);
digitalWrite(rightForward, LOW);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, HIGH);
right = map(joyX, 4, 9, 0, 255);
right = right < 0 ? 0 : right;
right = right > 255 ? 255 : right;
analogWrite(enableRight, right);
left = map(joyX, 4, 9, 0, 255);
left = left < 0 ? 0 : left;
left = left > 255 ? 255 : left;
analogWrite(enableLeft, left);
} else if (joyX == 4){
// Stopped
digitalWrite(rightBackward, LOW);
digitalWrite(rightForward, LOW);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, LOW);
analogWrite(enableRight, 0);
analogWrite(enableLeft, 0);
}
}
receivedMessage = "";
}
}
}
The Hello World exercise is below
http://starter-kit.nettigo.eu/2014/connecting-and-programming-nrf24l01-with-arduino-and-other-boards/
I have replaced the Arduino Nano from the example with an Arduino Mega, I am powering the receiver module from a separate 3.3 v source so I cannot see to find the problem. The connection is working, but not with the code from up. I am aware that the pins are different on the Mega.
I have done the tutorial also. It works. The problem is when I put the code for the Transmitter and Receiver from webondevice I do not have communication. I have checked everything.
Thank you!