For context, this is my attempt so far, but it has been frustrating to get anything to connect properly...
[size=0.8em]Code: [url=https://arduinogetstarted.com/tools/arduino-code-highlighter]see how to post code[/url] [/size]
---
|
``` [size=0.8em]/* Transmitter Code
|
| Code to read Joystick position and transmit it with a RF24L01+ to a receiver |
| */ |
#include <SPI.h>
#include <RF24.h>
// Radio Configuration
RF24 radio(9, 10);
byte addresses[][6] = {"1Node", "2Node"};
bool radioNumber = 1;
bool role = 1; //Control transmit 1/receive 0
// Decide where you are going to plug the joystick into the circuit board.
int UpDown_Pin = 0; // Plug Joystick Up/Down into Analog pin 0
int LeftRight_Pin = 1; // Plug Joystick Left/Right into Analog pin 1
// Create variables to read joystick values
float UpDown_Input ; // Variable to store data for Up/Down input from joystick
float LeftRight_Input ; // Variable to store data for for Left/Right input from joystick
// Create variables to transmit servo value
int UpDown_Output; // Expected range 0 - 1023
int LeftRight_Output; // Expected range 0 - 1023
// These variables allow for math conversions and later error checking as the program evolves.
int Up_Limit = 800; // High ADC Range of Joystick ForeAft
int Down_Limit = 220; // Low ADC Range of Joystick ForeAft
int Right_Limit = 800; // High ADC Range of Joystick LeftRight
int Left_Limit = 226; // Low ADC Range of Joystick LeftRight
// constants won't change. They're used here to set pin numbers:
bool SpotButton = 7;
bool SpeedButton = 8;
const int SpeedLed = 4;
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int SpotButtonState = 0; // variable for reading the pushbutton status
int SpeedButtonState = 0;
void [color=#5E6D03]setup/color {
// put your setup code here, to run once:
Serial.[color=#D35400]begin/color; // Get ready to send data back for debugging purposes
radio.[color=#D35400]begin/color; // Get the transmitter ready
radio.setPALevel(RF24_PA_LOW); // Set the power to low
radio.openWritingPipe(addresses[1]); // Where we send data out
radio.openReadingPipe(1, addresses[0]); // Where we receive data back
// initialize the Spotlight pin as an output:
pinMode(SpotButton, INPUT);
// initialize the Speed Button pin as an input:
pinMode(SpeedButton, INPUT);
pinMode (SpeedLed, OUTPUT);
}
void [color=#5E6D03]loop/color {
// put your main code here, to run repeatedly:
Serial.[color=#D35400]println/color;
UpDown_Input = [color=#D35400]analogRead/color ; // Read the Up/Down joystick value
Serial.[color=#D35400]println/color;
LeftRight_Input = [color=#D35400]analogRead/color ; // Read the Left/Right joystick value
// Serial.print(UpDown_Output);
radio.stopListening(); // Stop listening and begin transmitting
[color=#D35400]delay/color; // quite a long delay -- causes jittering of servo
if (radio.write(&UpDown_Output, [color=#00979C]sizeof/color), Serial.println("sent UpDown")); //Send UpDown data
if (radio.write(&LeftRight_Output, [color=#00979C]sizeof/color), Serial.println("sent LeftRight")); //Send LeftRight data
radio.startListening(); // Get ready to receive confirmation from receiver
// read the state of the pushbutton value:
SpotButtonState = [color=#D35400]digitalRead/color;
SpeedButtonState = [color=#D35400]digitalRead/color;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (SpeedButtonState = HIGH)
// turn LED on:
digitalWrite(SpeedLed, HIGH);
else
// turn LED off:
digitalWrite(SpeedLed, LOW);
}
// Function to convert and scale the Fore/Aft data
float convertUpDownToServo(float y) {
int result;
result = ((y - Down_Limit) / (Up_Limit - Down_Limit) * 180);
return result;
}
// Function to convert and scale the Left / Right data
// Can be replaced with Map function
float convertLeftRightToServo(float x) {
int result;
result = ((x - Left_Limit) / (Right_Limit - Left_Limit) * 180);
return result;
}[/size]
```
|