nRF24L01

Hey guys

Sorry didn't know where to place this , hope this is ok ?
Please go easy - I'm new, have been worried about asking and this is my first question.

Been learning Arduino during lock down so this might be a simple fix or just done wrong .

I've built a working remote control and robot but when sending the signal I don't know how to make it usable from the robots end .
I think it's because it's sent as text ? but no idea what to look for that might help .

How do I turn what's coming from radio.read into something I can use in my if loop ?
Just stuck and can't seem to get letters or numbers into a variable :confused:

void loop() {

while (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));

delay(100);

}
if (Command == 01100110)
{
Serial.println("Forward");
MoveForward();
}

else if (Command == 01100010)
{
Serial.println("Back");
MoveBack();
}

else if (Command == 01101100)
{
Serial.println("Left");
MoveLeft();
}

else if (Command == 01110010)
{
Serial.println("Right");
MoveRight();
}

else
{
Serial.println("Still");
Still();
}
}

bot_control.ino (3.17 KB)

remote.ino (1.77 KB)

You are receiving a string, so check for a string. Now, with strings you cannot use ==. Use strcmp() instead.

A start at using strcmp():

void loop()
{
   if (radio.available())
   {

      radio.read(&text, sizeof(text));
      Serial.print('<');
      Serial.print(text);
      Serial.println('>');
      //delay(250);


      if (strcmp("01110010", text) == 0)
      {
         Serial.println("Forward");
         MoveForward();
      }
   }

Note that the comparison in in the if (radio.available()) block. The command will not change unless a new one is received.

There should be no need for parsing with an nRF24 because it is easy to send either an array or a struct. It's much easier than using Serial.

Tell us exactly what items of data you want to send.

...R
Simple nRF24L01+ Tutorial

Thank you for awnsering guys . would have responded last night but it was late .

@groundFungus

Just trying your suggestion, getting some errors back "text was not declared in this scope"
adding [char text[32] = "";] seems to fix it but then the next line that uses text throws a wobbler.

I know it's probebly my fault . Sorry

@Robin2

Can see your link to related page and will look after a brew

I'm trying to send anything that can then be used to call a function on the robots side. Sorry only done simple scripts before this and just picking up more ways to do things - Little mind boggling but fun :smiley:

Again thanks for taking the time to respond as I'm terrible at asking questions. Know what I want it to do but unsure what magic word to use - saying please doesn't seem to make it work :grinning:

charliebrown80:
saying please doesn't seem to make it work

Maybe not on its own - but it sure is a great way to put the helpers in a good frame of mind :slight_smile:

I'm trying to send anything that can then be used to call a function on the robots side.

What are you sending the data from? For example are you using a joystick?

If the purpose of a message is to have (say) functionA() called on one occasion and functionB() called on another occasion then you could just send 'A' or 'B'. But if you want to include numbers that the function will use then you should send them in the same message. If you provide details of the data you want to send we can show you how to do it. You should get some ideas from my Tutorial, but it does not cover everything as it is just intended to get-you-started.

...R

@ Robin2

My controller is just 4 buttons on a breadboard with a Nano and a nRF24L01+PA+LNA (photo attached )

Sending messages is fine but have no idea how to turn sent data into something the robot can use.
Don't mind if it's text or numbers , I was just trying diffrent ways to make it work or throwing things into the script.

Have try'd your way groundFungus.
I'm just confused with using new syntax. Still doing alot of copy \ paste and learning in between.

Image from Reply #6 so we don't have to download it. See this Simple Image Posting Guide

...R

I would put the readings from the 4 buttons into an array and send the array - something like this (see it in the context of the examples in my Tutorial)

byte btnPins[] = {6,7,8,9); // the pins the buttons are connected to - put in your own values
byte btnVals[4];
for (byte n = 0; n < 4; n++) {
   btnVals[n] = digitalRead(btnPins[n];
}
radio.write(&btnVals, sizeof(btnVals));

and on the receiving side put the data into an equivalent array.

Now your receiver has all information about the buttons as though the buttons were attached to the receiver.

...R

Sorry for the delay was fixing the garden . Was gonna PM you but felt rude and others might be afraid to ask.

Just been trying to add your code to mine then had to copy \ paste whole thing. Think you ment look at your (SimpleTx - the master or the transmitter) ?

I was part of the learning support at school. Had rubbish computer lessons and Algebra was never taught.
So some of your example are similar to my first transmitter but confusing in places if you've never delt with more complex math (or forgot :wink: ) or things like arrays :cold_sweat:

just blagging my head abit after thinking I'd done it or why I can't get groundFungu's way to work, Looked a good fit :slight_smile:

byte btnPins[] = {6,7,8,9); // the pins the buttons are connected to - put in your own values
byte btnVals[4];
for (byte n = 0; n < 4; n++) {
   btnVals[n] = digitalRead(btnPins[n];
}

radio.write(&btnVals, sizeof(btnVals));

I'm thinking (top) is ment to replace what's in my loop (bottom) ?

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Arduino.h>


const int RightButton = 6;
const int LeftButton = 5;
const int ForwardButton = 4;
const int BackButton = 3;

int RightState = 0;
int LeftState = 0;
int ForwardState = 0;
int BackState = 0;

const char RightTurn[] ="Right";
const char LeftTurn[] ="Left";
const char Forward[] ="Forward";
const char Back[] ="Back";
const char Wait[] ="Waiting for input";

RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001"; 

void setup() {

  Serial.begin(9600);
  pinMode (RightButton, INPUT);
  pinMode (LeftButton, INPUT);
  pinMode (ForwardButton, INPUT);
  pinMode (BackButton, INPUT);

  radio.begin();
  radio.openWritingPipe(address);
  //radio.setPALevel(RF24_PA_MIN);
  radio.setPALevel(RF24_PA_MAX); 
  radio.stopListening();
}  
 
void loop() {


   RightState = digitalRead(RightButton);
   LeftState = digitalRead(LeftButton);
   ForwardState = digitalRead(ForwardButton);
   BackState = digitalRead(BackButton);
   
   delay (30);

   if (RightState == HIGH) 
   {
     radio.write(&RightTurn, sizeof(RightTurn));
     Serial.println("Right");
   }
   else if (ForwardState == HIGH) 
   {
     radio.write(&Forward, sizeof(Forward));
     Serial.println("Forward");
   }
   else if (BackState == HIGH) 
   {
     radio.write(&Back, sizeof(Back));
     Serial.println("Back");
   }
   else if (LeftState == HIGH) 
   {
     radio.write(&LeftTurn, sizeof(LeftTurn));
     Serial.println("Left");
   }  
   else 
   {    
     radio.write(&Wait, sizeof(Wait));
     Serial.println("No Input");    
   }
   
 }

Think I'm confused where and how to place your code . Looks like it belongs in the loop but [byte btnPins] & [byte btnVals] should be before or after set up and called on after ?
Will try again tomorrow .

As always thank you for putting up with how I'm asking :sunglasses: I Hate writing.

charliebrown80:
Think I'm confused where and how to place your code .

The best thing is to create the programs (tx and rx) that represent your best attempt at introducing my suggestion and then test the programs.

If it does not work then post the two programs and describe in details what happens when you run them. That way we can focus on the part you are having trouble with rather than wasting time on things that you can do.

...R

Just a quick message to say I'm not ignoring anyone just gonna be busy for the next day or two then will get back to tinkering .

Thank you :slight_smile: