Teensy and Ardunio 2 Way Communication
Send the maximum 16 byte data packages between the 2 x Arduino, or the teensy and a nano.
Components and supplies
Arduino UNO
Capacitor 100 µF
Teensy 4.0 Development Board
Project description
Code
Transmitter
arduino
1#include <SPI.h> 2#include <nRF24L01.h> 3#include "RF24.h" 4 5 6 7#define CE_PIN 9 //4 for nano 8#define CSN_PIN 10 //2 for nano 9 10//Radio variables declaration 11 12const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'}; 13 14RF24 radio(CE_PIN, CSN_PIN); // Create a Radio 15 16struct Package 17{ 18 int8_t thrust = 0; 19 int8_t x = 0; 20 int8_t y = 0; 21 int8_t z = 0; 22}; 23 24struct Sent_1 25{ 26 int8_t gyro_x = 0; 27 int8_t gyro_y = 0; 28 int8_t gyro_z = 0; 29 int8_t Front_Left; 30 int8_t Front_Right; 31 int8_t Rear_Left; 32 int8_t Rear_Right; 33}; 34 35Package package; 36Sent_1 sent_1; 37 38bool newData = false; 39 40long unsigned int current_time = 0; 41const int signal_confimation_rate = 1000; //how often to send a radio confimation signal 42long unsigned int signal_confimation_time = 1000; //counts how often this signal is sent 43 44//Joystick variables declaration 45 46//Input pins 47const int XL_pin = 0; // analog pin connected to XL output 48const int YL_pin = 1; // analog pin connected to YL output 49const int XR_pin = 2; // analog pin connected to XR output 50const int YR_pin = 3; // analog pin connected to YR output 51 52//Pot values 53float xr, yr; 54float xl, yl; 55float thrustRate; 56 57//Raw pot values 58float xr_raw, yr_raw; 59float xl_raw, yl_raw; 60 61//Smoothing Vars 62const int numReadings = 20; 63int readIndex = 0; // the index of the current reading 64 65float readings_xr[numReadings]; // the readings from the analog input 66float total_xr = 0; // the running total 67float average_xr = 0; // the average 68 69float readings_yr[numReadings]; // the readings from the analog input 70float total_yr = 0; // the running total 71float average_yr = 0; // the average 72 73float readings_xl[numReadings]; // the readings from the analog input 74float total_xl = 0; // the running total 75float average_xl = 0; // the average 76 77//Controller Values 78int xr_mid; 79int yr_mid; 80int xl_mid; 81int yl_mid; 82//Controller angle setting 83int max_tilt = 10; 84int min_tilt = 1; 85//Values for linearly mapping pot values to X,Y target angles 86 87void setup() 88{ 89 Serial.begin(115200); 90 delay(500); 91 Serial.println("Serial \ OK"); 92 93 radio.begin(); 94 radio.setDataRate( RF24_250KBPS ); 95 radio.enableAckPayload(); 96 radio.setRetries(5, 5); // delay, count 97 // 5 gives a 1500 µsec delay which is needed for a 32 byte ackPayload 98 radio.openWritingPipe(slaveAddress); 99 Serial.println("Radio \ OK"); 100 delay(500); 101 102 xr_mid = analogRead(XR_pin); 103 yr_mid = analogRead(YR_pin); 104 xl_mid = analogRead(XL_pin); 105 yl_mid = analogRead(YL_pin); 106} 107 108void loop() { 109 110 //Reading Joystick 111 processingJoyStick(); 112 113 //Packing data 114 package.y = -average_yr; 115 package.x = -average_xr; 116 package.z = -average_xl; 117 118 //Thrust rate is a stationary value 119 thrustRate = yl; 120 package.thrust = package.thrust + thrustRate; 121 122 if (package.thrust >= 1800) { 123 package.thrust = 1800; 124 } 125 if (package.thrust <= 1000) { 126 package.thrust = 1000; 127 } 128 129 send(); 130 131 printPackage(); 132} 133 134void processingJoyStick() 135{ 136 xr_raw = analogRead(XR_pin); 137 yr_raw = analogRead(YR_pin); 138 xl_raw = analogRead(XL_pin); 139 yl_raw = analogRead(YL_pin); 140 141 if (xr_raw > (xr_mid + 50)) { 142 xr = (0.02 * xr_raw) - 10; 143 } 144 if (xr_raw < (xr_mid - 50)) { 145 xr = (0.02 * xr_raw) - 10; 146 } 147 if (xr_raw > 471 && xr_raw < 569) { 148 xr = 0; 149 } 150 if (yr_raw > 570) { 151 yr = (0.02 * yr_raw) - 10; 152 } 153 if (yr_raw < 470) { 154 yr = (0.02 * yr_raw) - 10; 155 } 156 if (yr_raw > 471 && yr_raw < 569) { 157 yr = 0; 158 } 159 if (xl_raw > 570) { 160 xl = (0.02 * xl_raw) - 10; 161 } 162 if (xl_raw < 470) { 163 xl = (0.02 * xl_raw) - 10; 164 } 165 if (xl_raw > 471 && xl_raw < 569) { 166 xl = 0; 167 } 168 if (yl_raw > 570) { 169 yl = (-0.02 * yl_raw) + 10; 170 } 171 if (yl_raw < 400) { 172 yl = (-0.02 * yl_raw) + 10; 173 } 174 if (yl_raw > 401 && yl_raw < 569) { 175 yl = 0; 176 } 177 178 //Control values must be smoothed or they cause the PDI control system to spaz out when it receives a 10 straight away 179 //Smoothing XR values 180 total_xr = total_xr - readings_xr[readIndex]; 181 // read from the sensor: 182 readings_xr[readIndex] = xr; 183 // add the reading to the total: 184 total_xr = total_xr + readings_xr[readIndex]; 185 186 //Smoothing YR values 187 total_yr = total_yr - readings_yr[readIndex]; 188 // read from the sensor: 189 readings_yr[readIndex] = yr; 190 // add the reading to the total: 191 total_yr = total_yr + readings_yr[readIndex]; 192 193 //Smoothing XL values 194 total_xl = total_xl - readings_xl[readIndex]; 195 // read from the sensor: 196 readings_xl[readIndex] = xl; 197 // add the reading to the total: 198 total_xl = total_xl + readings_xl[readIndex]; 199 // advance to the next position in the array: 200 readIndex = readIndex + 1; 201 202 // if we're at the end of the array... 203 if (readIndex >= numReadings) { 204 // ...wrap around to the beginning: 205 readIndex = 0; 206 } 207 208 // calculate the xr average: 209 average_xr = total_xr / numReadings; 210 211 // calculate the yr average: 212 average_yr = total_yr / numReadings; 213 214 // calculate the xl average: 215 average_xl = total_xl / numReadings; 216 217} 218 219void printPackage() 220{ 221 Serial.print(sent_1.gyro_x); 222 Serial.print("\ "); 223 Serial.print(sent_1.gyro_y); 224 Serial.print("\ "); 225 Serial.print(sent_1.gyro_z); 226 Serial.print("\ "); 227 Serial.print(package.x); 228 Serial.print("\ "); 229 Serial.print(package.y); 230 Serial.print("\ "); 231 Serial.print(package.z); 232 233 234 Serial.println(" "); 235 236} 237 238void send() { 239 240 bool rslt; 241 rslt = radio.write( &package, sizeof(package) ); 242 // Always use sizeof() as it gives the size as the number of bytes. 243 // For example if dataToSend was an int sizeof() would correctly return 2 244 245 if (rslt) { 246 if ( radio.isAckPayloadAvailable() ) { 247 radio.read(&sent_1, sizeof(sent_1)); 248 newData = true; 249 } 250 else { 251 Serial.println(" Acknowledge but no data "); 252 } 253 } 254 else { 255 Serial.println(" Tx failed"); 256 } 257}
Transmitter
arduino
1#include <SPI.h> 2#include <nRF24L01.h> 3#include "RF24.h" 4 5 6 7#define 8 CE_PIN 9 //4 for nano 9#define CSN_PIN 10 //2 for nano 10 11//Radio 12 variables declaration 13 14const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'}; 15 16RF24 17 radio(CE_PIN, CSN_PIN); // Create a Radio 18 19struct Package 20{ 21 int8_t 22 thrust = 0; 23 int8_t x = 0; 24 int8_t y = 0; 25 int8_t z = 0; 26}; 27 28struct 29 Sent_1 30{ 31 int8_t gyro_x = 0; 32 int8_t gyro_y = 0; 33 int8_t gyro_z 34 = 0; 35 int8_t Front_Left; 36 int8_t Front_Right; 37 int8_t Rear_Left; 38 39 int8_t Rear_Right; 40}; 41 42Package package; 43Sent_1 sent_1; 44 45bool 46 newData = false; 47 48long unsigned int current_time = 0; 49const int signal_confimation_rate 50 = 1000; //how often to send a radio confimation signal 51long unsigned 52 int signal_confimation_time = 1000; //counts how often this signal is sent 53 54//Joystick 55 variables declaration 56 57//Input pins 58const int XL_pin = 0; // analog pin 59 connected to XL output 60const int YL_pin = 1; // analog pin connected to YL output 61const 62 int XR_pin = 2; // analog pin connected to XR output 63const int YR_pin = 3; // 64 analog pin connected to YR output 65 66//Pot values 67float xr, yr; 68float 69 xl, yl; 70float thrustRate; 71 72//Raw pot values 73float xr_raw, yr_raw; 74float 75 xl_raw, yl_raw; 76 77//Smoothing Vars 78const int numReadings = 20; 79int readIndex 80 = 0; // the index of the current reading 81 82float readings_xr[numReadings]; 83 // the readings from the analog input 84float total_xr = 0; // 85 the running total 86float average_xr = 0; // the average 87 88float 89 readings_yr[numReadings]; // the readings from the analog input 90float total_yr 91 = 0; // the running total 92float average_yr = 0; // 93 the average 94 95float readings_xl[numReadings]; // the readings from the 96 analog input 97float total_xl = 0; // the running total 98float 99 average_xl = 0; // the average 100 101//Controller Values 102int 103 xr_mid; 104int yr_mid; 105int xl_mid; 106int yl_mid; 107//Controller angle setting 108int 109 max_tilt = 10; 110int min_tilt = 1; 111//Values for linearly mapping pot values 112 to X,Y target angles 113 114void setup() 115{ 116 Serial.begin(115200); 117 delay(500); 118 119 Serial.println("Serial \ OK"); 120 121 radio.begin(); 122 radio.setDataRate( 123 RF24_250KBPS ); 124 radio.enableAckPayload(); 125 radio.setRetries(5, 5); // delay, 126 count 127 // 5 gives a 1500 µsec delay which is needed for a 32 byte ackPayload 128 129 radio.openWritingPipe(slaveAddress); 130 Serial.println("Radio \ OK"); 131 132 delay(500); 133 134 xr_mid = analogRead(XR_pin); 135 yr_mid = analogRead(YR_pin); 136 137 xl_mid = analogRead(XL_pin); 138 yl_mid = analogRead(YL_pin); 139} 140 141void 142 loop() { 143 144 //Reading Joystick 145 processingJoyStick(); 146 147 //Packing 148 data 149 package.y = -average_yr; 150 package.x = -average_xr; 151 package.z 152 = -average_xl; 153 154 //Thrust rate is a stationary value 155 thrustRate = yl; 156 157 package.thrust = package.thrust + thrustRate; 158 159 if (package.thrust >= 1800) 160 { 161 package.thrust = 1800; 162 } 163 if (package.thrust <= 1000) { 164 package.thrust 165 = 1000; 166 } 167 168 send(); 169 170 printPackage(); 171} 172 173void processingJoyStick() 174{ 175 176 xr_raw = analogRead(XR_pin); 177 yr_raw = analogRead(YR_pin); 178 xl_raw = analogRead(XL_pin); 179 180 yl_raw = analogRead(YL_pin); 181 182 if (xr_raw > (xr_mid + 50)) { 183 xr 184 = (0.02 * xr_raw) - 10; 185 } 186 if (xr_raw < (xr_mid - 50)) { 187 xr = (0.02 188 * xr_raw) - 10; 189 } 190 if (xr_raw > 471 && xr_raw < 569) { 191 xr = 0; 192 193 } 194 if (yr_raw > 570) { 195 yr = (0.02 * yr_raw) - 10; 196 } 197 if (yr_raw 198 < 470) { 199 yr = (0.02 * yr_raw) - 10; 200 } 201 if (yr_raw > 471 && yr_raw 202 < 569) { 203 yr = 0; 204 } 205 if (xl_raw > 570) { 206 xl = (0.02 * xl_raw) 207 - 10; 208 } 209 if (xl_raw < 470) { 210 xl = (0.02 * xl_raw) - 10; 211 } 212 213 if (xl_raw > 471 && xl_raw < 569) { 214 xl = 0; 215 } 216 if (yl_raw > 570) 217 { 218 yl = (-0.02 * yl_raw) + 10; 219 } 220 if (yl_raw < 400) { 221 yl = 222 (-0.02 * yl_raw) + 10; 223 } 224 if (yl_raw > 401 && yl_raw < 569) { 225 yl 226 = 0; 227 } 228 229 //Control values must be smoothed or they cause the PDI control 230 system to spaz out when it receives a 10 straight away 231 //Smoothing XR values 232 233 total_xr = total_xr - readings_xr[readIndex]; 234 // read from the sensor: 235 236 readings_xr[readIndex] = xr; 237 // add the reading to the total: 238 total_xr 239 = total_xr + readings_xr[readIndex]; 240 241 //Smoothing YR values 242 total_yr 243 = total_yr - readings_yr[readIndex]; 244 // read from the sensor: 245 readings_yr[readIndex] 246 = yr; 247 // add the reading to the total: 248 total_yr = total_yr + readings_yr[readIndex]; 249 250 251 //Smoothing XL values 252 total_xl = total_xl - readings_xl[readIndex]; 253 254 // read from the sensor: 255 readings_xl[readIndex] = xl; 256 // add the reading 257 to the total: 258 total_xl = total_xl + readings_xl[readIndex]; 259 // advance 260 to the next position in the array: 261 readIndex = readIndex + 1; 262 263 // if 264 we're at the end of the array... 265 if (readIndex >= numReadings) { 266 // 267 ...wrap around to the beginning: 268 readIndex = 0; 269 } 270 271 // calculate 272 the xr average: 273 average_xr = total_xr / numReadings; 274 275 // calculate 276 the yr average: 277 average_yr = total_yr / numReadings; 278 279 // calculate 280 the xl average: 281 average_xl = total_xl / numReadings; 282 283} 284 285void 286 printPackage() 287{ 288 Serial.print(sent_1.gyro_x); 289 Serial.print("\ "); 290 291 Serial.print(sent_1.gyro_y); 292 Serial.print("\ "); 293 Serial.print(sent_1.gyro_z); 294 295 Serial.print("\ "); 296 Serial.print(package.x); 297 Serial.print("\ "); 298 299 Serial.print(package.y); 300 Serial.print("\ "); 301 Serial.print(package.z); 302 303 304 305 Serial.println(" "); 306 307} 308 309void send() { 310 311 bool rslt; 312 313 rslt = radio.write( &package, sizeof(package) ); 314 // Always use sizeof() as 315 it gives the size as the number of bytes. 316 // For example if dataToSend was 317 an int sizeof() would correctly return 2 318 319 if (rslt) { 320 if ( radio.isAckPayloadAvailable() 321 ) { 322 radio.read(&sent_1, sizeof(sent_1)); 323 newData = true; 324 } 325 326 else { 327 Serial.println(" Acknowledge but no data "); 328 } 329 330 } 331 else { 332 Serial.println(" Tx failed"); 333 } 334}
Receiver
arduino
1#include <SPI.h> 2#include <nRF24L01.h> 3#include <RF24.h> 4 5#define CE_PIN 4 6#define CSN_PIN 2 7 8RF24 myRadio(4, 2); const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'}; 9 10RF24 radio(CE_PIN, CSN_PIN); 11 12char dataReceived[10]; // this must match dataToSend in the TX 13 14struct Package 15{ 16 uint8_t thrust = 1000; 17 uint8_t x = 1; 18 uint8_t y = 2; 19 uint8_t z = 3; 20}; 21 22Package package; 23 24struct Sent_1 25{ 26 uint8_t gyro_x = 4; 27 uint8_t gyro_y = 5; 28 uint8_t gyro_z = 6; 29 uint8_t Front_Left; 30 uint8_t Front_Right; 31 uint8_t Rear_Left; 32 uint8_t Rear_Right; 33}; 34 35Sent_1 sent_1; 36 37bool newData = false; 38 39void setup() { 40 Serial.begin(115200); 41 delay(1000); 42 43 Serial.println("SimpleRxAckPayload Starting"); 44 radio.begin(); 45 radio.setDataRate( RF24_250KBPS ); 46 radio.openReadingPipe(1, thisSlaveAddress); 47 radio.enableAckPayload(); 48 radio.startListening(); 49 radio.writeAckPayload(1, &sent_1, sizeof(sent_1)); // pre-load data 50 51} 52 53void loop() { 54 packageReply(); 55 getData(); 56 showData(); 57} 58 59void packageReply() { 60 61 sent_1.gyro_x = 4; 62 sent_1.gyro_y = 5; 63 sent_1.gyro_z = 6; 64 65 Serial.print(package.x); 66 Serial.print(" "); 67 Serial.print(package.y); 68 Serial.print(" "); 69 Serial.println(package.z); 70 71} 72 73void getData() { 74 if ( radio.available() ) { 75 radio.read( &package, sizeof(package) ); 76 updateReplyData(); 77 newData = true; 78 } 79} 80 81//================ 82 83void showData() { 84 if (newData == true) { 85 Serial.print("Data received "); 86 Serial.println(dataReceived); 87 Serial.print(" ackPayload sent "); 88 Serial.print(package.x); 89 Serial.print(", "); 90 Serial.println(sent_1.gyro_x); 91 newData = false; 92 } 93} 94 95//================ 96 97void updateReplyData() { 98 radio.writeAckPayload(1, &sent_1, sizeof(sent_1)); // load the payload for the next time 99}
Downloadable files
Teensy 4.0
Use this to connect the necessary pins to the NRF24, notice how there are no CE or CSN pin, these are specified in the code.
Teensy 4.0

NRF24 Pinout
Follow this to connect the pins to the necessary pins on the teensy or Arduino
NRF24 Pinout

Arduino Pinout
Use this to connect the necessary pins to the NRF24, notice how there are no CE or CSN pin, these are specified in the code.
Arduino Pinout

Teensy 4.0
Use this to connect the necessary pins to the NRF24, notice how there are no CE or CSN pin, these are specified in the code.
Teensy 4.0

NRF24 Pinout
Follow this to connect the pins to the necessary pins on the teensy or Arduino
NRF24 Pinout

Arduino Pinout
Use this to connect the necessary pins to the NRF24, notice how there are no CE or CSN pin, these are specified in the code.
Arduino Pinout

Comments
Only logged in users can leave comments