-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
joystickRX.ino
111 lines (90 loc) · 3.24 KB
/
joystickRX.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* JoyStick module receiver code
By Nazmus
http://www.Easyprogramming.net
http://nazm.us
- CONNECTIONS: nRF24L01 Modules See:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 6
4 - CSN to Arduino pin 7
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
Above connections created by shield
On Motor Shield connect Servos to servo connections 1 & 2
*/
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 6
#define CSN_PIN 7
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
const int servo1 = 9; // first servo
const int servo2 = 10; // second servo
int servoVal; // variable to read the value from the analog pin
Servo myservo1; // create servo1
Servo myservo2; // create servo2
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[6]; // 6 element array holding Joystick readings
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
delay(1000);
myservo1.attach(servo1); // attaches the servo1
myservo2.attach(servo2); // attaches the servo2
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();;
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
done = radio.read( joystick, sizeof(joystick) );
int joystickX = map(joystick[0],0,1023,0,180); // turn value of 0-1023 to 0-180 degrees
int joystickY = map(joystick[1],0,1023,0,180); // turn value of 0-1023 to 0-180 degrees
// the four button variables from joystick array
int upbut = joystick[2];
int rightbut = joystick[3];
int downbut = joystick[4];
int lefttbut = joystick[5];
servoVal = joystickX; // scale it to use it with the servo (result between 0 and 180)
myservo2.write(servoVal); // sets the servo position according to the scaled value
//Serial.println(servoVal); // for debugging
servoVal = joystickY; // scale it to use it with the servo (result between 0 and 180)
myservo1.write(servoVal); // sets the servo position according to the scaled value
//Serial.println(servoVal); // for debugging
// Fetch the data payload - Debugging code below
Serial.print("X = ");
Serial.print(joystickX);
Serial.print(" Y = ");
Serial.print(joystickY);
Serial.print(" Up = ");
Serial.print(joystick[2]);
Serial.print(" Right = ");
Serial.print(joystick[3]);
Serial.print(" Down = ");
Serial.print(joystick[4]);
Serial.print(" Left = ");
Serial.println(joystick[5]);
delay(15); // waits for the servo to get there
}
}
else
{
Serial.println("No radio available");
}
}//--(end main loop )---