Advertisement
Cpt_Jellyfish

EEEE4008: Remote-Control Arduino Code

May 13th, 2021
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. //***send all data from the remote to be received on another NRF24L01 and print to the serial monitor
  2.  
  3. //include libraries & header files
  4. //I2C libraries
  5. #include <SPI.h>
  6. #include <Wire.h>
  7. //RF libraries
  8. #include <nRF24L01.h>
  9. #include <RF24.h>
  10. //LCD library
  11. #include "rgb_lcd.h"
  12.  
  13. //definitions
  14. #define sw1T      5  
  15. #define sw2S      4
  16. #define sw3F      3
  17. #define sw4G      2
  18.  
  19. //initalise the RF module
  20. RF24 radio(9, 10);   // nRF24L01 (CE, CSN)
  21. const byte address[6] = "00001"; // Address
  22.  
  23. //initalise the LCD screen
  24. rgb_lcd lcd;
  25.  
  26. // initalise the data structure
  27. // max size of this structure is 32 bytes - NRF24L01 buffer limit
  28. struct Data_Package {
  29.   byte jLValX;
  30.   byte jLValY;
  31.   byte jRValX;
  32.   byte jRValY;
  33.   byte toggleButton;
  34.   byte switchButton;
  35.   byte fireButton;
  36.   byte grabButton;
  37. };
  38.  
  39. Data_Package data; //create a variable with the above structure
  40.  
  41. //LCD Colour - red tint
  42. const int colorR = 20;
  43. const int colorG = 0;
  44. const int colorB = 0;
  45.  
  46. //setup the hardware
  47. void setup() {
  48.   Serial.begin(9600);   //serial output
  49.   pinMode(9, OUTPUT);   //set pin 9 as an output
  50.  
  51.   // define the radio communication
  52.   radio.begin();
  53.   radio.openWritingPipe(address);
  54.   radio.setAutoAck(false);
  55.   radio.setDataRate(RF24_250KBPS);
  56.   radio.setPALevel(RF24_PA_LOW);
  57.  
  58.   //arduino pull-up resistors
  59.   pinMode(sw1T, INPUT_PULLUP);
  60.   pinMode(sw2S, INPUT_PULLUP);
  61.   pinMode(sw3F, INPUT_PULLUP);
  62.   pinMode(sw4G, INPUT_PULLUP);
  63.  
  64.   // set initial default values
  65.   data.jLValX = 127; //map value from 0-1023 to 0-255 (byte)
  66.   data.jLValY = 127;
  67.   data.jRValX = 127;
  68.   data.jRValY = 127;
  69.   data.toggleButton = 1; //connected to GND so 0 when pressed
  70.   data.switchButton = 1;
  71.   data.fireButton = 1;
  72.   data.grabButton = 1;
  73.  
  74.   // setup the LCD screen
  75.   lcd.begin(16, 2);
  76.   lcd.setRGB(colorR, colorG, colorB);
  77. }
  78.  
  79. void loop() {
  80.   // read all analog inputs and map them to a one byte value
  81.   data.jLValX = map(analogRead(A0), 0, 1023, 0, 255); // map 0-1023 into a byte value from 0-255
  82.   data.jLValY = map(analogRead(A1), 0, 1023, 0, 255);
  83.   data.jRValX = map(analogRead(A2), 0, 1023, 0, 255);
  84.   data.jRValY = map(analogRead(A3), 0, 1023, 0, 255);
  85.  
  86.   // read all digital inputs
  87.   data.toggleButton = digitalRead(sw1T);
  88.   data.switchButton = digitalRead(sw2S);
  89.   data.fireButton = digitalRead(sw3F);
  90.   data.grabButton = digitalRead(sw4G);
  91.  
  92.   // send the whole data from the structure to the receiver
  93.   radio.write(&data, sizeof(Data_Package));
  94.  
  95.   // print to the serial monitor - troubleshooting only
  96.   Serial.print(data.jLValX);
  97.   Serial.print(",");
  98.   Serial.print(data.jLValY);
  99.   Serial.print(",");
  100.   Serial.print(data.jRValX);
  101.   Serial.print(",");
  102.   Serial.print(data.jRValY);
  103.   Serial.print(",");
  104.   Serial.print(data.toggleButton);
  105.   Serial.print(",");
  106.   Serial.print(data.switchButton);
  107.   Serial.print(",");
  108.   Serial.print(data.fireButton);
  109.   Serial.print(",");
  110.   Serial.println(data.grabButton);
  111.  
  112.   // print all values to the LCD screen for on board troubleshooting
  113.   lcd.setCursor(0,0);
  114.   lcd.print("Remote: ");
  115.   lcd.print(data.jLValX);
  116.   lcd.print(",");
  117.   lcd.print(data.jLValY);
  118.   lcd.print(" ");
  119.   lcd.setCursor(0,1);
  120.   lcd.print(data.jRValX);
  121.   lcd.print(",");
  122.   lcd.print(data.jRValY);
  123.   lcd.print(",");
  124.   lcd.print(data.toggleButton);
  125.   lcd.print(",");
  126.   lcd.print(data.switchButton);
  127.   lcd.print(",");
  128.   lcd.print(data.fireButton);
  129.   lcd.print(",");
  130.   lcd.print(data.grabButton);
  131.   lcd.print(" ");
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement