I'm using my Mega to communicate with my ESP32 via nRF24L01. I'm using the ezbutton library. I have 3 buttons on my transmitter and when I press each button, I want an LED to light up at the transmitter while a certain action is performed at the receiver's end. I am able to see joystick data come through to my receiver but it shows my joystick's analog values at the time the button was released which means for new joystick data to appear, I have to press ("release" technically) the button each time my joystick is moved. Can someone please assist? I've tried having a while loop nested within my if loop and while that seems to work for my transmitter, I see nothing come through to the receiver. Thanks in advance!
P.S. Yes, functions would help to clean up my code a whole lot, just laying it all out for now since I am still relatively new to functions in C.
// Transmitter
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example:
* + uses debounce for multiple buttons.
* + reads state of multiple buttons
* + detects the pressed and released events of multiple buttons
*/
#include <ezButton.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001"; // Address for communication
struct __attribute__((packed)) payload {
uint16_t mode;
uint16_t joystick[2];
uint16_t heading;
} data;
ezButton button1(11); // create ezButton object that attach to pin 11;
ezButton button2(30); // create ezButton object that attach to pin 30;
ezButton button3(40); // create ezButton object that attach to pin 40;
const byte led1 = 12;
const byte led2 = 5;
const byte led3 = 4;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
// add manual mode here - should start at initial setup
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
button1.setDebounceTime(50); // set debounce time to 50 milliseconds
button2.setDebounceTime(50); // set debounce time to 50 milliseconds
button3.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button1.loop(); // MUST call the loop() function first
button2.loop(); // MUST call the loop() function first
button3.loop(); // MUST call the loop() function first
int btn1State = button1.getState();
int btn2State = button2.getState();
int btn3State = button3.getState();
radio.write(&data, sizeof(data));
if(button1.isReleased()){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
data.mode = 0;
int rudder, throttle;
rudder = analogRead(A0);
throttle = 1023 - analogRead(A1);
data.joystick[0] = rudder;
data.joystick[1] = throttle;
Serial.print("Manual mode is active");
Serial.print(rudder); Serial.print("|"); Serial.print(throttle); Serial.println();
}
else if (button2.isReleased()){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
data.mode = 1;
Serial.println("Heading hold mode is active");
}
else if (button3.isReleased()){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
data.mode = 2;
Serial.println("Waypoint navigation mode is active");
}
}
// Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <ESP32Servo.h>
RF24 radio(4, 5); // CE, CSN pins for nRF24L01
const byte address[6] = "00001";
const int IN1 = 32; // Motor direction pin 1
const int IN2 = 33; // Motor direction pin 2
const int PWMA = 25; // Motor speed control (PWM)
const int STBY = 26; // Motor stby pin
const int servo1_pin = 14; // Servo pin
Servo servo1;
struct __attribute__((packed)) payload {
uint16_t mode; // 0: Manual, 1: Heading Hold, 2: Blink Mode
uint16_t joystick[2];
uint16_t heading;
} data;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(STBY, OUTPUT);
servo1.attach(servo1_pin);
pinMode(LED_BUILTIN, OUTPUT);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_LOW);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(&data, sizeof(data));
if (data.mode == 0) {
Serial.print("Mode 0 received");
int rudder = data.joystick[0];
int throttle = data.joystick[1];
Serial.print(rudder); Serial.print("|"); Serial.print(throttle); Serial.println();
if (throttle > 562) { // Forward; 50 added as deadzone to center position of 512 for stability
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, map(throttle, 562, 1023, 0, 255));
} else if (throttle < 462) { // Reverse
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, map(throttle, 0, 462, 255, 0));
} else { // Stop
digitalWrite(STBY, LOW);
}
int servo_angle = map(rudder, 0, 1023, 0, 180);
servo1.write(servo_angle);
}
else if (data.mode == 1) {
// Heading Hold Mode
Serial.print("Mode 1 received");
digitalWrite(LED_BUILTIN, HIGH); // Test code
}
else if (data.mode == 2) {
// Blink Mode
Serial.println("Mode 2 received");
digitalWrite(LED_BUILTIN, HIGH); // Test code
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
}
}