Reading the latest raw potentiometer values

I wish to do what the title says. I am struggling to find out how to send the latest potentiometer.raw value. Like, if the left stick is moved, that val is sent

Here's what I got. I put all.lines because.I figured you guys.may also help me. This code will not be used for my real project; it is more of a practice and understanding and proof of concept.

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

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

struct send
{
	int potValX;
	potValX = analogRead(14); //A0
	int potValY;
	potValY = analogRead(15); //A1
	
}message;
	
void setup()
{
	pinMode(13, INPUT); //A0
	pinMode(14, INPUT); //A1
	radio.begin();
	radio.setDataRate(RF24_250KBPS);
	radio.setPALevel(RF24_PA_MAX);
	radio.setRetries(2, 15)
	radio.openWritingPipe(address);

}

void loop()
{
	readPotAndSend();
	
	bool sending = readPotAndSend();
	if(sending = false)
	{
		Serial.print("Failed to send");
		Serial.print("Trying...");
	}
	else if(sending = true)
	{
		Serial.println("I think I am sending X: ");
		Serial.println(message.potValX);
		Serial.println("I think I am sending Y: ");
		Serial.println(message.potValY);
		Serial.println("Content of struct: ");
		Serial.println(message);
	}
	
}

/*
sending individual struct parts
uncomment if desired
*/
	
/*
the goal of the function us to send
the most recent value by supposedly
reading the latest value/ whichever
had a gain last
*/


void readPotAndSend()
{
	int potValXNow = message.potValX;
	int potValYNow = message.potValY;		

//Supposed method 1
	if(message.potValX > || < potValXNow)
	{
		radio.write
		(&message.potValX, sizeof(message.potValX));
		
	}
//end method 1

//Supposed method 2
	if(potValYNow > || < message.potValY)
	{
		radio.write
		(&message.potValY, sizeof(message.potValY));
	}

//end method 2

	bool send0 = radio.write
		(&message.potValX, sizeof(message.potValX));
		
	bool send1 = radio.write
		(&message.potValY, sizeof(message.potValY));
		
	bool send2 = radio.write
		(&message, sizeof(message));
	if(!send0 || !send1 || send2)
	{
		return false;
	}
	else if(send0 || send1 || send2)
	{
		return true;
	}
}

/*
	
//basic send raw values
	
void readPotandSend()
{
	radio.write(&message, sizeof(message));
}
*/

I cannot see any purpose for the "struct message", does it work at all? The struct is initialized once, so the values stored in it should never changes. The values may even be read before pinMode is called on the pins it reads.

Depending on the code you did not think was relevant, I would do somthing like this instead:

#define POTX_PIN A0
#define POTY_PIN A1

int potValues[2];

void readPots() {
  potValues[0] = analogRead(POTX_PIN);
  potValues[1] = analogRead(POTY_PIN);
}

void setup() {
  pinMode(POTX_PIN, INPUT);
  pinMode(POTY_PIN, INPUT);
}

void loop() {

  readPots(); //Initialize actual values

  //Do something with values

  radio.write(potValues, sizeof(potValues));

}

And your code is missing a declaration for the radio object.

groundFungus:
And your code is missing a declaration for the radio object.

Haha thanks!!

Danois90:
I cannot see any purpose for the "struct message", does it work at all? The struct is initialized once, so the values stored in it should never changes. The values may even be read before pinMode is called on the pins it reads.

Depending on the code you did not think was relevant, I would do somthing like this instead:

#define POTX_PIN A0

#define POTY_PIN A1

int potValues[2];

void readPots() {
 potValues[0] = analogRead(POTX_PIN);
 potValues[1] = analogRead(POTY_PIN);
}

void setup() {
 pinMode(POTX_PIN, INPUT);
 pinMode(POTY_PIN, INPUT);
}

void loop() {

readPots(); //Initialize actual values

//Do something with values

radio.write(potValues, sizeof(potValues));

}

Thanks for code advice! Really appreciate it!

I was at the networking or programming forum about Code Efficiency on a DIY transmitter (i think that's the title). They suggested sending my data in structs or arrays to save typing space and make the code essentially better.

Code efficiency help on DIY transmitter and quadcopter

My problem is in my application (quadcopter) I am sending 4 different commands: throttle, roll, pitch, and yaw. But how can the receiver react to 4 different commands?!?!?!

This ^ has been bugging me for days! It's driving me crazy!!!!!

JeromeAriola:
My problem is in my application (quadcopter) I am sending 4 different commands: throttle, roll, pitch, and yaw. But how can the receiver react to 4 different commands?!?!?!

This ^ has been bugging me for days! It's driving me crazy!!!!!

Hard to answer without knowing how the commands are to be transmitted. One could suggest that the quadcopter receives 4 values defining throttle, yaw, pitch and roll and it then adjusts rotorspeed and servos accordingly.

Build a packet to be sent using snprintf.

char packet[32];  // max rf24 packet size change to suit
snprintf(packet, 31, "%d,%d,%d,%d", throttle, roll, pitch, yaw);  // leave room for NULL

Send the packet.

Once the packet is received use strtok and atoi to separate the values back into the throttle, roll, pitch, yaw values.

A good example of how to use strtok is in the serial input basics thread example #5.

I am sure there are different (better?) ways to do it, but that is my method.

You can send the data using a struct, but you need to separate the declaration of the struct, the instancing of the struct, and the valuing of the instance members.

struct send
{
   int potValX;
   int potValY;
};
struct send message;
   message.potValX = analogRead(0);
   message.potValY = analogRead(1);

Do not try to declare/initialize/populate the instance in one step.

I would not convert the values to strings and back again. It increases bandwidth usage and requires more compute power. I would send a small header to signal transmission start and then the raw values.

I'm just not sure what the problem with "reacting to 4 commands" is all about. It could be a question about how to convert the pot[x,y] values into XYZ (or yaw, pitch, roll) motion which can be understood by the quadcopters control system.

Wow, thanks all!

The commands are sent as values read from the different axes of each potentiometer. The transmitter just says, "Oh, left stick is moved to left," stores that value in to message.yaw.

Now, it sends it. But receiver would be like, "Which does he want to do?"

I was thinking of using a switch statement here... a separated case for each axes when it sends. But, I don't know -- still pretty novice

Danois90:
I'm just not sure what the problem with "reacting to 4 commands" is all about. It could be a question about how to convert the pot[x,y] values into XYZ (or yaw, pitch, roll) motion which can be understood by the quadcopters control system.

Exactly what I want!!!!

LOL! And how is anyone supposed to help you with that, since you do not provide information about what values the quadcopter wants? You really need to explain the problem better if you want any help :slight_smile:

Lady to the doctor: HELP ME, I'M DYING!
Doctor to lady: Oh my.. Lets check if you car still runs..

Danois90:
LOL! And how is anyone supposed to help you with that, since you do not provide information about what values the quadcopter wants? You really need to explain the problem better if you want any help :slight_smile:

Lady to the doctor: HELP ME, I'M DYING!
Doctor to lady: Oh my.. Lets check if you car still runs..

I want my quadcopter to work just like how any quad would. I want it to react to a sent pot value, map it in the Rx, and write in servo value the speed of the motor.