Advertisement
JonD1988

nrf24TransmitterRev2

Jul 14th, 2022
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Rev 0 Created 7/7/2022 This is a limited version of Reference 1 focusing only on 4 pushbuttons - Transmitter Code
  2. //Rev 1 Created 7/9/2022 This is my successful attempt to introduce a pause after each reading of the pushbuttons without using the delay function. This has the effect of working as a speed control too by adjusting the interval value. interval=10 seems to be a good speed.
  3. //Rev 2 Created 7/14/2022 This is my successfull attempt to create a 13 pushbutton transmitter
  4. #include <SPI.h>
  5. #include <nRF24L01.h>
  6. #include <RF24.h>
  7.  
  8. const int PB1p=2, PB2p=3, PB3p=4, PB4p=5, PB5p=6, PB6p=7, PB7p=10; //Pin numbers for Pushbutton Pins
  9.  
  10. const uint64_t pipeOut = 0xF9E8F0F0E1LL;   //IMPORTANT: The same as in the receiver 0xF9E8F0F0E1LL
  11.  
  12. RF24 radio(8, 9); // select CE,CSN pin
  13.  
  14. struct PacketData
  15. {
  16.   byte switch1Value;
  17.   byte switch2Value;
  18.   byte switch3Value;
  19.   byte switch4Value;
  20.   byte switch5Value;
  21.   byte switch6Value;
  22.   byte switch7Value;
  23.   byte switch8Value;
  24.   byte switch9Value;
  25.   byte switch10Value;
  26.   byte switch11Value;
  27.   byte switch12Value;
  28.   byte switch13Value;
  29. };
  30. PacketData data;
  31.  
  32. unsigned long previousMillis = 0;
  33. unsigned long interval = 10;
  34.  
  35. void setup()
  36. {
  37.   radio.begin();
  38.   radio.setDataRate(RF24_250KBPS);
  39.   radio.openWritingPipe(pipeOut);
  40.   radio.stopListening(); //start the radio Transmitter
  41.  
  42.   pinMode(PB1p,INPUT_PULLUP); //PB1
  43.   pinMode(PB2p,INPUT_PULLUP); //PB2
  44.   pinMode(PB3p,INPUT_PULLUP); //PB3
  45.   pinMode(PB4p,INPUT_PULLUP); //PB4
  46.   pinMode(PB5p,INPUT_PULLUP); //PB5
  47.   pinMode(PB6p,INPUT_PULLUP); //PB6
  48.   pinMode(PB7p,INPUT_PULLUP); //PB7
  49.   pinMode(A0,INPUT_PULLUP); //PB8
  50.   pinMode(A1,INPUT_PULLUP); //PB9
  51.   pinMode(A2,INPUT_PULLUP); //PB10
  52.   pinMode(A3,INPUT_PULLUP); //PB11
  53.   pinMode(A4,INPUT_PULLUP); //PB12
  54.   pinMode(A5,INPUT_PULLUP); //PB13    
  55. }
  56.  
  57. void loop()
  58. {
  59.   unsigned long currentMillis = millis();
  60.   if (currentMillis - previousMillis > interval)
  61.   {
  62.   previousMillis = currentMillis;
  63.   data.switch1Value   = !digitalRead(PB1p);
  64.   data.switch2Value   = !digitalRead(PB2p);
  65.   data.switch3Value   = !digitalRead(PB3p);
  66.   data.switch4Value   = !digitalRead(PB4p);
  67.   data.switch5Value   = !digitalRead(PB5p);
  68.   data.switch6Value   = !digitalRead(PB6p);
  69.   data.switch7Value   = !digitalRead(PB7p);
  70.   data.switch8Value   = !digitalRead(A0);
  71.   data.switch9Value   = !digitalRead(A1);
  72.   data.switch10Value   = !digitalRead(A2);
  73.   data.switch11Value   = !digitalRead(A3);
  74.   data.switch12Value   = !digitalRead(A4);
  75.   data.switch13Value   = !digitalRead(A5);
  76.  
  77.   radio.write(&data, sizeof(PacketData));
  78.   } //End of millis interval if statement
  79.  
  80. } //End of void loop()
  81.  
  82. //References
  83. //Reference 1- Adaptation of https://www.youtube.com/watch?v=gsHeNrDoK84&t=14s which is the "Hash Include Electronics" YouTube channel video "Arduino 10 Channels Wireless Transmitter Receiver | nrf24l01+| DIY" File Located at https://github.com/un0038998/10_Channel_NRF_Transmitter_Receiver
  84. //Reference 2- https://www.baldengineer.com/blink-without-delay-explained.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement