Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "Wireless Control"
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-10-15 16:52:22
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* use rf24 to transmit values from potentiometer and */
- /* all switches to receiver */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- #include <SPI.h> // Include SPI library for RF24
- #include <RF24.h> // Include RF24 library for wireless communication
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Horn_PushButton_PIN_D2 = 2;
- const uint8_t Forward_Reverse_PushButton_PIN_D3 = 3;
- const uint8_t Light_PushButton_PIN_D4 = 4;
- const uint8_t Emergency_Stop_PushButton_PIN_D5 = 5;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t Throttle_Potentiometer_Vout_PIN_A0 = A0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Initialize EasyButton objects for each button
- EasyButton hornButton(Horn_PushButton_PIN_D2);
- EasyButton forwardReverseButton(Forward_Reverse_PushButton_PIN_D3);
- EasyButton lightButton(Light_PushButton_PIN_D4);
- EasyButton emergencyStopButton(Emergency_Stop_PushButton_PIN_D5);
- // Initialize RF24 object
- RF24 radio(9, 10); // CE, CSN pins for the RF24 module
- const byte address[6] = "00001"; // Address for the RF24 communication
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Set pin modes for the buttons and potentiometer
- pinMode(Horn_PushButton_PIN_D2, INPUT_PULLUP);
- pinMode(Forward_Reverse_PushButton_PIN_D3, INPUT_PULLUP);
- pinMode(Light_PushButton_PIN_D4, INPUT_PULLUP);
- pinMode(Emergency_Stop_PushButton_PIN_D5, INPUT_PULLUP);
- pinMode(Throttle_Potentiometer_Vout_PIN_A0, INPUT);
- // Initialize the EasyButton objects
- hornButton.begin();
- forwardReverseButton.begin();
- lightButton.begin();
- emergencyStopButton.begin();
- // Set up button press event handlers
- hornButton.onPressed([]() { Serial.println("Horn Button Pressed"); });
- forwardReverseButton.onPressed([]() { Serial.println("Forward/Reverse Button Pressed"); });
- lightButton.onPressed([]() { Serial.println("Light Button Pressed"); });
- emergencyStopButton.onPressed([]() { Serial.println("Emergency Stop Button Pressed"); });
- // Initialize RF24 communication
- radio.begin();
- radio.openWritingPipe(address); // Set the address for writing
- radio.setPALevel(RF24_PA_HIGH); // Set the power level for transmission
- }
- void loop(void)
- {
- // Read the state of the buttons
- hornButton.read();
- forwardReverseButton.read();
- lightButton.read();
- emergencyStopButton.read();
- // Read the potentiometer value
- int potValue = analogRead(Throttle_Potentiometer_Vout_PIN_A0);
- // Create a structure to hold the data to send
- struct DataToSend {
- int potValue;
- bool hornPressed;
- bool forwardReversePressed;
- bool lightPressed;
- bool emergencyStopPressed;
- } data;
- // Fill the structure with the current state
- data.potValue = potValue;
- data.hornPressed = hornButton.isPressed();
- data.forwardReversePressed = forwardReverseButton.isPressed();
- data.lightPressed = lightButton.isPressed();
- data.emergencyStopPressed = emergencyStopButton.isPressed();
- // Send the data via RF24
- radio.write(&data, sizeof(data));
- // Add a small delay to avoid flooding the receiver
- delay(100);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement