Advertisement
pleasedontcode

"Wireless Control" rev_01

Oct 15th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Wireless Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-15 16:52:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* use rf24 to transmit values from potentiometer and */
  21.     /* all switches to receiver */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  26. #include <SPI.h>        // Include SPI library for RF24
  27. #include <RF24.h>       // Include RF24 library for wireless communication
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t Horn_PushButton_PIN_D2        = 2;
  35. const uint8_t Forward_Reverse_PushButton_PIN_D3     = 3;
  36. const uint8_t Light_PushButton_PIN_D4       = 4;
  37. const uint8_t Emergency_Stop_PushButton_PIN_D5      = 5;
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t Throttle_Potentiometer_Vout_PIN_A0        = A0;
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. // Initialize EasyButton objects for each button
  44. EasyButton hornButton(Horn_PushButton_PIN_D2);
  45. EasyButton forwardReverseButton(Forward_Reverse_PushButton_PIN_D3);
  46. EasyButton lightButton(Light_PushButton_PIN_D4);
  47. EasyButton emergencyStopButton(Emergency_Stop_PushButton_PIN_D5);
  48.  
  49. // Initialize RF24 object
  50. RF24 radio(9, 10); // CE, CSN pins for the RF24 module
  51. const byte address[6] = "00001"; // Address for the RF24 communication
  52.  
  53. void setup(void)
  54. {
  55.     // Initialize serial communication for debugging
  56.     Serial.begin(115200);
  57.  
  58.     // Set pin modes for the buttons and potentiometer
  59.     pinMode(Horn_PushButton_PIN_D2, INPUT_PULLUP);
  60.     pinMode(Forward_Reverse_PushButton_PIN_D3,  INPUT_PULLUP);
  61.     pinMode(Light_PushButton_PIN_D4,    INPUT_PULLUP);
  62.     pinMode(Emergency_Stop_PushButton_PIN_D5,   INPUT_PULLUP);
  63.     pinMode(Throttle_Potentiometer_Vout_PIN_A0, INPUT);
  64.  
  65.     // Initialize the EasyButton objects
  66.     hornButton.begin();
  67.     forwardReverseButton.begin();
  68.     lightButton.begin();
  69.     emergencyStopButton.begin();
  70.  
  71.     // Set up button press event handlers
  72.     hornButton.onPressed([]() { Serial.println("Horn Button Pressed"); });
  73.     forwardReverseButton.onPressed([]() { Serial.println("Forward/Reverse Button Pressed"); });
  74.     lightButton.onPressed([]() { Serial.println("Light Button Pressed"); });
  75.     emergencyStopButton.onPressed([]() { Serial.println("Emergency Stop Button Pressed"); });
  76.  
  77.     // Initialize RF24 communication
  78.     radio.begin();
  79.     radio.openWritingPipe(address); // Set the address for writing
  80.     radio.setPALevel(RF24_PA_HIGH); // Set the power level for transmission
  81. }
  82.  
  83. void loop(void)
  84. {
  85.     // Read the state of the buttons
  86.     hornButton.read();
  87.     forwardReverseButton.read();
  88.     lightButton.read();
  89.     emergencyStopButton.read();
  90.  
  91.     // Read the potentiometer value
  92.     int potValue = analogRead(Throttle_Potentiometer_Vout_PIN_A0);
  93.  
  94.     // Create a structure to hold the data to send
  95.     struct DataToSend {
  96.         int potValue;
  97.         bool hornPressed;
  98.         bool forwardReversePressed;
  99.         bool lightPressed;
  100.         bool emergencyStopPressed;
  101.     } data;
  102.  
  103.     // Fill the structure with the current state
  104.     data.potValue = potValue;
  105.     data.hornPressed = hornButton.isPressed();
  106.     data.forwardReversePressed = forwardReverseButton.isPressed();
  107.     data.lightPressed = lightButton.isPressed();
  108.     data.emergencyStopPressed = emergencyStopButton.isPressed();
  109.  
  110.     // Send the data via RF24
  111.     radio.write(&data, sizeof(data));
  112.  
  113.     // Add a small delay to avoid flooding the receiver
  114.     delay(100);
  115. }
  116.  
  117. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement