-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc_car_tx
110 lines (94 loc) · 2.82 KB
/
rc_car_tx
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
/* JoyStick module transmitter code
By Nazmus
http://www.Easyprogramming.net
http://nazm.us
- SEE the comments after "//" on each line below
- 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 9
4 - CSN to Arduino pin 10
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
Default Button Pins:
Up - pin 2
Right - pin 3
Down - pin 4
Left - pin 5
-
Analog Joystick module
GND to Arduino GND
VCC to Arduino +5V
X Pot to Arduino A0
Y Pot to Arduino A1
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
#define JOYSTICK_X A0
#define JOYSTICK_Y A1
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[7]; // 6 element array holding Joystick reading and 4 buttons
int upbut = 2;
int rightbut = 3;
int downbut = 4;
int leftbut = 5;
int pot = A2;
int sensorValue = 0;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
//declare pinMode for all buttons and initial state
pinMode(upbut,INPUT);
digitalWrite(upbut,LOW);
pinMode(rightbut,INPUT);
digitalWrite(upbut,LOW);
pinMode(downbut,INPUT);
digitalWrite(downbut,LOW);
pinMode(leftbut,INPUT);
digitalWrite(leftbut,LOW);
pinMode(pot,INPUT);
//end pinMode and digitalWrite
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
sensorValue = analogRead(pot);
sensorValue = map(sensorValue,0,1023,0,255);
joystick[0] = analogRead(JOYSTICK_X);
joystick[1] = analogRead(JOYSTICK_Y);
joystick[2] = digitalRead(upbut);
joystick[3] = digitalRead(rightbut);
joystick[4] = digitalRead(downbut);
joystick[5] = digitalRead(leftbut);
joystick[6] = analogRead(pot);
radio.write( joystick, sizeof(joystick) );
// Debugging code below
Serial.print("X = ");
Serial.print(analogRead(JOYSTICK_X));
Serial.print(" Y = ");
Serial.print(analogRead(JOYSTICK_Y));
Serial.print(" Y = ");
Serial.print(" Up = ");
Serial.print(digitalRead(upbut));
Serial.print(" Right = ");
Serial.print(digitalRead(rightbut));
Serial.print(" Down = ");
Serial.print(digitalRead(downbut));
Serial.print(" Left = ");
Serial.print(digitalRead(leftbut));
Serial.print(" Pot = ");
Serial.println(sensorValue);
// Debugging code above
}//--(end main loop )---