analogRead returns ONE value put in loop

The idea is i send a raw analoflg value from a potentiometer to RX, and it acts accordingly to mapped servo values, running 4 brushless motors. The signal is excellent; the message goes through to rx. The only problem is i need CONSISTENT input coming from Tx.

But on tx, it is only sending once. I wrote some code where it prints the raw value on the Serial Monitor, then sends it via nRF.

I wouod have posted the code, but i only took pics of it. Laptop wireless card is busted, i have no eth cable. Ill see if i can send the pics i took of the code. I also dont have it on my phone.

Are you sure that you put the Serial.print() statement into loop(), not into setup()?

Yes, I am certain.

I copied the code via the picture. Ugh

Tx:

//preprocessor directives; dw 'bout it...

RF24 radio (9,10);

void loop()
{
   int data = analogRead(2);
   Serial.println(data);
   radio.write(&data, size of(data));
}

void setup()
{
   radio.begin()
   radio.setDataRate(RF24_250KBPS);
   radio.setPALevel(RF24_PA_MAX);
   radio.setRetries(3,5);
   radio.openWritingPipe(address);
}
Rx:

//preprocessor directives

Servo a,b,c,d;

void setup()
{
   a.attach(3);
   b.attach(4);
   c.attach(5);
   d.attach(6);

   radio.begin();
   radio.setDataRate(RF24_250KBPS);
   radio.setPALevel(RF24_PA_MAX);
   radio.openReadingPipe(1, address);
   radio.startListening();
}

void loop()
{
   radio.read(&data, sizeof(data));
   a.write(data);
   b.write(data);
   c.write(data);
   d.write(data);
}

I cannot find the declaration of data in RX.

Sending values in every iteration of loop() is a bad idea. Wait at least until the preceding transmissions on all channels are complete.

Similarly for RX, wait at least for one servo cycle (20ms) before changing the value.

Oh, the "//preprocessor directives" line has the declaration of some of the vars.

I had to shortcut the code because i took pictures where I explicitly copied and typed here. My laptop is at school :slight_smile:

But my issue is that analogRead() checks the potentiometer once when its supposed to consistently do it.

If I put the stick on top, i get a reading of 1023 (of course) but then i get nothing else after. It seems that it only checks it once.

DrDiettrich:
Sending values in every iteration of loop() is a bad idea. Wait at least until the preceding transmissions on all channels are complete.

I think i get the idea, but can you please elaborate?

My laptop is at school

So, go to school. Post your real code.

The code you did post is silly. You call Serial.print() without calling Serial.begin(), and yet you expect us to believe that you got output somewhere. Not going to happen, until you post your REAL code.

Sorry!

The full code will be posted tomorrow.

And I copied the picture wrong for the tx. The code did have Serial.begin()

some pics

BTW they compiled

JeromeAriola:
some pics

BTW they compiled

How did you compile the pictures?

Why did you waste so much bandwidth posting three pictures of the same thing?

PaulS:
Why did you waste so much bandwidth posting three pictures of the same thing?

Goodness, i need my eyes checked.... Sorry!

The code that I took a photo of. That's what compiled!

I'll be patient for tomorrow. In about 7-9 hrs, i can get back here with the code.

These

and

Please display your images in your posts so we can see them without downloading them. See this Simple Image Guide

...R

Here's the code; I am at school so I was able to get it.

//Rx
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Servo.h>

Servo a,b,c,d;

RF24 radio(9,10);

int data;

const byte address[5] = {'R','o','M','e','O'};

void setup()
{
  a.attach(3);
  b.attach(4);
  c.attach(5);
  d.attach(6);
  
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setPALevel(RF24_PA_MAX);
  radio.openReadingPipe(1, address);
  radio.startListening();
}

void loop()
{
  radio.read(&data, sizeof(data));
  a.write(data);
  b.write(data);
  c.write(data);
  d.write(data);
}
//Tx
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const byte address[5] = {'R','o','M','e','O'};

RF24 radio(9,10);

void setup()
{
  Serial.begin(9600);
  
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_MAX);
  radio.setRetries(3,5);
  radio.openWritingPipe(address);
}

void loop()
{
  int data;
  data = analogRead(2);
  delay(10);
  radio.write(&data, sizeof(data));
  Serial.println(data);
  
}

Rx is supposed to write ms values to the motors as it receives command

Tx seems to only check the analog pin once, then sends it, then it stops.

To make debugging easier you should increase the delay() in the TX to 1000 (i.e. once per second).

And on your RX print the value that is received rather than try to control a servo with it.

I suspect I have given you the link to this Simple nRF24L01+ Tutorial in another Thread. Does the tutorial code work for you?

If not. get it working first. Then extend the project to do other stuff.

...R

I did exactly

Robin2:
I suspect I have given you the link to this Simple nRF24L01+ Tutorial in another Thread. Does the tutorial code work for you?

If not. get it working first. Then extend the project to do other stuff.

...R

That's exactly what i did! I made it a routine to use your code for any nRF projects.

But even changing the delay won't work for some reason! I don't know why it's not working. I have read over the code a bunch of times, but I cant find any errors

I did notice today that if i declared "data" as a global variable (ie, outside of loop) i get nothing printed.

What the heck?????

JeromeAriola:
What the heck?????

Put your program to one side for a while and go back to the exact code in the tutorial. Get that working first.

...R

You're referring to the SimpleTx and SimpleRx. I got those to work.

I make it a routine whenever dealing with nRF to use your tutorial.

I tried to add the reading of the analog values in loop(). But then it doesnt work. Sometimes, it doesn't even check the value of the analog pin. It will consistently do it only when I remove the radio.write(). I also tried switching the order in which they happen, where it prints the value first, then sends it. Then when that didn't work, I'd send it then print the value.

Neither work

And the problem still persists!

And the problem still persists!

If the problem only occurs when you read the analog pin, then it is very unlikely to be a software problem.

On the transmitter, comment out the radio.write() call. Just read the analog pin and print the value. Do you get more than one value printed? If not, there is something wrong with your hardware.