Advertisement
pleasedontcode

RF Control rev_02

Oct 15th, 2024
64
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: RF Control
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-15 16:56:47
  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. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* This code will use nf24.  transmit 0-5 from */
  24.     /* potentiometer, 3 switches for on off functions, */
  25.     /* emergency stop button, than when pressed will take */
  26.     /* potentiometer value to 0.  Emergency stop function */
  27.     /* that will take potentiometer value to 0 when */
  28.     /* transmitter */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  33. #include <RF24.h>        // Include RF24 library for nRF24L01 transceiver
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t Horn_PushButton_PIN_D2 = 2;
  41. const uint8_t Forward_Reverse_PushButton_PIN_D3 = 3;
  42. const uint8_t Light_PushButton_PIN_D4 = 4;
  43. const uint8_t Emergency_Stop_PushButton_PIN_D5 = 5;
  44.  
  45. /***** DEFINITION OF ANALOG INPUT PINS *****/
  46. const uint8_t Throttle_Potentiometer_Vout_PIN_A0 = A0;
  47.  
  48. /****** DEFINITION OF RF24 CONFIGURATION *****/
  49. RF24 radio(7, 8); // CE, CSN pins
  50. const uint64_t address = 0xF0F0F0F0E1LL; // Address for communication
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  53. // Instantiate EasyButton objects for each button
  54. EasyButton hornButton(Horn_PushButton_PIN_D2);
  55. EasyButton forwardReverseButton(Forward_Reverse_PushButton_PIN_D3);
  56. EasyButton lightButton(Light_PushButton_PIN_D4);
  57. EasyButton emergencyStopButton(Emergency_Stop_PushButton_PIN_D5);
  58.  
  59. /****** FUNCTION DEFINITIONS *****/
  60. void setup(void)
  61. {
  62.     // Initialize serial communication for debugging
  63.     Serial.begin(115200);
  64.    
  65.     // Set up button pins as input with pull-up resistors
  66.     pinMode(Horn_PushButton_PIN_D2, INPUT_PULLUP);
  67.     pinMode(Forward_Reverse_PushButton_PIN_D3, INPUT_PULLUP);
  68.     pinMode(Light_PushButton_PIN_D4, INPUT_PULLUP);
  69.     pinMode(Emergency_Stop_PushButton_PIN_D5, INPUT_PULLUP);
  70.     pinMode(Throttle_Potentiometer_Vout_PIN_A0, INPUT);
  71.  
  72.     // Initialize EasyButton instances
  73.     hornButton.begin();
  74.     forwardReverseButton.begin();
  75.     lightButton.begin();
  76.     emergencyStopButton.begin();
  77.  
  78.     // Set up button press event handlers
  79.     hornButton.onPressed([]() {
  80.         Serial.println("Horn button pressed");
  81.     });
  82.     forwardReverseButton.onPressed([]() {
  83.         Serial.println("Forward/Reverse button pressed");
  84.     });
  85.     lightButton.onPressed([]() {
  86.         Serial.println("Light button pressed");
  87.     });
  88.     emergencyStopButton.onPressed([]() {
  89.         Serial.println("Emergency Stop button pressed");
  90.     });
  91.  
  92.     // Initialize RF24 radio
  93.     radio.begin();
  94.     radio.openWritingPipe(address);
  95.     radio.setPALevel(RF24_PA_LOW); // Set power level
  96.     radio.stopListening(); // Set to transmit mode
  97. }
  98.  
  99. void loop(void)
  100. {
  101.     // Read the state of each button
  102.     hornButton.read();
  103.     forwardReverseButton.read();
  104.     lightButton.read();
  105.     emergencyStopButton.read();
  106.  
  107.     // Read potentiometer value
  108.     int potValue = analogRead(Throttle_Potentiometer_Vout_PIN_A0);
  109.  
  110.     // Check if emergency stop button is pressed
  111.     if (emergencyStopButton.isPressed()) {
  112.         potValue = 0; // Set potentiometer value to 0
  113.         Serial.println("Emergency Stop activated, setting potentiometer value to 0");
  114.     }
  115.  
  116.     // Transmit the potentiometer value and button states
  117.     int buttonStates = (hornButton.isPressed() << 3) | (forwardReverseButton.isPressed() << 2) |
  118.                        (lightButton.isPressed() << 1) | emergencyStopButton.isPressed();
  119.    
  120.     // Create a payload to send
  121.     struct Payload {
  122.         int potValue;
  123.         int buttonStates;
  124.     } payload;
  125.  
  126.     payload.potValue = potValue;
  127.     payload.buttonStates = buttonStates;
  128.  
  129.     // Send the payload
  130.     radio.write(&payload, sizeof(payload));
  131.     Serial.print("Transmitting Potentiometer Value: ");
  132.     Serial.print(potValue);
  133.     Serial.print(", Button States: ");
  134.     Serial.println(buttonStates);
  135.  
  136.     delay(100); // Delay for stability
  137. }
  138.  
  139. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement