Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //***send all data from the remote to be received on another NRF24L01 and print to the serial monitor
- //include libraries & header files
- //I2C libraries
- #include <SPI.h>
- #include <Wire.h>
- //RF libraries
- #include <nRF24L01.h>
- #include <RF24.h>
- //LCD library
- #include "rgb_lcd.h"
- //definitions
- #define sw1T 5
- #define sw2S 4
- #define sw3F 3
- #define sw4G 2
- //initalise the RF module
- RF24 radio(9, 10); // nRF24L01 (CE, CSN)
- const byte address[6] = "00001"; // Address
- //initalise the LCD screen
- rgb_lcd lcd;
- // initalise the data structure
- // max size of this structure is 32 bytes - NRF24L01 buffer limit
- struct Data_Package {
- byte jLValX;
- byte jLValY;
- byte jRValX;
- byte jRValY;
- byte toggleButton;
- byte switchButton;
- byte fireButton;
- byte grabButton;
- };
- Data_Package data; //create a variable with the above structure
- //LCD Colour - red tint
- const int colorR = 20;
- const int colorG = 0;
- const int colorB = 0;
- //setup the hardware
- void setup() {
- Serial.begin(9600); //serial output
- pinMode(9, OUTPUT); //set pin 9 as an output
- // define the radio communication
- radio.begin();
- radio.openWritingPipe(address);
- radio.setAutoAck(false);
- radio.setDataRate(RF24_250KBPS);
- radio.setPALevel(RF24_PA_LOW);
- //arduino pull-up resistors
- pinMode(sw1T, INPUT_PULLUP);
- pinMode(sw2S, INPUT_PULLUP);
- pinMode(sw3F, INPUT_PULLUP);
- pinMode(sw4G, INPUT_PULLUP);
- // set initial default values
- data.jLValX = 127; //map value from 0-1023 to 0-255 (byte)
- data.jLValY = 127;
- data.jRValX = 127;
- data.jRValY = 127;
- data.toggleButton = 1; //connected to GND so 0 when pressed
- data.switchButton = 1;
- data.fireButton = 1;
- data.grabButton = 1;
- // setup the LCD screen
- lcd.begin(16, 2);
- lcd.setRGB(colorR, colorG, colorB);
- }
- void loop() {
- // read all analog inputs and map them to a one byte value
- data.jLValX = map(analogRead(A0), 0, 1023, 0, 255); // map 0-1023 into a byte value from 0-255
- data.jLValY = map(analogRead(A1), 0, 1023, 0, 255);
- data.jRValX = map(analogRead(A2), 0, 1023, 0, 255);
- data.jRValY = map(analogRead(A3), 0, 1023, 0, 255);
- // read all digital inputs
- data.toggleButton = digitalRead(sw1T);
- data.switchButton = digitalRead(sw2S);
- data.fireButton = digitalRead(sw3F);
- data.grabButton = digitalRead(sw4G);
- // send the whole data from the structure to the receiver
- radio.write(&data, sizeof(Data_Package));
- // print to the serial monitor - troubleshooting only
- Serial.print(data.jLValX);
- Serial.print(",");
- Serial.print(data.jLValY);
- Serial.print(",");
- Serial.print(data.jRValX);
- Serial.print(",");
- Serial.print(data.jRValY);
- Serial.print(",");
- Serial.print(data.toggleButton);
- Serial.print(",");
- Serial.print(data.switchButton);
- Serial.print(",");
- Serial.print(data.fireButton);
- Serial.print(",");
- Serial.println(data.grabButton);
- // print all values to the LCD screen for on board troubleshooting
- lcd.setCursor(0,0);
- lcd.print("Remote: ");
- lcd.print(data.jLValX);
- lcd.print(",");
- lcd.print(data.jLValY);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(data.jRValX);
- lcd.print(",");
- lcd.print(data.jRValY);
- lcd.print(",");
- lcd.print(data.toggleButton);
- lcd.print(",");
- lcd.print(data.switchButton);
- lcd.print(",");
- lcd.print(data.fireButton);
- lcd.print(",");
- lcd.print(data.grabButton);
- lcd.print(" ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement