Go Down

Topic: (NRF24l01) Sending values from Raspberry to Arduino  (Read 189 times) previous topic - next topic

Melobih

Hey guys,

the idea of my project is to connect a PS4 controller with the raspberry Pi and then send the values of the joystick with a nrf24l01 module to a Arduino Nano. The Nano controls a DC motor with a L293D module via PWM. Everything works, the Rpi gets the values and sends it to the Nano. But whats really annoying is that - when I press the throttle, the motor starts to spin, but when I release the throttle (the motor should stop spinning) the motor is still spinning.

Here's the code of the Nano:

Code: [Select]


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

int code2;


const int max_payload_size = 32;

RF24 myradio (7, 8);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
char receive_payload[max_payload_size+1];


void setup(void)
{
  pinMode(6, OUTPUT);
  Serial.begin(500000);
  myradio.begin();
  myradio.enableDynamicPayloads();
  myradio.setRetries(5,15);
  myradio.openReadingPipe(1, pipes[0]);
  myradio.startListening();




}
 



void loop(void)
{



    while (myradio.available() )
    {

      uint8_t len = myradio.getDynamicPayloadSize();
      receive_payload[len] = 0;
      myradio.read( receive_payload, len );
      code2 = int(receive_payload);
      int receive_payload;
      if (code2 > 50)
      analogWrite(6,code2);
      if (code2 < 50)
      analogWrite(6, 0);
      }
}



The part of the code from the Raspberry:

Code: [Select]
if  l_oben > 0.2:
 l_neu = int(l_oben*255)
 ls = str(l_neu)
 print ls
 payload_size = 32
 radio.write(ls[:payload_size])


Heres the Dropbox link to the clip:
https://www.dropbox.com/s/m6n2luivpsc1t22/VID_20181114_165004.mp4?dl=0

Thanks

PaulS

Code: [Select]
      code2 = int(receive_payload);
That is NOT how to convert a string to an int. The atoi() function is.

Code: [Select]
      int receive_payload;
You already have a variable called receive_payload. Why are you creating another one of a different type, in the same scope?

Quote
The part of the code from the Raspberry:
That is hardly the relevant part of the code. There is NOTHING there that explains where the value to be sent comes from, or when the snippet gets executed.
The art of getting good answers lies in asking good questions.

Robin2

I don't understand why you need to convert the values received by the Arduino - with my nRF24 programs I just read the data direct to a suitable variable, array or struct.

I don't have an RPi and I don't know if it stores data in a different format from an Arduino. When I am sending data from my Linux PC to my Arduino using a Python program on the PC I use the Python struct.pack() function to match the PC data to the Arduino data.

...R
Simple nRF24L01+ Tutorial
Two or three hours spent thinking and reading documentation solves most programming problems.

Melobih

That is NOT how to convert a string to an int. The atoi() function is.
Oh, thanks, I mixed it with python up.

You already have a variable called receive_payload. Why are you creating another one of a different type, in the same scope?

My fault, I corrected it.

That is hardly the relevant part of the code. There is NOTHING there that explains where the value to be sent comes from, or when the snippet gets executed.
It's just a float value of the controller.

I don't understand why you need to convert the values received by the Arduino - with my nRF24 programs I just read the data direct to a suitable variable, array or struct.

When I am trying to send an int with python, I've got the error message:
TypeError: 'int' object has no attribute '__getitem__'

So, when I try to send a string it seems to be ok.

But whats really interesting is, I've got the first values, after that the arduino seems to be "frozen".
Heres the clip
https://www.dropbox.com/s/kf624mxw8khht0a/VID_20181115_215811.mp4?dl=0

Thank you for the help guys!



PaulS

So, you made some code changes, and the code still doesn't do what you want.

Now, you want us to guess just exactly what you changed. Pass.
The art of getting good answers lies in asking good questions.

Melobih

So, you made some code changes, and the code still doesn't do what you want.

Now, you want us to guess just exactly what you changed. Pass.
I just changed what I wrote, I implemented the atoi() function. There is also nothing to guess.

Code: [Select]

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


int i;

const int max_payload_size = 32;

RF24 myradio (7, 8);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
char receive_payload[max_payload_size+1];


void setup(void)
{
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  myradio.begin();
  myradio.enableDynamicPayloads();
  myradio.setRetries(5,15);
  myradio.openReadingPipe(1, pipes[0]);
  myradio.startListening();




}


void loop()
{



    while (myradio.available() )
    {

      uint8_t len = myradio.getDynamicPayloadSize();
      receive_payload[len] = 0;
      myradio.read( receive_payload, len );
      i = atoi(receive_payload);
      int i2  = i*2.5;
      Serial.println(i2);
      if (i2 > 10);       
      analogWrite(6,i2);
      if (i2 < 10);
      analogWrite(6,0);

      }
}
     
     
     
 



Robin2

I just changed what I wrote, I implemented the atoi() function. There is also nothing to guess.
After you have helped with 200 or 300 Threads here you will understand that "changed what I wrote" is very unreliable :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Code: [Select]
      uint8_t len = myradio.getDynamicPayloadSize();
      receive_payload[len] = 0;
      myradio.read( receive_payload, len );

The NULL goes after the last used byte in the array, NOT (potentially) in the first byte beyond the end of the array.

Code: [Select]
      if (i2 > 10);       
Does that ; belong there?

Code: [Select]
      if (i2 < 10);
Or there?

Code: [Select]
      i = atoi(receive_payload);
There may be garbage in the receive_payload array t that atoi() interprets as part of the actual value, because the array is not properly NULL terminated.



The art of getting good answers lies in asking good questions.

Go Up