ive written code for my TX which should send an array using the potentiometer values from A0 and A1 i have a simple serial.println() to make sure its reading them properly
TX code:
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
RF24 radio(8,9);
const byte address[6] = "12345"; // setting up pipe address
int values[2];
void setup() {
Serial.begin(9600);
radio.begin();
radio.setChannel(100);
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(address); // setting up address for writing the values and sending them
radio.setPALevel(RF24_PA_MIN); // Arduinos are relatively close so im using MIN
radio.stopListening();
pinMode(A0, INPUT);
pinMode(A1, INPUT);
}
void loop() {
radio.stopListening();
values[0] = analogRead(A0); // assigning the values in the array the values of the potentiometer
values[1] = analogRead(A1);
Serial.print("x = ");
Serial.println(values[0]); // printing the xvalues in the serial monitor
Serial.println("----------------------");
delay(100);
Serial.print("y = ");
Serial.println(values[1]); printing the yvalues in the serial monitor
Serial.println("----------------------");
delay(100);
radio.write(&values, sizeof(values)); // sending the values to RX
}
And then my RX which has a L98NA it should receive the data sent by the TX and call a function movement() based off the values being received
RX:
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
int motorin1 = 2; // setting up the motor inputs
int motorin2 = 3;
int motor2in1 = 4;
int motor2in2 = 5;
int motor1ENA = 6; // setting up the enable pins for speed control
int motor2ENA = 9;
float turnspeed;
RF24 radio(8,7); // ce , csn
const byte address[6] = "12345";
float fwdspeed; // speed variables that ive assigned below
float bwdspeed;
float rightspd;
float lftspd;
int values[2]; // setting up an array for the data being received
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,address);
radio.setPALevel(RF24_PA_MIN);
radio.setChannel(100);
radio.setDataRate( RF24_250KBPS );
radio.startListening();
pinMode(motorin1, OUTPUT); // setting up motors as outputs
pinMode(motorin2, OUTPUT);
pinMode(motor2in1, OUTPUT);
pinMode(motor2in2, OUTPUT);
pinMode(motor2ENA, OUTPUT);
pinMode(motor1ENA, OUTPUT);
}
int movement(String dir, float speedd) { // function fro moving the motors based off a string and a speed
if(dir == "forward"){
digitalWrite(motorin1, HIGH);
digitalWrite(motorin2, LOW);
digitalWrite(motor2in1, HIGH);
digitalWrite(motor2in2, LOW);
Serial.println("Motor is turning forwards");
analogWrite(motor2ENA, speedd);
analogWrite(motor1ENA, speedd);
}
if (dir == "backwards"){
digitalWrite(motorin1, LOW);
digitalWrite(motorin2, HIGH);
digitalWrite(motor2in1, LOW);
digitalWrite(motor2in2, HIGH);
Serial.println("Motor is turning backwards");
analogWrite(motor2ENA, speedd);
analogWrite(motor1ENA, speedd);
}
if (dir == "right"){
digitalWrite(motorin1, LOW);
digitalWrite(motorin2, HIGH);
digitalWrite(motor2in1, HIGH);
digitalWrite(motor2in2, LOW);
}
if( dir == "left"){
digitalWrite(motorin1, HIGH);
digitalWrite(motorin2, LOW);
digitalWrite(motor2in1, LOW);
digitalWrite(motor2in2, HIGH);
}
}
void loop() {
radio.startListening();
if(radio.available()){
while(radio.available());{
radio.read(&values, sizeof(values));
}
fwdspeed = (float)values[0] * 2.191; // setting up speed translation because incoming values are from 540 to 1023 and i want them in the range 0 to 225
bwdspeed = (float)values[0] * 2.55; // 530 to 0 incoming
rightspd = (float)values[1] * 2.28; // 514 to 0 incoming
lftspd = (float)values[1] * 2.257; // 515 to 1023 incoming
int yval = values[0];
int xval = values[1];
if(yval > 540 && xval < 525 && xval > 515){ // ive each if statement a range because the two pots are on a joystick so i wanted to account for side to side movement when you push the joystick forward
movement("forward", fwdspeed);
delay(100);
}
if(yval < 525 && xval < 525 && xval > 515){
movement("backwards", bwdspeed);
delay(100);
}
if(xval > 525 && yval < 540 && yval > 525){
movement("left", lftspd);
delay(100);
}
if(xval < 515 && yval , 540 && yval > 525){
movement("right", rightspd);
delay(100);
}
Serial.println(xval);
Serial.println(yval); // not sure if the block of if statements would work i havent been able to test it
delay(100);
}
if(!radio.available()){
Serial.println("not available"); // this is all that's printing in the monitor
}
}
im not sure if i was thorough enough in explaining my code but i mainly want to know why radio.available is false im not sure what i did wrong