CCCPaste

RF Control rev_02

/********* 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: RF Control
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-10-15 16:56:47

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* use rf24 to transmit values from potentiometer and */
	/* all switches to receiver */
/****** SYSTEM REQUIREMENT 2 *****/
	/* This code will use nf24.  transmit 0-5 from */
	/* potentiometer, 3 switches for on off functions, */
	/* emergency stop button, than when pressed will take */
	/* potentiometer value to 0.  Emergency stop function */
	/* that will take potentiometer value to 0 when */
	/* transmitter */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/evert-arias/EasyButton
#include         // Include RF24 library for nRF24L01 transceiver

/****** 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 RF24 CONFIGURATION *****/
RF24 radio(7, 8); // CE, CSN pins
const uint64_t address = 0xF0F0F0F0E1LL; // Address for communication

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// Instantiate 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);

/****** FUNCTION DEFINITIONS *****/
void setup(void)
{
    // Initialize serial communication for debugging
    Serial.begin(115200);
    
    // Set up button pins as input with pull-up resistors
    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 EasyButton instances
    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 radio
    radio.begin();
    radio.openWritingPipe(address);
    radio.setPALevel(RF24_PA_LOW); // Set power level
    radio.stopListening(); // Set to transmit mode
}

void loop(void)
{
    // Read the state of each button
    hornButton.read();
    forwardReverseButton.read();
    lightButton.read();
    emergencyStopButton.read();

    // Read potentiometer value
    int potValue = analogRead(Throttle_Potentiometer_Vout_PIN_A0);

    // Check if emergency stop button is pressed
    if (emergencyStopButton.isPressed()) {
        potValue = 0; // Set potentiometer value to 0
        Serial.println("Emergency Stop activated, setting potentiometer value to 0");
    }

    // Transmit the potentiometer value and button states
    int buttonStates = (hornButton.isPressed() << 3) | (forwardReverseButton.isPressed() << 2) |
                       (lightButton.isPressed() << 1) | emergencyStopButton.isPressed();
    
    // Create a payload to send
    struct Payload {
        int potValue;
        int buttonStates;
    } payload;

    payload.potValue = potValue;
    payload.buttonStates = buttonStates;

    // Send the payload
    radio.write(&payload, sizeof(payload));
    Serial.print("Transmitting Potentiometer Value: ");
    Serial.print(potValue);
    Serial.print(", Button States: ");
    Serial.println(buttonStates);

    delay(100); // Delay for stability
}

/* END CODE */